zoukankan      html  css  js  c++  java
  • Java导出数据为EXCEL的两种方式JXL和POI

    JXL和POI导出数据方式的比较

    POI支持excel2003和2007,而jxl只支持excel2003。

    下面为测试代码:

    Java代码  收藏代码
    1. public class TestCondition {  
    2.       
    3.     /** 
    4.      * 生成的记录条数 
    5.      */  
    6.     public static final int RECORD_COUNT = 21000;  
    7.       
    8.     /** 
    9.      * 模板文件 
    10.      */  
    11.     public static final String TEMPLATE_FILE = "E:/MyKernelPlatformWorkspace/Template/query_order.xls";  
    12.       
    13.     /** 
    14.      * JXL生成文件位置 
    15.      */  
    16.     public static final String JXL_TARGET_FILE_NAME = "E:/MyKernelPlatformWorkspace/Template/target/jxl_order.xls";  
    17.       
    18.     /** 
    19.      * POI生成文件位置 
    20.      */  
    21.     public static final String POI_TARGET_FILE_NAME = "E:/MyKernelPlatformWorkspace/Template/target/poi_order.xls";  
    22.       
    23.     /** 
    24.      * JXL临时文件位置 
    25.      */  
    26.     public static final String JXL_TEMP_DIR = "E:/MyKernelPlatformWorkspace/Template/temp";  
    27.       
    28. }  

    然后在此测试条件下编写JXL和POI的测试类,首先是JXL的

    Java代码  收藏代码
    1. public class JXLExcel {  
    2.       
    3.     /** 
    4.      * 起始行 
    5.      */  
    6.     private static final int start_row = 3;  
    7.       
    8.     private WorkbookSettings settings;  
    9.       
    10.     private File target;  
    11.       
    12.     public JXLExcel() {  
    13.         this.settings = new WorkbookSettings();  
    14.           
    15.         //设定JXL在生成excel文件时使用临时文件  
    16.         settings.setUseTemporaryFileDuringWrite(true);  
    17.         settings.setTemporaryFileDuringWriteDirectory(new File(TestCondition.JXL_TEMP_DIR));  
    18.           
    19.         this.target = new File(TestCondition.JXL_TARGET_FILE_NAME);  
    20.     }  
    21.       
    22.     public void execute() throws Exception {  
    23.   
    24.         // 读入模板文件  
    25.         Workbook template = Workbook.getWorkbook(new File(TestCondition.TEMPLATE_FILE));  
    26.           
    27.         WritableWorkbook worbook = Workbook.createWorkbook(target, template, settings);  
    28.           
    29.         // 获取第一个sheet  
    30.         WritableSheet sheet = worbook.getSheet(0);  
    31.           
    32.         Random random = new Random();  
    33.           
    34.         // 循环写入数据  
    35.         for(int i = 0;i < TestCondition.RECORD_COUNT;i++) {  
    36.             int row = i + start_row;  
    37.             sheet.insertRow(row);  
    38.               
    39.             Label col1 = new Label(0, row, String.valueOf(i + 1));  
    40.             Label col2 = new Label(1, row, String.valueOf(random.nextLong()));  
    41.             Label col3 = new Label(2, row, String.valueOf(random.nextLong()));  
    42.             Label col4 = new Label(3, row, "merchant" + (i +1));  
    43.             jxl.write.Number col5 = new Number(4, row, random.nextDouble());  
    44.             jxl.write.Number col6 = new Number(5, row, random.nextDouble());  
    45.             jxl.write.Number col7 = new Number(6, row, random.nextDouble());  
    46.             jxl.write.Number col8 = new Number(7, row, random.nextDouble());  
    47.             Label col9 = new Label(8, row, String.valueOf(random.nextLong()));  
    48.             Label col10 = new Label(9, row, "PAY");  
    49.             Label col11 = new Label(10, row, "POS");  
    50.             Label col12 = new Label(11, row, "2010-09-03 12:45:13");  
    51.             Label col13 = new Label(12, row, "2010-09-09 12:45:13");  
    52.             Label col14 = new Label(13, row, "interface" + (i + 1));  
    53.             Label col15 = new Label(14, row, "18701001830");  
    54.             Label col16 = new Label(15, row, "ccbc");  
    55.             Label col17 = new Label(16, row, String.valueOf(random.nextLong()));  
    56.             Label col18 = new Label(17, row, String.valueOf(random.nextLong()));  
    57.             jxl.write.Number col19 = new Number(18, row, random.nextDouble());  
    58.             jxl.write.Number col20 = new Number(19, row, random.nextDouble());  
    59.             Label col21 = new Label(20, row, "payer" + (i + 1));  
    60.             Label col22 = new Label(21, row, String.valueOf(random.nextLong()));  
    61.             Label col23 = new Label(22, row, "192.168.1.1");  
    62.             Label col24 = new Label(23, row, "192.168.1.1");  
    63.               
    64.             sheet.addCell(col1);  
    65.             sheet.addCell(col2);  
    66.             sheet.addCell(col3);  
    67.             sheet.addCell(col4);  
    68.             sheet.addCell(col5);  
    69.             sheet.addCell(col6);  
    70.             sheet.addCell(col7);  
    71.             sheet.addCell(col8);  
    72.             sheet.addCell(col9);  
    73.             sheet.addCell(col10);  
    74.             sheet.addCell(col11);  
    75.             sheet.addCell(col12);  
    76.             sheet.addCell(col13);  
    77.             sheet.addCell(col14);  
    78.             sheet.addCell(col15);  
    79.             sheet.addCell(col16);  
    80.             sheet.addCell(col17);  
    81.             sheet.addCell(col18);  
    82.             sheet.addCell(col19);  
    83.             sheet.addCell(col20);  
    84.             sheet.addCell(col21);  
    85.             sheet.addCell(col22);  
    86.             sheet.addCell(col23);  
    87.             sheet.addCell(col24);  
    88.         }  
    89.           
    90.         worbook.write();  
    91.         worbook.close();  
    92.     }  
    93.   
    94. }  

    执行Main函数

    Java代码  收藏代码
    1. public class JXLMain {  
    2.   
    3.     /**     
    4.      * 描述:     
    5.      * @param args     
    6.      * @throws Exception  
    7.      */  
    8.     public static void main(String[] args) throws Exception {  
    9.            
    10.         long jxlStart = System.currentTimeMillis();  
    11.         JXLExcel jxl = new JXLExcel();  
    12.         jxl.execute();  
    13.         long jxlStop = System.currentTimeMillis();  
    14.         System.out.println("jxl takes : " + (jxlStop - jxlStart)/1000 + " seconds.");  
    15.           
    16.     }  

    然后是POI的

    public class POIExcel {

    Java代码  收藏代码
    1. /** 
    2.  * 起始行 
    3.  */  
    4. private static final int start_row = 3;  
    5.   
    6. public void execute() throws Exception {  
    7.   
    8.     // 读入模板文件  
    9.     InputStream is = new FileInputStream(TestCondition.TEMPLATE_FILE);  
    10.     POIFSFileSystem poifsFileSystem = new POIFSFileSystem(is);  
    11.       
    12.     HSSFWorkbook workbook = new HSSFWorkbook(poifsFileSystem);  
    13.       
    14.     // 获取第一个sheet  
    15.     HSSFSheet sheet = workbook.getSheetAt(0);  
    16.   
    17.     Random random = new Random();  
    18.   
    19.     // 将模板的最后两行移动  
    20.     sheet.shiftRows(3, 4, TestCondition.RECORD_COUNT);  
    21.       
    22.     OutputStream os = new FileOutputStream(  
    23.             TestCondition.POI_TARGET_FILE_NAME);  
    24.   
    25.     // 循环写入数据  
    26.     for (int i = 0; i < TestCondition.RECORD_COUNT; i++) {  
    27.         int rowNum = i + start_row;  
    28.         HSSFRow row = sheet.createRow(rowNum);  
    29.   
    30.         HSSFCell cell1 = row.createCell(0);  
    31.         cell1.setCellValue(i + 1);  
    32.           
    33.         HSSFCell cell2 = row.createCell(1);  
    34.         cell2.setCellValue(String.valueOf(random.nextLong()));  
    35.           
    36.         HSSFCell cell3 = row.createCell(2);  
    37.         cell3.setCellValue(String.valueOf(random.nextLong()));  
    38.           
    39.         HSSFCell cell4 = row.createCell(3);  
    40.         cell4.setCellValue("merchant" + (i +1));  
    41.           
    42.         HSSFCell cell5 = row.createCell(4);  
    43.         cell5.setCellValue(random.nextDouble());  
    44.           
    45.         HSSFCell cell6 = row.createCell(5);  
    46.         cell6.setCellValue(random.nextDouble());  
    47.           
    48.         HSSFCell cell7 = row.createCell(6);  
    49.         cell7.setCellValue(random.nextDouble());  
    50.           
    51.         HSSFCell cell8 = row.createCell(7);  
    52.         cell8.setCellValue(random.nextDouble());  
    53.           
    54.         HSSFCell cell9 = row.createCell(8);  
    55.         cell9.setCellValue(String.valueOf(random.nextLong()));  
    56.           
    57.         HSSFCell cell10 = row.createCell(9);  
    58.         cell10.setCellValue("PAY");  
    59.           
    60.         HSSFCell cell11 = row.createCell(10);  
    61.         cell11.setCellValue("POS");  
    62.           
    63.         HSSFCell cell12 = row.createCell(11);  
    64.         cell12.setCellValue(new Date());  
    65.           
    66.         HSSFCell cell13 = row.createCell(12);  
    67.         cell13.setCellValue(new Date());  
    68.           
    69.         HSSFCell cell14 = row.createCell(13);  
    70.         cell14.setCellValue("interface" + (i + 1));  
    71.           
    72.         HSSFCell cell15 = row.createCell(14);  
    73.         cell15.setCellValue("18701001830");  
    74.           
    75.         HSSFCell cell16 = row.createCell(15);  
    76.         cell16.setCellValue("ccbc");  
    77.           
    78.         HSSFCell cell17 = row.createCell(16);  
    79.         cell17.setCellValue(String.valueOf(random.nextLong()));  
    80.           
    81.         HSSFCell cell18 = row.createCell(17);  
    82.         cell18.setCellValue(String.valueOf(random.nextLong()));  
    83.           
    84.         HSSFCell cell19 = row.createCell(18);  
    85.         cell19.setCellValue(random.nextDouble());  
    86.           
    87.         HSSFCell cell20 = row.createCell(19);  
    88.         cell20.setCellValue(random.nextDouble());  
    89.           
    90.         HSSFCell cell21 = row.createCell(20);  
    91.         cell21.setCellValue("payer" + (i + 1));  
    92.           
    93.         HSSFCell cell22 = row.createCell(21);  
    94.         cell22.setCellValue(String.valueOf(random.nextLong()));  
    95.           
    96.         HSSFCell cell23 = row.createCell(22);  
    97.         cell23.setCellValue("192.168.1.1");  
    98.           
    99.         HSSFCell cell24 = row.createCell(23);  
    100.         cell24.setCellValue("192.168.1.1");  
    101.           
    102.     }  
    103.     workbook.write(os);  
    104.     os.close();  
    105. }  

    执行Main函数

    Java代码  收藏代码
    1. public class POIMain {  
    2.   
    3.     /**     
    4.      * 描述:     
    5.      * @param args     
    6.      * @throws Exception  
    7.      */  
    8.     public static void main(String[] args) throws Exception {  
    9.           
    10.         long jxlStart = System.currentTimeMillis();  
    11.         POIExcel poi = new POIExcel();  
    12.         poi.execute();  
    13.         long jxlStop = System.currentTimeMillis();  
    14.         System.out.println("poi takes : " + (jxlStop - jxlStart)/1000 + " seconds.");  
    15.     }  
    16.   
    17. }  

    转:http://tianwenbo.iteye.com/blog/1485654

  • 相关阅读:
    Windows 2008 R2 远程桌面服务(八)远程桌面服务器安全设置
    从硬盘上安装SQLServer2005的问题
    在 Windows server 2008 下计划任务无法正常执行bat批处理文件
    两部搞定windows server 2008 R2 中IE8的增强安全配置功能
    Microsoft SQL Server 2005 Service Pack 4 RTM
    学习地址
    Windows 2008 远程桌面如何设置两个用户共享一个会话
    Windows Server 2008 启用无线网卡
    远程桌面连接指定会话(Session)
    固定宽度弹性布局(以适应各种各辨率)
  • 原文地址:https://www.cnblogs.com/xijin-wu/p/5628927.html
Copyright © 2011-2022 走看看