Java poi讀取,寫入Excel2003html
相關閱讀:
poi讀寫Excel2007:http://www.cnblogs.com/gavinYang/p/3576741.html
jxl讀寫excel2003/2007:http://www.cnblogs.com/gavinYang/p/3576819.html
java
package com.gavin.operational.excle; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.Row; public class ReadExcelXls { public static void main(String[] args) throws Exception{ //讀取xls Map<Integer, List<String[]>> map = readXls("E:/測試讀取XLS.xls"); for(int n=0;n<map.size();n++){ List<String[]> list = map.get(n); System.out.println("-------------------------sheet"+n+"--------------------------------"); for(int i=0;i<list.size();i++){ String[] arr = (String[]) list.get(i); for(int j=0;j<arr.length;j++){ if(j==arr.length-1) System.out.print(arr[j]); else System.out.print(arr[j]+"|"); } System.out.println(); } } //寫入xls writeXls("E:/測試寫入XLS.xls",map); } //讀取xls public static Map<Integer, List<String[]>> readXls(String fileName) { Map<Integer, List<String[]>> map = new HashMap<Integer, List<String[]>>(); try { InputStream is = new FileInputStream(fileName); HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is); // 循環工做表Sheet for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) { HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet); if (hssfSheet == null) { continue; } List<String[]> list = new ArrayList<String[]>(); //總行數:hssfSheet.getLastRowNum()-hssfSheet.getFirstRowNum()+1); // 循環行Row for (int rowNum = 0; rowNum <= hssfSheet.getLastRowNum(); rowNum++) { HSSFRow hssfRow = hssfSheet.getRow(rowNum); if (hssfRow == null) { continue; } String[] singleRow = new String[hssfRow.getLastCellNum()]; for(int column=0;column<hssfRow.getLastCellNum();column++){ Cell cell = hssfRow.getCell(column,Row.CREATE_NULL_AS_BLANK); switch(cell.getCellType()){ case Cell.CELL_TYPE_BLANK: singleRow[column] = ""; break; case Cell.CELL_TYPE_BOOLEAN: singleRow[column] = Boolean.toString(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_ERROR: singleRow[column] = ""; break; case Cell.CELL_TYPE_FORMULA: cell.setCellType(Cell.CELL_TYPE_STRING); singleRow[column] = cell.getStringCellValue(); if (singleRow[column] != null) { singleRow[column] = singleRow[column].replaceAll("#N/A", "").trim(); } break; case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { singleRow[column] = String.valueOf(cell.getDateCellValue()); } else { cell.setCellType(Cell.CELL_TYPE_STRING); String temp = cell.getStringCellValue(); // 判斷是否包含小數點,若是不含小數點,則以字符串讀取,若是含小數點,則轉換爲Double類型的字符串 if (temp.indexOf(".") > -1) { singleRow[column] = String.valueOf(new Double(temp)).trim(); } else { singleRow[column] = temp.trim(); } } break; case Cell.CELL_TYPE_STRING: singleRow[column] = cell.getStringCellValue().trim(); break; default: singleRow[column] = ""; break; } } list.add(singleRow); } map.put(numSheet, list); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return map; } //寫入xls public static void writeXls(String fileName,Map<Integer,List<String[]>> map) { try { HSSFWorkbook wb = new HSSFWorkbook(); for(int sheetnum=0;sheetnum<map.size();sheetnum++){ HSSFSheet sheet = wb.createSheet(""+sheetnum); List<String[]> list = map.get(sheetnum); for(int i=0;i<list.size();i++){ HSSFRow row = sheet.createRow(i); String[] str = list.get(i); for(int j=0;j<str.length;j++){ HSSFCell cell = row.createCell(j); cell.setCellValue(str[j]); } } } FileOutputStream outputStream = new FileOutputStream(fileName); wb.write(outputStream); outputStream.close(); } catch (FileNotFoundException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } catch (IOException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } } }