java操作Excel--jxl与poj的比较

本文介绍使用Java JXL库和Apache POI库进行Excel文件读写的方法。包括创建Excel文件、设置单元格内容及从Excel中读取数据的过程,并对比了两种库在处理大量数据时的效率。
[java]  view plain  copy
 print ?
  1. package com.nexusy.excel.jxl;  
  2. import java.io.File;  
  3. import java.io.IOException;  
  4. import java.util.List;  
  5. import jxl.Cell;  
  6. import jxl.CellType;  
  7. import jxl.NumberCell;  
  8. import jxl.Sheet;  
  9. import jxl.Workbook;  
  10. import jxl.read.biff.BiffException;  
  11. import jxl.write.Label;  
  12. import jxl.write.Number;  
  13. import jxl.write.WritableSheet;  
  14. import jxl.write.WritableWorkbook;  
  15. import jxl.write.WriteException;  
  16. import jxl.write.biff.RowsExceededException;  
  17. import com.nexusy.excel.Record;  
  18. import com.nexusy.excel.TestUtil;  
  19. public class JXLTestMain {  
  20.       
  21.     private final static String filename = "jxltest.xls";  
  22.     private final static String[] headers = {"ID""标题""价格""数量""描述"};  
  23.       
  24.     private final static int rows = 65535;  
  25.     public static void main(String[] args) {  
  26.         writeExcel();  
  27.         readExcel();  
  28.     }  
  29.       
  30.     public static void writeExcel() {  
  31.         try {  
  32.             Thread.sleep(1000*20);  
  33.         } catch (InterruptedException e1) {  
  34.             e1.printStackTrace();  
  35.         }  
  36.         try {  
  37.             WritableWorkbook workbook = Workbook.createWorkbook(new File(filename));  
  38.               
  39.             WritableSheet  sheet = workbook.createSheet("jxl测试"0);  
  40.               
  41.             for (int i = 0; i < headers.length; i++) {  
  42.                 Label label = new Label(i, 0 , headers[i]);  
  43.                 sheet.addCell(label);  
  44.             }  
  45.               
  46.             List<Record> records = TestUtil.getRecords(rows);  
  47.             long s1 = System.nanoTime();  
  48.             int c = 1;  
  49.             for (Record record : records) {  
  50.                 sheet.addCell(new Number(0, c, record.getId()));  
  51.                 sheet.addCell(new Label(1, c, record.getTitle()));  
  52.                 sheet.addCell(new Number(2, c, record.getPrice()));  
  53.                 sheet.addCell(new Number(3, c, record.getQuantity()));  
  54.                 sheet.addCell(new Label(4, c, record.getDesc()));  
  55.                 c++;  
  56.             }  
  57.               
  58.             workbook.write();  
  59.             workbook.close();  
  60.             long s2 = System.nanoTime();  
  61.             System.out.println("jxl write " + rows + " rows to excel:" + (s2-s1));  
  62.         } catch (IOException e) {  
  63.             e.printStackTrace();  
  64.         } catch (RowsExceededException e) {  
  65.             e.printStackTrace();  
  66.         } catch (WriteException e) {  
  67.             e.printStackTrace();  
  68.         }  
  69.     }  
  70.     public static void readExcel() {  
  71.         try {  
  72.             Thread.sleep(1000*20);  
  73.         } catch (InterruptedException e1) {  
  74.             e1.printStackTrace();  
  75.         }  
  76.         try {  
  77.             long s1 = System.nanoTime();  
  78.             Workbook workbook = Workbook.getWorkbook(new File(filename));  
  79.             Sheet sheet = workbook.getSheet(0);  
  80.             System.out.println(sheet.getName());  
  81.             for(int i = 0; i < sheet.getRows(); i++){  
  82.                 Cell[] cells = sheet.getRow(i);  
  83.                 for (Cell cell : cells) {  
  84.                     if(cell.getType() == CellType.NUMBER){  
  85.                         System.out.print(((NumberCell)cell).getValue()+"  ");  
  86.                     } else if(cell.getType() == CellType.LABEL){  
  87.                         System.out.print(cell.getContents()+"  ");  
  88.                     }  
  89.                 }  
  90.                 System.out.println();  
  91.             }  
  92.             workbook.close();  
  93.             long s2 = System.nanoTime();  
  94.             System.out.println("jxl read " + rows + " rows from excel:" + (s2-s1));  
  95.         } catch (BiffException e) {  
  96.             e.printStackTrace();  
  97.         } catch (IOException e) {  
  98.             e.printStackTrace();  
  99.         }  
  100.     }  
  101. }  
  102.    
  103. package com.nexusy.excel.poi;  
  104. import java.io.FileInputStream;  
  105. import java.io.FileNotFoundException;  
  106. import java.io.FileOutputStream;  
  107. import java.io.IOException;  
  108. import java.io.InputStream;  
  109. import java.util.List;  
  110. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  111. import org.apache.poi.ss.usermodel.Cell;  
  112. import org.apache.poi.ss.usermodel.Row;  
  113. import org.apache.poi.ss.usermodel.Sheet;  
  114. import org.apache.poi.ss.usermodel.Workbook;  
  115. import com.nexusy.excel.Record;  
  116. import com.nexusy.excel.TestUtil;  
  117. public class POITestMain {  
  118.       
  119.     private final static String filename = "poitest.xls";  
  120.     private final static String[] headers = {"ID""标题""价格""数量""描述"};  
  121.       
  122.     private final static int rows = 65535;  
  123.     public static void main(String[] args) {  
  124.         writeExcel();  
  125.         readExcel();  
  126.     }  
  127.     public static void writeExcel() {  
  128.         try {  
  129.             Thread.sleep(1000*20);  
  130.         } catch (InterruptedException e1) {  
  131.             e1.printStackTrace();  
  132.         }  
  133.         Workbook wb = new HSSFWorkbook();  
  134.         try {  
  135.             FileOutputStream fileOut = new FileOutputStream(filename);  
  136.               
  137.             Sheet sheet = wb.createSheet("poi测试");  
  138.             Row row = sheet.createRow(0);  
  139.             for (int i = 0; i < headers.length; i++) {  
  140.                 row.createCell(i).setCellValue(headers[i]);  
  141.             }  
  142.               
  143.             List<Record> records = TestUtil.getRecords(rows);  
  144.             long s1 = System.nanoTime();  
  145.             int r = 1;  
  146.             for (Record record : records) {  
  147.                 row = sheet.createRow(r);  
  148.                 row.createCell(0).setCellValue(record.getId());  
  149.                 row.createCell(1).setCellValue(record.getTitle());  
  150.                 row.createCell(2).setCellValue(record.getPrice());  
  151.                 row.createCell(3).setCellValue(record.getQuantity());  
  152.                 row.createCell(4).setCellValue(record.getDesc());  
  153.                 r++;  
  154.             }  
  155.               
  156.             wb.write(fileOut);  
  157.             fileOut.close();  
  158.             long s2 = System.nanoTime();  
  159.             System.out.println("poi write " + rows + " rows to excel:" + (s2-s1));  
  160.         } catch (FileNotFoundException e) {  
  161.             e.printStackTrace();  
  162.         } catch (IOException e) {  
  163.             e.printStackTrace();  
  164.         }  
  165.     }  
  166.     public static void readExcel() {  
  167.         try {  
  168.             Thread.sleep(1000*20);  
  169.         } catch (InterruptedException e1) {  
  170.             e1.printStackTrace();  
  171.         }  
  172.         try {  
  173.             long s1 = System.nanoTime();  
  174.             InputStream inp = new FileInputStream(filename);  
  175.             Workbook wb = new HSSFWorkbook(inp);  
  176.             Sheet sheet = wb.getSheetAt(0);  
  177.             System.out.println(sheet.getSheetName());  
  178.               
  179.             for(Row row : sheet){  
  180.                 for(Cell cell : row){  
  181.                     switch (cell.getCellType()) {  
  182.                     case Cell.CELL_TYPE_NUMERIC:  
  183.                         System.out.print(cell.getNumericCellValue() + "  ");  
  184.                         break;  
  185.                     case Cell.CELL_TYPE_STRING:  
  186.                         System.out.print(cell.getStringCellValue() + "  ");  
  187.                         break;  
  188.                     default:  
  189.                         break;  
  190.                     }  
  191.                 }  
  192.                 System.out.println();  
  193.             }  
  194.             inp.close();  
  195.             long s2 = System.nanoTime();  
  196.             System.out.println("poi read " + rows + " rows from excel:" + (s2-s1));  
  197.         } catch (FileNotFoundException e) {  
  198.             e.printStackTrace();  
  199.         } catch (IOException e) {  
  200.             e.printStackTrace();  
  201.         }  
  202.   
  203.     }  
  204. }  
内容概要:本文提出了一种结合在线鲁棒主成分分析(RPCA)模型长短期记忆(LSTM)循环网络的商品需求预测方法,并提供了完整的Python代码实现。该方法首先利用RPCA模型对原始商品需求时间序列进行分解,分离出低秩的潜在趋势成分稀疏的异常波动成分,有效实现数据去噪异常值修正,提升输入数据的鲁棒性;随后将净化后的数据输入LSTM网络,充分挖掘时间序列中的长期依赖关系时序模式,从而提高对未来需求的预测精度。整个模型设计针对实际商业场景中普遍存在的数据噪声大、波动剧烈、突发性事件干扰等问题,展现出较强的稳定性预测能力。文中通过实验验证了该混合模型在多个指标上优于传统统计模型及单一LSTM模型,体现了其在复杂环境下的优越性能。; 适合人群:具备一定Python编程能力和机器学习基础知识,从事数据分析、供应链管理、电商运营、零售优化及相关领域研究的研发人员或研究生;特别适合关注时间序列预测、深度学习建模以及鲁棒数据处理技术的技术人员。; 使用场景及目标:①应用于电商平台、零售企业或制造行业中的销量预测,以支持库存优化、生产计划制定物流调度决策;②为科研工作者提供一种融合鲁棒统计深度学习的预测建模范例,推动高噪声环境下预测算法的创新复现研究;③帮助开发者深入理解RPCALSTM的集成机制,掌握复杂预测模型的构建、训练调优流程。; 阅读建议:建议读者结合所提供的Python代码逐步实现模型,重点理解RPCA在数据预处理阶段的作用机制以及LSTM网络的结构设计超参数配置。学习过程中应在真实或模拟数据集上复现实验结果,对比不同参数设置下的模型表现,以深化对模型内在工作原理的理解。同时可进一步探索其他深度学习模型(如GRU、Transformer)鲁棒分解方法(如VMD、STL)的融合可能性,拓展应用场景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值