java中使用POI如何获得EXCEL中的一行数据?
static public void main(String[] args){
try {
FileInputStream fis = new FileInputStream("d:\\aa.xls");
HSSFWorkbook wb = new HSSFWorkbook(fis);
HSSFSheet s = wb.getSheet("Sheet1");
HSSFRow row=s.getRow(0);
HSSFCell cell=row.getCell((short) 0);
System.out.println(cell.getStringCellValue());
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
请问怎样用java 逐条 读取Excel表格数据?并获取那个值?
使用APACHE的POI JAR包,有各种方法操作excel表格
搜下很多说明的
怎么用java把excel中的数据一行一行的读出来
请了解poi相关知识
java怎么读取很大的excel(20w条数据),怎么通过字节流获取到单元格的内容
使用java poi
package webservice;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ExcelController {
@SuppressWarnings("deprecation")
public void excel() throws FileNotFoundException, IOException{
String filename = "d:\\excel.xls";
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filename));
//按名引用excel工作表
// HSSFSheet sheet = workbook.getSheet("JSP");
//也可以用以下方式来获取excel的工作表,采用工作表的索引值
HSSFSheet sheet = workbook.getSheetAt(0);
HSSFRow row ;
HSSFCell cell1;
int rows=sheet.getLastRowNum();
for(int icount=0;icount
row = sheet.getRow(icount);
int line=row.getPhysicalNumberOfCells();
for(int j=0;j
cell1= row.getCell(j);
System.out.println(cell1 "--" icount "---" j);
}
}
//打取值
// System.out.println(cell.getStringCellValue());
//新建一输出流
FileOutputStream fout = new FileOutputStream(filename); //PS:filename 是你另存为的路径,不处理直接写入模版文件
//存盘
workbook.write(fout);
fout.flush();
//结束关闭
fout.close();
}
public HSSFCell getCell(HSSFRow row, int index) {
// 取得分发日期单元格
HSSFCell cell = row.getCell(index);
// 如果单元格不存在
if (cell == null) {
// 创建单元格
cell = row.createCell(index);
}
// 返回单元格
return cell;
}
public static void main(String[] args) {
ExcelController ec = new ExcelController();
try {
ec.excel();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
java如何读取整个excel文件的内容
工具:
参考代码释如下:
import Java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
public class ReadExcel {
public static void readExcel(File file){
try {
InputStream inputStream = new FileInputStream(file);
String fileName = file.getName();
Workbook wb = null;
// poi-3.9.jar 只可以读取2007以下本,后缀为:xsl
wb = new HSSFWorkbook(inputStream);//解析xls格式
Sheet sheet = wb.getSheetAt(0);//第一个表 ,第二个则为1,以此类推...
int firstRowIndex = sheet.getFirstRowNum();
int lastRowIndex = sheet.getLastRowNum();
for(int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex ){
Row row = sheet.getRow(rIndex);
if(row != null){
int firstCellIndex = row.getFirstCellNum();
// int lastCellIndex = row.getLastCellNum();
//此处参数cIndex决定可以取到excel的列数。
for(int cIndex = firstCellIndex; cIndex
Cell cell = row.getCell(cIndex);
String value = "";
if(cell != null){
value = cell.toString();
System.out.print(value "\t");
}
}
System.out.println();
}
}
} catch (FileNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
public static void main(String[] args) {
File file = new File("D:/test.xls");
readExcel(file);
}
}
java如何读取整个excel文件的内容?
在Java中读Excel文件的内容
在这里使用的是一个叫Java Excel API的东西,类似的还有jakarta的POI,不过感觉那个
太复杂了点儿。而且jxl文的支持相当的好,至少我在用的过程中一点问题没出。
一、下载地址
http://www.andykhan.com/jexcelapi/
二、特性
可以读取Excel 95, 97, 2000文件
可以读或写Excel 97及其以后版本的的公式(不过我发现好像有bug)
生成Excel 97格式的电子表格
支持字体、数字和日期格式化
支持单元格的颜色和阴影
可以编辑现文件
三、读文件
//声明一下,记得后面要关闭哦。。
Workbook workbook = null;
try {
workbook = Workbook.getWorkbook(new File("d:\\temp\\TestRead.xls"));
} catch (Exception e) {
throw new Exception("file to import not found!");
}
Sheet sheet = workbook.getSheet(0);
Cell cell = null;
int columnCount=3;
int rowCount=sheet.getRows();
for (int i = 0; i
for (int j = 0; j
//注意,这里的两个参数,第一个是表示列的,第二才表示行
cell=sheet.getCell(j, i);
//要根据单元格的类型分别做处理,否则格式化过的内容可能会不正确
if(cell.getType()==CellType.NUMBER){
System.out.print(((NumberCell)cell).getValue());
}
else if(cell.getType()==CellType.DATE){
System.out.print(((DateCell)cell).getDate());
}
else{
System.out.print(cell.getContents());
}
//System.out.print(cell.getContents());
System.out.print("\t");
}
System.out.print("\n");
}
//关闭它,否则会有内存泄露
workbook.close();
怎么用java实现读取excel表格里的数据生成
Cell cell=workbook.getSheetAt(1).getRow(2).getCell(30);
我想用java来读取Excel文件的某行某列,就是指定读取某个位置的数据,然后显示出来!怎么弄?求
工作中用到的导excel一个方法,你还可以通过一些插件导入,代码要你自己了,基本原理如下...
public Object importDoucument(MultipartFile uploadfile)
{
StringBuffer resultMessage = new StringBuffer();
ExcelImport excelImport = new ExcelImport();
Sheet sheet = null;
try
{
// 验证文件格式 如不出错 返回工作簿
excelImport.verifyExeclFile(uploadfile);
ExcelBean excelBean = excelImport.getExcelBean();
if (null != excelBean)
{
sheet = excelBean.getSheet();
}
//导入excel文件分析整理出list对象
List dataList = getAssessCateRange(sheet, "战略要素名称", "战略要素名称", 2, 1);
int num = 0;
if(dataList.size()>0){
for (StcCoreElements itemStcVO : dataList)
{
StcCoreElements stcCoreElementsVo = nitemStcVO
//*****修改些处
this.save(stcCoreElementsVo);
num;
}
}
resultMessage.append("已成功导入 " num " 条核心要素信息");
}
catch (Exception e)
{
resultMessage.append(e.getMessage());
e.printStackTrace();
}
finally
{
excelImport.close();
}
return resultMessage;
}
private List getAssessCateRange(Sheet sheet, String startName, String endName, int rowNum, int titleRowNum)
{
int[] cateRange = new int[2];
List dataList = new ArrayList();
int lastRowNumber = sheet.getLastRowNum();
Row cateRow = sheet.getRow(rowNum - 1);
Cell cateCell = cateRow.getCell(0);
String cateCellValue = ImportExcelUtil.getCellValue(cateCell, sheet);
if (StringUtils.isNotBlank(cateCellValue))
{
if (StringUtils.startsWith(cateCellValue, startName))
{
cateRange[0] = rowNum titleRowNum;
}
}
String currentCellValue0 = "";
do
{
Row currentRow = sheet.getRow(rowNum);
StcCoreElements info =new StcCoreElements();
Cell currentCell0 = currentRow.getCell(0);
currentCellValue0 = ImportExcelUtil.getCellValue(currentCell0, sheet);
info.setOverallPlan(currentCellValue0);
dataList.add(info);
rowNum ;
} while (rowNum <= lastRowNumber);
return dataList;
}