使用jxl,poi方式建立/解析Excel檔案,jxlpoi
前幾天公司的網站有了新的需求,需要將資料一鍵匯出到Excel檔案,或直接將Excel中的資料直接插入到資料庫,查閱可一天的api文檔和百度.也算有了一點小經驗,分享出來給大家看看.有什麼錯誤可以評論指出.
本次實驗了jxl和POI兩種方式解析,感覺各有千秋.
實驗中使用的jar包以及工具包都在我的百度雲中,各位自行下載即可 點擊開啟連結
一、jxl
- Jxl是純javaAPI,在跨平台上表現的非常完美,代碼可以再windows或者Linux上運行而無需重新編寫
- 支援Excel 95-2000的所有版本(網上說目前可以支援Excel2007了,還沒有嘗試過)
- 產生Excel 2000標準格式
- 支援字型、數字、日期操作
- 能夠修飾儲存格屬性
- 支援映像和圖表,但是這套API對圖形和圖表的支援很有限,而且僅僅識別PNG格式。
缺點:效率低,圖片支援不完善,對格式的支援不如POI強大
二、POI
- 效率高,但編寫較複雜.
- 支援公式,宏
- 能夠修飾儲存格屬性
- 支援字型、數字、日期操作並一鍵轉換
jxl方式:
/** * jxl建立Excel活頁簿,並寫入資料 * @param fullPath */ public static void writeExcel(String fullPath) { //定義表頭標題 String[] labelTitle = new String[] { "ID", "姓名", "性別", "年齡" }; File file = new File(fullPath); //定義表格儲存格格式 WritableCellFormat cellFormat = new WritableCellFormat(); try { //橫向置中 cellFormat.setAlignment(Alignment.CENTRE); //豎向置中 cellFormat.setVerticalAlignment(VerticalAlignment.CENTRE); //建立工作表格簿執行個體 WritableWorkbook workbook = Workbook.createWorkbook(file); //建立sheet頁 參數:"sheet1"--sheet頁的名字,0--sheet的索引 WritableSheet sheet = workbook.createSheet("sheet1", 0); //文字物件 Label label = null; for (int j = 0; j < labelTitle.length; j++) { //參數--列,行,內容 label = new Label(j, 0, labelTitle[j], cellFormat); //添加儲存格 sheet.addCell(label); } for (int i = 1; i <= 10; i++) { label = new Label(0, i, "" + i, cellFormat); sheet.addCell(label); label = new Label(1, i, "李四", cellFormat); sheet.addCell(label); label = new Label(2, i, "男", cellFormat); sheet.addCell(label); label = new Label(3, i, "" + (i + 20), cellFormat); sheet.addCell(label); } //寫入活頁簿 workbook.write(); //關閉寫入流 workbook.close(); System.out.println("寫入完畢!"); } catch (Exception e) { e.printStackTrace(); } }
實驗結果:
解析Excel檔案
public static void readExcel(String filePath) throws BiffException, IOException { File file = new File(filePath); if (!file.exists()) { throw new FileNotFoundException("檔案未找到"); } //建立workbook活頁簿 Workbook workbook = Workbook.getWorkbook(file); //擷取第一張sheet頁 Sheet sheet = workbook.getSheet(0); /* * 迴圈遍曆每一個儲存格 * sheet.getRows()擷取sheet頁中的行數 * sheet.getColumns()擷取sheet頁中的列數 * sheet.getCell()擷取儲存格 * sheet.getContents()擷取儲存格內容 */ for (int i = 0; i < sheet.getRows(); i++) { for (int j = 0; j < sheet.getColumns(); j++) { //參數列表------------------列,行 Cell cell = sheet.getCell(j, i); System.out.print(cell.getContents() + ","); } System.out.println(); } workbook.close(); }
實驗結果:
POI方式:
/** * POI建立Excel活頁簿 * @param fullPath * @throws IOException */ public static void CreateExcel(String fullPath) throws IOException { String[] title = new String[] { "id", "name", "age" }; //建立Excel活頁簿 HSSFWorkbook workbook = new HSSFWorkbook(); //建立sheet頁 HSSFSheet sheet = workbook.createSheet(); //建立行,首行為0 HSSFRow row = sheet.createRow(0); HSSFCell cell = null; for (int i = 0; i < title.length; i++) { //建立儲存格 cell = row.createCell(i); //設定儲存格的值 cell.setCellValue(title[i]); } HSSFCell nextCell = null; for (int i = 1; i <= 10; i++) { HSSFRow nextRow = sheet.createRow(i); nextCell = nextRow.createCell(0); nextCell.setCellValue(i); nextCell = nextRow.createCell(1); nextCell.setCellValue("張三" + i); nextCell = nextRow.createCell(2); nextCell.setCellValue(30 + i); } //建立檔案 File file = new File(fullPath); //建立輸出資料流 FileOutputStream stream = FileUtils.openOutputStream(file); //寫入活頁簿 workbook.write(stream); //關流 stream.close(); workbook.close(); }
實驗結果:
/** * POI解析Excel活頁簿 * @param fullPath * @throws IOException */ public static void readExcel(String fullPath) throws IOException { File file = new File(fullPath); if (!file.exists()) { throw new FileNotFoundException("該檔案不存在"); } //擷取活頁簿 HSSFWorkbook workbook = new HSSFWorkbook(FileUtils.openInputStream(file)); //通過索引擷取sheet頁 HSSFSheet sheet = workbook.getSheetAt(0); //擷取最後的行數 int rowIndex = sheet.getLastRowNum(); HSSFRow row; short lastCellIndex; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd"); DecimalFormat df = new DecimalFormat("0"); for (int i = 0; i < rowIndex; i++) { row = sheet.getRow(i); //擷取最後的儲存格列數 lastCellIndex = row.getLastCellNum(); for (int j = 0; j < lastCellIndex; j++) { HSSFCell cell = row.getCell(j); switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_NUMERIC://數字 if (HSSFDateUtil.isCellDateFormatted(cell)) { Date date = cell.getDateCellValue(); if (date != null) { System.out.print(sdf.format(date)); } else { System.out.print(" "); } } else { System.out.print(df.format(cell.getNumericCellValue()) + " "); } break; case HSSFCell.CELL_TYPE_STRING://字串 System.out.print(cell.getStringCellValue() + " "); break; case HSSFCell.CELL_TYPE_BOOLEAN://布爾 System.out.print(cell.getBooleanCellValue() + " "); break; case HSSFCell.CELL_TYPE_BLANK://空值 System.out.print(" "); break; case HSSFCell.CELL_TYPE_FORMULA://公式 System.out.print(cell.getCellFormula() + " "); break; case HSSFCell.CELL_TYPE_ERROR://錯誤 System.out.print("非法字元 "); break; default: System.out.print("未知類型 "); } } System.out.println(); } }
實驗結果:
查看評論