How do I use Apache poi to manipulate Excel files-----how do i insert a new row of data into an existing Excel file?

Source: Internet
Author: User

In the first section of the poi, we provide two simple examples of how to create a new workbook with Apache poi, and another example is to create a new worksheet with Apache Poi. So in this section, i'll show you how to insert a new row of data into an existing Excel file using Apache Poi. For specific code, See the example BELOW.

[java]View PlainCopy
  1. Import java.io.File;
  2. Import java.io.FileInputStream;
  3. Import java.io.FileNotFoundException;
  4. Import java.io.FileOutputStream;
  5. Import java.io.IOException;
  6. Import org.apache.poi.xssf.usermodel.XSSFCell;
  7. Import org.apache.poi.xssf.usermodel.XSSFRow;
  8. Import org.apache.poi.xssf.usermodel.XSSFSheet;
  9. Import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  10. Public class Creatrowtest {
  11. //the current file already exists
  12. private String Excelpath = "d:\\exceltest\\comments.xlsx";
  13. //insert in from the first few lines
  14. private int insertstartpointer = 3;
  15. //insert This row of data in the work sheet of the current workbook
  16. private String sheetname = "Sheet1";
  17. /** 
  18. * Total Entrance method
  19. */
  20. public static void main (string[] Args) {
  21. Creatrowtest CRT = new Creatrowtest ();
  22. Crt.insertrows ();
  23. }
  24. /** 
  25. * Entry method for inserting a new row of data into an existing Excel file
  26. */
  27. public void insertrows () {
  28. Xssfworkbook WB = Returnworkbookgivenfilehandle ();
  29. Xssfsheet Sheet1 = Wb.getsheet (sheetname);
  30. Xssfrow row = CreateRow (sheet1, insertstartpointer);
  31. Createcell (row);
  32. Saveexcel (wb);
  33. }
  34. /** 
  35. * Save Work Book
  36. * @param WB
  37. */
  38. private void Saveexcel (xssfworkbook wb) {
  39. FileOutputStream fileout;
  40. try {
  41. Fileout = new FileOutputStream (excelpath);
  42. Wb.write (fileout);
  43. Fileout.close ();
  44. } catch (filenotfoundexception e) {
  45. E.printstacktrace ();
  46. } catch (ioexception e) {
  47. E.printstacktrace ();
  48. }
  49. }
  50. /** 
  51. * Create rows in the row to enter or leave cells
  52. * @param row
  53. * @return
  54. */
  55. private Xssfcell Createcell (xssfrow Row) {
  56. Xssfcell cell = Row.createcell ((short) 0);
  57. Cell.setcellvalue (999999);
  58. Row.createcell (1). setcellvalue (1.2);
  59. Row.createcell (2). setcellvalue ("this is a string cell");
  60. return cell;
  61. }
  62. /** 
  63. * Get a POI object for an existing workbook
  64. * @return
  65. */
  66. Private Xssfworkbook returnworkbookgivenfilehandle () {
  67. Xssfworkbook WB = null;
  68. FileInputStream FIS = null;
  69. File F = new file (excelpath);
  70. try {
  71. if (f! = Null) {
  72. FIS = new FileInputStream (f);
  73. WB = New Xssfworkbook (fis);
  74. }
  75. } catch (Exception e) {
  76. return null;
  77. } finally {
  78. if (fis! = Null) {
  79. try {
  80. Fis.close ();
  81. } catch (ioexception e) {
  82. E.printstacktrace ();
  83. }
  84. }
  85. }
  86. return wb;
  87. }
  88. /** 
  89. * Find the number of rows to insert and create a new POI Row object
  90. * @param sheet
  91. * @param rowIndex
  92. * @return
  93. */
  94. private Xssfrow createrow (xssfsheet sheet, Integer RowIndex) {
  95. Xssfrow row = null;
  96. if (sheet.getrow (rowIndex) = Null) {
  97. int lastrowno = Sheet.getlastrownum ();
  98. Sheet.shiftrows (rowIndex, lastrowno, 1);
  99. }
  100. row = Sheet.createrow (rowIndex);
  101. return row;
  102. }
  103. }

How do I use Apache poi to manipulate Excel files-----how do i insert a new row of data into an existing Excel file?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.