標籤:
處理excel檔案的開源庫有很多,常見的POI、jxls。。。。。
重點分析下POI處理excel的方法:
1、寫檔案
// 按照行優先進行資料表格的初始化 public static void createRows() throws IOException { Workbook wb = new HSSFWorkbook(); // 建立表格 Sheet sheet = wb.createSheet("測試Sheet_01"); List<String> list = new ArrayList<String>(); list.add("a"); list.add("b"); int rowCount = list.size(); // 根據資料集設定行數 for (int i = 0; i < rowCount; i++) { Row row = sheet.createRow(i);// 建立行,表頭是第0行 for (int j = 0; j < list.size(); j++) { // 為第一個儲存格賦值 Cell cell = row.createCell(0); cell.setCellValue("測試資料"); // 為第二個儲存格賦值 Cell cell_1 = row.createCell(1); cell_1.setCellValue("test"); } } FileOutputStream fos = new FileOutputStream("/Users/file/測試的Excel.xls"); wb.write(fos); if (null != fos) { fos.close(); } System.out.println("測試資料完成輸出。。"); }
2、讀檔案
// 將檔案內容轉換為二維數組 @SuppressWarnings("resource") private static String[][] getData(FileItem fileItem, int ignoreRows) throws FileNotFoundException, IOException { List<String[]> result = new ArrayList<String[]>(); int rowSize = 0; BufferedInputStream in = new BufferedInputStream(fileItem.getInputStream()); Workbook wb; if (fileItem.getName().indexOf(".xlsx") > -1) { wb = new XSSFWorkbook(in); } else { wb = new HSSFWorkbook(in); } Cell cell = null; for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) { Sheet st = wb.getSheetAt(sheetIndex); // 第一行為標題,不取 for (int rowIndex = ignoreRows; rowIndex <= st.getLastRowNum(); rowIndex++) { Row row = st.getRow(rowIndex); if (row == null) { continue; } int tempRowSize = row.getLastCellNum() + 1; if (tempRowSize > rowSize) { rowSize = tempRowSize; } String[] values = new String[rowSize]; Arrays.fill(values, ""); boolean hasValue = false; for (int columnIndex = 0; columnIndex <= row.getLastCellNum(); columnIndex++) { String value = ""; cell = row.getCell(columnIndex); if (cell != null) { //按照儲存格資料類型進行資料處理 switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_STRING: value = cell.getStringCellValue(); break; case HSSFCell.CELL_TYPE_NUMERIC: if (HSSFDateUtil.isCellDateFormatted(cell)) { Date date = cell.getDateCellValue(); if (date != null) { value = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date); } else { value = ""; } } else { value = new DecimalFormat("0").format(cell.getNumericCellValue()); } break; case HSSFCell.CELL_TYPE_FORMULA: // 匯入時如果為公式產生的資料則無值 if (!cell.getStringCellValue().equals("")) { value = cell.getStringCellValue(); } else { value = cell.getNumericCellValue() + ""; } break; case HSSFCell.CELL_TYPE_BLANK: break; case HSSFCell.CELL_TYPE_ERROR: value = ""; break; case HSSFCell.CELL_TYPE_BOOLEAN: value = (cell.getBooleanCellValue() == true ? "Y" : "N"); break; default: value = ""; } } if (columnIndex == 0 && value.trim().equals("")) { break; } values[columnIndex] = rightTrim(value); hasValue = true; } if (hasValue) { result.add(values); } } } in.close(); String[][] returnArray = new String[result.size()][rowSize]; for (int i = 0; i < returnArray.length; i++) { returnArray[i] = (String[]) result.get(i); } return returnArray; }
上述檔案處理的過程能夠判斷儲存格的資料類型,判斷邏輯較為繁瑣,如果是自行定義模板檔案可以直接將檔案模板中的儲存格資料類型全部處理成文本類型;
這樣背景程式全部按照字串資料型別處理即可,相對容易,而且不易出錯。
3、容易出現的錯誤
對office2003和office2007的處理異常,異常如下
org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
解決方案是按照上面的
fileItem.getName().indexOf(".xlsx")檔案尾碼的判斷邏輯進行分類處理即可。
java-POI處理excel檔案方法