Java读写Excel的包是Apache POI(项目地址:http://poi.apache.org/),因此需要先获取POI的jar包,本实验使用的是POI 3.9稳定版。
Apache POI 代码例子地址:http://poi.apache.org/spreadsheet/quick-guide.html
本例子可以读取Microsoft Office Excel 2003/2007/2010,具体代码及注释如下:
读取“.xls”格式使用 import org.apache.poi.hssf.usermodel.*;包的内容,例如:HSSFWorkbook
读取“.xlsx”格式使用 import org.apache.poi.xssf.usermodel.*; 包的内容,例如:XSSFWorkbook
读取两种格式使用 import org.apache.poi.ss.usermodel.* 包的内容,例如:Workbook
引入包如下:
1 importorg.apache.poi.ss.usermodel.Cell;2 importorg.apache.poi.ss.usermodel.Row;3 importorg.apache.poi.ss.usermodel.Sheet;4 importorg.apache.poi.ss.usermodel.Workbook;5 importorg.apache.poi.ss.usermodel.WorkbookFactory;6 import org.apache.poi.ss.usermodel.DateUtil;
【其中的DateUtil不是必须的】
1 /**
2 * 读取Excel测试,兼容 Excel 2003/2007/20103 */
4 publicString readExcel()5 {6 SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");7 try{8 //同时支持Excel 2003、2007
9 File excelFile = new File("/home/zht/test.xls"); //创建文件对象
10 FileInputStream is = new FileInputStream(excelFile); //文件流
11 Workbook workbook = WorkbookFactory.create(is); //这种方式 Excel 2003/2007/2010 都是可以处理的
12 int sheetCount = workbook.getNumberOfSheets(); //Sheet的数量13 //遍历每个Sheet
14 for (int s = 0; s < sheetCount; s++) {15 Sheet sheet =workbook.getSheetAt(s);16 int rowCount = sheet.getPhysicalNumberOfRows(); //获取总行数17 //遍历每一行
18 for (int r = 0; r < rowCount; r++) {19 Row row =sheet.getRow(r);20 int cellCount = row.getPhysicalNumberOfCells(); //获取总列数21 //遍历每一列
22 for (int c = 0; c < cellCount; c++) {23 Cell cell =row.getCell(c);24 int cellType =cell.getCellType();25 String cellValue = null;26 switch(cellType) {27 case Cell.CELL_TYPE_STRING: //文本
28 cellValue =cell.getStringCellValue();29 break;30 case Cell.CELL_TYPE_NUMERIC: //数字、日期
31 if(DateUtil.isCellDateFormatted(cell)) {32 cellValue = fmt.format(cell.getDateCellValue()); //日期型
33 }34 else{35 cellValue = String.valueOf(cell.getNumericCellValue()); //数字
36 }37 break;38 case Cell.CELL_TYPE_BOOLEAN: //布尔型
39 cellValue =String.valueOf(cell.getBooleanCellValue());40 break;41 case Cell.CELL_TYPE_BLANK: //空白
42 cellValue =cell.getStringCellValue();43 break;44 case Cell.CELL_TYPE_ERROR: //错误
45 cellValue = "错误";46 break;47 case Cell.CELL_TYPE_FORMULA: //公式
48 cellValue = "错误";49 break;50 default:51 cellValue = "错误";52 }53 System.out.print(cellValue + " ");54 }55 System.out.println();56 }57 }58
59 }60 catch(Exception e) {61 e.printStackTrace();62 }63
64 returnAction.SUCCESS;65 }
如果执行的代码的过程中报出如下错误:
poi导入excel表格数据时Cannot get a text value from a numeric cell
异常描述:在导入excel的时候在获取excel单元格数据的时候会出现Cannot get a text value from a numeric cell的异常抛出。
异常原因:poi读取excel单元格的数据,cell有不同的数据类型(CELL_TYPE_NUMERIC,CELL_TYPE_STRING,CELL_TYPE_FORMULA),如果cell中的数据是数值的话,如果你没有给他设置cell的类型的话。默认会认为是CELL_TYPE_NUMERICl类型,如果从一个NUMBER类型的Cell使用.cell.getStringCellValue()读取出一个字符串就会出错。
解决的方法:在读取数据之前,设置cell的类型为CELL_TYPE_STRING;
cell.setCellType(Cell.CELL_TYPE_STRING);
所以,上面的代码可以简单写成:
1 /**
2 * 读取Excel测试,兼容 Excel 2003/2007/20103 */
4 publicString readExcel()5 {6 SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");7 try{8 //同时支持Excel 2003、2007
9 File excelFile = new File("/home/zht/test.xls"); //创建文件对象
10 FileInputStream is = new FileInputStream(excelFile); //文件流
11 Workbook workbook = WorkbookFactory.create(is); //这种方式 Excel 2003/2007/2010 都是可以处理的
12 int sheetCount = workbook.getNumberOfSheets(); //Sheet的数量13 //遍历每个Sheet
14 for (int s = 0; s < sheetCount; s++) {15 Sheet sheet =workbook.getSheetAt(s);16 int rowCount = sheet.getPhysicalNumberOfRows(); //获取总行数17 //遍历每一行
18 for (int r = 0; r < rowCount; r++) {19 Row row =sheet.getRow(r);20 int cellCount = row.getPhysicalNumberOfCells(); //获取总列数21 //遍历每一个单元格
22 for (int c = 0; c < cellCount; c++) {23 Cell cell =row.getCell(c);24 int cellType =cell.getCellType();25 String cellValue = null;26
27 //在读取单元格内容前,设置所有单元格中内容都是字符串类型
28 cell.setCellType(Cell.CELL_TYPE_STRING);29
30 //按照字符串类型读取单元格内数据
31 cellValue =cell.getStringCellValue();32
33 /*在这里可以对每个单元格中的值进行二次操作转化*/
34
35 System.out.print(cellValue + " ");36 }37 System.out.println();38 }39 }40
41 }42 catch(Exception e) {43 e.printStackTrace();44 }45
46 returnAction.SUCCESS;47 }
项目中原来就有maven的相关poi依赖,具体如下:
1 maven依赖配置:2
3
4 fakepath
5 poi-ooxml-schemas
6 3.14-20160307
7
8 fakepath
9 poi-scratchpad
10 3.14-20160307
11
12 fakepath
13 poi-ooxml
14 3.14-20160307
15
16 fakepath
17 poi-examples
18 3.14-20160307
19
20 fakepath
21 poi-excelant
22 3.14-20160307
23
24 fakepath
25 poi
26 3.14-20160307
27
28 fakepath
29 xmlbeans
30 2.6.0
31
32