Java poi读取,写入Excel2003
相关阅读:
poi读写Excel2007:http://www.cnblogs.com/gavinYang/p/3576741.html
jxl读写excel2003/2007:http://www.cnblogs.com/gavinYang/p/3576819.html
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{//读取xlsMap<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]);elseSystem.out.print(arr[j]+"|");}System.out.println();}}//写入xlswriteXls("E:/测试写入XLS.xls",map);}//读取xlspublic 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);// 循环行Rowfor (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;} //写入xlspublic 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();}}}