如何用Apache POI操作Excel文件-----如何在已有的Excel文件中插入一行新的数据?

摘要:
在POI的第一节入门中,我们提供了两个简单的例子,一个是如何用ApachePOI新建一个工作薄,另外一个例子是,如果用ApachePOI新建一个工作表。那么在这个章节里面,我将会给大家演示一下,如何用ApachePOI在已有的Excel文件中插入一行新的数据。

在POI的第一节入门中,我们提供了两个简单的例子,一个是如何用Apache POI新建一个工作薄,另外一个例子是,如果用Apache POI新建一个工作表。那么在这个章节里面,我将会给大家演示一下,如何用Apache POI在已有的Excel文件中插入一行新的数据。具体代码,请看下面的例子。

  1. importjava.io.File;
  2. importjava.io.FileInputStream;
  3. importjava.io.FileNotFoundException;
  4. importjava.io.FileOutputStream;
  5. importjava.io.IOException;
  6. importorg.apache.poi.xssf.usermodel.XSSFCell;
  7. importorg.apache.poi.xssf.usermodel.XSSFRow;
  8. importorg.apache.poi.xssf.usermodel.XSSFSheet;
  9. importorg.apache.poi.xssf.usermodel.XSSFWorkbook;
  10. publicclassCreatRowTest{
  11. //当前文件已经存在
  12. privateStringexcelPath="D:\exceltest\comments.xlsx";
  13. //从第几行插入进去
  14. privateintinsertStartPointer=3;
  15. //在当前工作薄的那个工作表单中插入这行数据
  16. privateStringsheetName="Sheet1";
  17. /**
  18. *总的入口方法
  19. */
  20. publicstaticvoidmain(String[]args){
  21. CreatRowTestcrt=newCreatRowTest();
  22. crt.insertRows();
  23. }
  24. /**
  25. *在已有的Excel文件中插入一行新的数据的入口方法
  26. */
  27. publicvoidinsertRows(){
  28. XSSFWorkbookwb=returnWorkBookGivenFileHandle();
  29. XSSFSheetsheet1=wb.getSheet(sheetName);
  30. XSSFRowrow=createRow(sheet1,insertStartPointer);
  31. createCell(row);
  32. saveExcel(wb);
  33. }
  34. /**
  35. *保存工作薄
  36. *@paramwb
  37. */
  38. privatevoidsaveExcel(XSSFWorkbookwb){
  39. FileOutputStreamfileOut;
  40. try{
  41. fileOut=newFileOutputStream(excelPath);
  42. wb.write(fileOut);
  43. fileOut.close();
  44. }catch(FileNotFoundExceptione){
  45. e.printStackTrace();
  46. }catch(IOExceptione){
  47. e.printStackTrace();
  48. }
  49. }
  50. /**
  51. *创建要出入的行中单元格
  52. *@paramrow
  53. *@return
  54. */
  55. privateXSSFCellcreateCell(XSSFRowrow){
  56. XSSFCellcell=row.createCell((short)0);
  57. cell.setCellValue(999999);
  58. row.createCell(1).setCellValue(1.2);
  59. row.createCell(2).setCellValue("Thisisastringcell");
  60. returncell;
  61. }
  62. /**
  63. *得到一个已有的工作薄的POI对象
  64. *@return
  65. */
  66. privateXSSFWorkbookreturnWorkBookGivenFileHandle(){
  67. XSSFWorkbookwb=null;
  68. FileInputStreamfis=null;
  69. Filef=newFile(excelPath);
  70. try{
  71. if(f!=null){
  72. fis=newFileInputStream(f);
  73. wb=newXSSFWorkbook(fis);
  74. }
  75. }catch(Exceptione){
  76. returnnull;
  77. }finally{
  78. if(fis!=null){
  79. try{
  80. fis.close();
  81. }catch(IOExceptione){
  82. e.printStackTrace();
  83. }
  84. }
  85. }
  86. returnwb;
  87. }
  88. /**
  89. *找到需要插入的行数,并新建一个POI的row对象
  90. *@paramsheet
  91. *@paramrowIndex
  92. *@return
  93. */
  94. privateXSSFRowcreateRow(XSSFSheetsheet,IntegerrowIndex){
  95. XSSFRowrow=null;
  96. if(sheet.getRow(rowIndex)!=null){
  97. intlastRowNo=sheet.getLastRowNum();
  98. sheet.shiftRows(rowIndex,lastRowNo,1);
  99. }
  100. row=sheet.createRow(rowIndex);
  101. returnrow;
  102. }
  103. }

免责声明:文章转载自《如何用Apache POI操作Excel文件-----如何在已有的Excel文件中插入一行新的数据?》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇VCSA6.7 升级6.7 U3commons-pool2 实现 sftp 连接池下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Apache设置反向代理

一、配置http反向代理地址 1、首先确保Apache有这些模块,在Apache根目录下确认有这些模块,主要包含以下模块: mod_proxy.so mod_proxy_ajp.so mod_proxy_balancer.so mod_proxy_connect.so mod_proxy_http.so  然后打开配置conf/httpd.conf文件,将...

Windows 下apache https配置(phpstudy)

1.首先获取证书,https://www.pianyissl.com/ 免费三个月的 或者 自己生成私钥、证书,然后应用到apache中。 http://blog.sina.com.cn/s/blog_58f71ef00102wvlx.html 讲解了如何在windows PHPStudy Apache 配置支持HTTPS http://www.cnbl...

zookeeper的一些异常总结

1.Could not find the main class: org.apache.zookeeper.server.quorum.QuorumPeerMain. Program will exit. 安装包损坏,找不到对应文件。重新安装。 2. 2013-03-28 11:13:32,205 - INFO [main:QuorumPeerConfig...

golang 写数据到excel文件 清明

package main import ( "encoding/csv" "fmt" "math/rand" "os" "strconv" "time" ) type person struct { JOB_NUMBER string NAME string DEP_CODE string DEP_NAME st...

解决 WP迁移后出现的404错误

项目迁移WordPress后仅首页正常,其它页面全部 404。时隔一年,再度遇到这问题,总结和梳理一下。 1、想办法登录后台,刷新一次“设置”中的“固定链接”。比如换成默认后保存,再设回原先设置并保存,理论上就可以解决绝大多数人的问题了。 2、检查Apache的 rewrite mod 是否开启。 LoadModule rewrite_module mo...

Windows下Apache的优化

(1)首选查看apache的工作模式windows下的查看apache的工作模式命令:httpd -l如果列出mod_win32.c,则表示是 win32.c 工作方式。 列出的全部内容如下所示:core.cmod_win32.cmpm_winnt.chttp_core.cmod_so.cmpm_winnt.c是专门针对Windows NT优化的MPM(多...