最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
java解析excel文件的方法
时间:2017-03-14 编辑:简简单单 来源:一聚教程网
建立工程前需要导入POI包。POI相关jar包下载地址:http://poi.apache.org/download.html
1.解析.xlsx后缀名的的EXCEL文件:
代码如下 | 复制代码 |
packagecom.shuai.hello;
importjava.io.FileInputStream; importjava.io.IOException; importjava.io.InputStream;
importorg.apache.poi.hssf.usermodel.HSSFCell; importorg.apache.poi.xssf.usermodel.XSSFCell; importorg.apache.poi.xssf.usermodel.XSSFRow; importorg.apache.poi.xssf.usermodel.XSSFSheet; importorg.apache.poi.xssf.usermodel.XSSFWorkbook;
publicclassReadExcel { publicstaticvoidmain(String[] args)throwsIOException {
//File file = new File("C:/Users.xlsx"); InputStream stream =newFileInputStream("C:/Users.xlsx");
XSSFWorkbook xssfWorkbook =newXSSFWorkbook(stream); XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
introwstart = xssfSheet.getFirstRowNum(); introwEnd = xssfSheet.getLastRowNum(); for(inti=rowstart;i<=rowEnd;i++) { XSSFRow row = xssfSheet.getRow(i); if(null== row)continue; intcellStart = row.getFirstCellNum(); intcellEnd = row.getLastCellNum();
for(intk=cellStart;k<=cellEnd;k++) { XSSFCell cell = row.getCell(k); if(null==cell)continue;
switch(cell.getCellType()) { caseHSSFCell.CELL_TYPE_NUMERIC:// 数字 System.out.print(cell.getNumericCellValue() +"\t"); break; caseHSSFCell.CELL_TYPE_STRING:// 字符串 System.out.print(cell.getStringCellValue() +"\t"); break; caseHSSFCell.CELL_TYPE_BOOLEAN:// Boolean System.out.println(cell.getBooleanCellValue() +"\t"); break; caseHSSFCell.CELL_TYPE_FORMULA:// 公式 System.out.print(cell.getCellFormula() +"\t"); break; caseHSSFCell.CELL_TYPE_BLANK:// 空值 System.out.println(" "); break; caseHSSFCell.CELL_TYPE_ERROR:// 故障 System.out.println(" "); break; default: System.out.print("未知类型 "); break; }
} System.out.print("\n"); } } }
/*String fileType = filePath.substring(filePath.lastIndexOf(".") + 1, filePath.length()); InputStream stream = new FileInputStream(filePath); Workbook wb = null; if (fileType.equals("xls")) { wb = new HSSFWorkbook(stream); } else if (fileType.equals("xlsx")) { wb = new XSSFWorkbook(stream); } else { System.out.println("您输入的excel格式不正确"); }*/ |
2.解析后缀为.xls的EXCEL文件:
代码如下 | 复制代码 |
packagecom.shuai.hello;
importjava.io.File; importjava.io.FileInputStream; importjava.io.IOException;
importorg.apache.poi.hssf.usermodel.HSSFCell; importorg.apache.poi.hssf.usermodel.HSSFRow; importorg.apache.poi.hssf.usermodel.HSSFSheet; importorg.apache.poi.hssf.usermodel.HSSFWorkbook; importorg.apache.poi.poifs.filesystem.POIFSFileSystem;
publicclassReadXls { publicstaticvoidmain(String[] args)throwsIOException, IOException { File file =newFile("C:/Users/dengta/Desktop/ok1.xls"); POIFSFileSystem poifsFileSystem =newPOIFSFileSystem(newFileInputStream(file)); HSSFWorkbook hssfWorkbook =newHSSFWorkbook(poifsFileSystem); HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0);
introwstart = hssfSheet.getFirstRowNum(); introwEnd = hssfSheet.getLastRowNum(); for(inti=rowstart;i<=rowEnd;i++) { HSSFRow row = hssfSheet.getRow(i); if(null== row)continue; intcellStart = row.getFirstCellNum(); intcellEnd = row.getLastCellNum();
for(intk=cellStart;k<=cellEnd;k++) { HSSFCell cell = row.getCell(k); if(null==cell)continue; //System.out.print("" + k + " "); //System.out.print("type:"+cell.getCellType());
switch(cell.getCellType()) { caseHSSFCell.CELL_TYPE_NUMERIC:// 数字 System.out.print(cell.getNumericCellValue() +" "); break; caseHSSFCell.CELL_TYPE_STRING:// 字符串 System.out.print(cell.getStringCellValue() +" "); break; caseHSSFCell.CELL_TYPE_BOOLEAN:// Boolean System.out.println(cell.getBooleanCellValue() +" "); break; caseHSSFCell.CELL_TYPE_FORMULA:// 公式 System.out.print(cell.getCellFormula() +" "); break; caseHSSFCell.CELL_TYPE_BLANK:// 空值 System.out.println(" "); break; caseHSSFCell.CELL_TYPE_ERROR:// 故障 System.out.println(" "); break; default: System.out.print("未知类型 "); break; }
} System.out.print("\n"); } } } |
-
上一个: Java性能调优概述
-
下一个: Java不可变类机制浅析
相关文章
- SpringBoot测试配置属性与web启动环境解析 10-24
- vue中将el-switch值true、false改为number类型的1和0解析 10-24
- Vue中的路由配置项meta使用解读 10-24
- SpringBoot自定义bean绑定解析 10-24
- SpringBoot常用计量与bean属性校验和进制数据转换规则解析 10-24
- 工厂方法在Spring框架中的运用介绍 10-24