首页 > 代码库 > POI操作Excel

POI操作Excel

POI导出Excel2007比导出Excel2003要慢.
当时项目中用的JAR包:
  Excel2003:poi-2.5.1.jar
  Excel2007:
    poi-3.7-20101029.jar
    poi-ooxml-3.7-20101029.jar
    poi-ooxml-schemas-3.7-20101029.jar
    xmlbeans-2.3.0.jar
    dom4j-1.6.1.jar

import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;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.xssf.usermodel.XSSFRow;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook;public class PoiExcel {    // POI导出Excel2003    public static void exportExcel2003() {        try {            // 创建Excel文件            FileOutputStream fileOut = new FileOutputStream(                    "C:\\Users\\Yangyuan\\Desktop\\table.xls");            // 创建工作薄            HSSFWorkbook workbook = new HSSFWorkbook();            // 创建工作表            HSSFSheet sheet = workbook.createSheet();            // 设置工作表名称,参数1为工作表下标,参数2为工作表名称,参数3为编码            workbook.setSheetName(0, "第一页", HSSFWorkbook.ENCODING_UTF_16);            // 创建行            HSSFRow row = null;            // 循环创建单元格            for (int i = 0; i < 65536; i++) {                row = sheet.createRow(i);                for (int j = 0; j < 5; j++) {                    row.createCell((short) j).setCellValue(                            "Test_" + (j + 1) + "_" + (i + 1));                }            }            // 写入数据并关闭流            workbook.write(fileOut);            fileOut.flush();            fileOut.close();            System.out.println("导出Excel2003完成");        } catch (FileNotFoundException e) {            System.out.println(e);        } catch (IOException e) {            System.out.println(e);        }    }    // POI导出Excel2007    public static void exportExcel2007() {        try {            // 创建Excel文件            FileOutputStream fileOut = new FileOutputStream(                    "C:\\Users\\Yangyuan\\Desktop\\table.xlsx");            // 创建工作薄            XSSFWorkbook workbook = new XSSFWorkbook();            // 创建工作表            XSSFSheet sheet = workbook.createSheet("第一页");            // 创建行            XSSFRow row = null;            // 循环创建单元格            for (int i = 0; i < 75537; i++) {                row = sheet.createRow(i);                for (int j = 0; j < 5; j++) {                    row.createCell(j).setCellValue(                            "Test_" + (j + 1) + "_" + (i + 1));                }            }            // 写入数据并关闭流            workbook.write(fileOut);            fileOut.close();            fileOut.close();            System.out.println("导出Excel2007完成");        } catch (FileNotFoundException e) {            System.out.println(e);        } catch (IOException e) {            System.out.println(e);        }    }    public static void main(String[] args) {        exportExcel2003();        exportExcel2007();    }}

 

POI操作Excel