对于一个程序员来说,文件操作是经常遇到的,尤其是对Excel文件的操作。
这里介绍一个操作excel的工具-POI,它是一个apache的类库,主要提供对office文件的处理。
代码示例1.读取Excel
1 public void testReadExcel() { 2 try { 3 // 读取Excel 4 Workbook wb = new HSSFWorkbook(new FileInputStream("d:\2.xls")); 5 6 // 获取sheet数目 7 for (int t = 0; t < wb.getNumberOfSheets(); t++) { 8 Sheet sheet = wb.getSheetAt(t); 9 Row row = null; 10 int lastRowNum = sheet.getLastRowNum(); 11 12 // 循环读取 13 for (int i = 0; i <= lastRowNum; i++) { 14 row = sheet.getRow(i); 15 if (row != null) { 16 // 获取每一列的值 17 for (int j = 0; j < row.getLastCellNum(); j++) { 18 Cell cell = row.getCell(j); 19 String value = getCellValue(cell) ; 20 if(!value.equals("")){ 21 System.out.print(value + " | "); 22 } 23 } 24 System.out.println(); 25 } 26 } 27 } 28 } catch (Exception e) { 29 e.printStackTrace(); 30 } 31 }
用到了一个方法:
1 /*** 2 * 读取单元格的值 3 * 4 * @Title: getCellValue 5 * @Date : 2014-9-11 上午10:52:07 6 * @param cell 7 * @return 8 */ 9 private String getCellValue(Cell cell) { 10 Object result = ""; 11 if (cell != null) { 12 switch (cell.getCellType()) { 13 case Cell.CELL_TYPE_STRING: 14 result = cell.getStringCellValue(); 15 break; 16 case Cell.CELL_TYPE_NUMERIC: 17 result = cell.getNumericCellValue(); 18 break; 19 case Cell.CELL_TYPE_BOOLEAN: 20 result = cell.getBooleanCellValue(); 21 break; 22 case Cell.CELL_TYPE_FORMULA: 23 result = cell.getCellFormula(); 24 break; 25 case Cell.CELL_TYPE_ERROR: 26 result = cell.getErrorCellValue(); 27 break; 28 case Cell.CELL_TYPE_BLANK: 29 break; 30 default: 31 break; 32 } 33 } 34 return result.toString(); 35 }
解释一下,首先将文件读入到工作簿Workbook中,Workbook是一个接口,他有2个实现:HSSFWorkbook和XSSFWorkbook。前者是用来读取97-03版的Excel,扩展名为xls,后者是读取07及以后的版本,扩展名为xlsx。读入到workbook中,然后循环所有的sheet,在sheet循环所有的有效行和有效列。其中sheet.getLastRowNum()获得最后一行的索引值(从0开始),而sheet.getPhysicalNumberOfRows()则是获取的最后一行的行号(从1开始)。这里要注意的是循环列不是在sheet中循环,而是在row中循环。
效果图如下:
代码示例2. 写入Excel文件
1 public void testWriteExcel() { 2 String excelPath = "d:/3.xls"; 3 4 Workbook workbook = null; 5 try { 6 // XSSFWorkbook used for .xslx (>= 2007), HSSWorkbook for 03 .xsl 7 workbook = new HSSFWorkbook();// XSSFWorkbook();//WorkbookFactory.create(inputStream); 8 } catch (Exception e) { 9 System.out.println("创建Excel失败: "); 10 e.printStackTrace(); 11 } 12 if (workbook != null) { 13 Sheet sheet = workbook.createSheet("测试数据"); 14 Row row0 = sheet.createRow(0); 15 for (int i = 0; i < 6; i++) { 16 Cell cell = row0.createCell(i, Cell.CELL_TYPE_STRING); 17 cell.setCellValue("列标题" + i ); 18 //sheet.autoSizeColumn(i);//自动调整宽度 19 } 20 for (int rowNum = 1; rowNum < 10; rowNum++) { 21 Row row = sheet.createRow(rowNum); 22 for (int i = 0; i < 6; i++) { 23 Cell cell = row.createCell(i, Cell.CELL_TYPE_STRING); 24 cell.setCellValue("单元格" + String.valueOf(rowNum + 1) 25 + String.valueOf(i + 1)); 26 } 27 } 28 try { 29 FileOutputStream outputStream = new FileOutputStream(excelPath); 30 workbook.write(outputStream); 31 outputStream.flush(); 32 outputStream.close(); 33 } catch (Exception e) { 34 System.out .println("写入Excel失败: "); 35 e.printStackTrace(); 36 } 37 } 38 }
效果图如下: