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

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

[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. //当前文件已经存在
  12. private String excelPath = "D:\\exceltest\\comments.xlsx";
  13. //从第几行插入进去
  14. private int insertStartPointer = 3;
  15. //在当前工作薄的那个工作表单中插入这行数据
  16. private String sheetName = "Sheet1";
  17. /**
  18. * 总的入口方法
  19. */
  20. public static void main(String[] args) {
  21. CreatRowTest crt = new CreatRowTest();
  22. crt.insertRows();
  23. }
  24. /**
  25. * 在已有的Excel文件中插入一行新的数据的入口方法
  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. * 保存工作薄
  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. * 创建要出入的行中单元格
  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. * 得到一个已有的工作薄的POI对象
  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. * 找到需要插入的行数,并新建一个POI的row对象
  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. }