Apache-POI/ Java/ 写入Excel文件时跳过行

huangapple go评论96阅读模式
英文:

Apache-POI/ Java/ leaving out rows when writing to an Excel file

问题

这是我用中文翻译好的代码部分:

  1. try {
  2. boolean fileExists = new File("Stundenabrechnung" + test + ".xlsx").exists();
  3. XSSFWorkbook workbook;
  4. XSSFSheet sheet;
  5. if (fileExists) {
  6. // ... (之前的代码)
  7. } else {
  8. workbook = new XSSFWorkbook();
  9. sheet = workbook.createSheet("Stundenabrechnung");
  10. sheet.setDefaultColumnWidth(18);
  11. Map<String, Object[]> data = new TreeMap<>();
  12. data.put("1", new Object[]{"DATUM:", " INS. ABG. STUNDEN:", " ABGR. STUNDEN:", "ÜBERSTUNDEN:", " BESCHREIBUNG:"});
  13. data.put("2", new Object[]{datum, ergebnis + "0", LHabg.getText(), Lueber.getText(), taBes.getText()});
  14. Set<String> keyset = data.keySet();
  15. int rownum = 0;
  16. for (String key : keyset) {
  17. XSSFRow row = sheet.createRow(rownum++);
  18. Object[] objArr = data.get(key);
  19. int cellnum = 0;
  20. for (Object obj : objArr) {
  21. XSSFCell cell = row.createCell(cellnum++);
  22. CellStyle cellStyle = workbook.createCellStyle();
  23. cellStyle.setAlignment(HorizontalAlignment.CENTER);
  24. cellStyle.setVerticalAlignment(VerticalAlignment.TOP);
  25. cell.setCellStyle(cellStyle);
  26. if (obj instanceof String) {
  27. cell.setCellValue((String) obj);
  28. } else if (obj instanceof Integer) {
  29. cell.setCellValue((Integer) obj);
  30. }
  31. }
  32. }
  33. // ... (之后的代码)
  34. }
  35. System.out.println("ExcelFile is created successfully");
  36. } catch (Exception e) {
  37. e.printStackTrace();
  38. }
  39. XSSFRow rowTotal = sheet.createRow(24);
  40. XSSFCell totalText = rowTotal.createCell(2);
  41. totalText.setCellValue("Überstunden:");
  42. XSSFCell totalValue = rowTotal.createCell(3);
  43. totalValue.setCellFormula("SUM(D2:D20)");

希望这对你有所帮助!如果还有其他问题,请随时问我。

英文:

so I am trying to add some numbers together I wrote into an Excel file. At the end of the File I want to write the result, but my problem is, everytime I create the Excel file I can write two rows but the second one is getting skiped, so nothing is written into it. after the third row everything continues running perfectly fine. I tried to solve it myself but unfortunately I can‘t get any further.

This is my code for writing to the ExelFile:

  1. try {
  2. boolean fileExists = new File(&quot;Stundenabrechnung&quot; + test + &quot;.xlsx&quot;).exists();
  3. XSSFWorkbook workbook;
  4. XSSFSheet sheet;
  5. if (fileExists) {
  6. workbook = new XSSFWorkbook(new FileInputStream(new File(&quot;Stundenabrechnung&quot; + test + &quot;.xlsx&quot;)));
  7. sheet = workbook.getSheetAt(0);
  8. Map&lt;String, Object[]&gt; data = new TreeMap&lt;&gt;();
  9. data.put(&quot;3&quot;, new Object[]{datum, ergebnis + &quot;0&quot;, LHabg.getText(), &#252;ber, taBes.getText()});
  10. Set&lt;String&gt; keyset = data.keySet();
  11. int rownum = sheet.getPhysicalNumberOfRows();
  12. for (String key : keyset) {
  13. XSSFRow row = sheet.createRow(rownum++);
  14. Object[] objArr = data.get(key);
  15. int cellnum = 0;
  16. for (Object obj : objArr) {
  17. XSSFCell cell = row.createCell(cellnum++);
  18. CellStyle cellStyle = workbook.createCellStyle();
  19. cellStyle.setAlignment(HorizontalAlignment.CENTER);
  20. cellStyle.setVerticalAlignment(VerticalAlignment.TOP);
  21. cell.setCellStyle(cellStyle);
  22. if (obj instanceof String) {
  23. cell.setCellValue((String) obj);
  24. } else if (obj instanceof Integer) {
  25. cell.setCellValue((Integer) obj);
  26. }
  27. }
  28. }
  29. } else {
  30. workbook = new XSSFWorkbook();
  31. sheet = workbook.createSheet(&quot;Stundenabrechnung&quot;);
  32. sheet.setDefaultColumnWidth(18);
  33. Map&lt;String, Object[]&gt; data = new TreeMap&lt;&gt;();
  34. data.put(&quot;1&quot;, new Object[]{&quot;DATUM:&quot;, &quot; INS. ABG. STUNDEN:&quot;, &quot; ABGR. STUNDEN:&quot;, &quot;&#220;BERSTUNDEN:&quot;, &quot; BESCHREIBUNG:&quot;});
  35. data.put(&quot;2&quot;, new Object[]{datum, ergebnis + &quot;0&quot;, LHabg.getText(), Lueber.getText(), taBes.getText()});
  36. Set&lt;String&gt; keyset = data.keySet();
  37. int rownum = 0;
  38. for (String key : keyset) {
  39. XSSFRow row = sheet.createRow(rownum++);
  40. Object[] objArr = data.get(key);
  41. int cellnum = 0;
  42. for (Object obj : objArr) {
  43. XSSFCell cell = row.createCell(cellnum++);
  44. CellStyle cellStyle = workbook.createCellStyle();
  45. cellStyle.setAlignment(HorizontalAlignment.CENTER);
  46. cellStyle.setVerticalAlignment(VerticalAlignment.TOP);
  47. cell.setCellStyle(cellStyle);
  48. if (obj instanceof String) {
  49. cell.setCellValue((String) obj);
  50. } else if (obj instanceof Integer) {
  51. cell.setCellValue((Integer) obj);
  52. }
  53. }
  54. }
  55. FileOutputStream outputStream = new FileOutputStream(&quot;Stundenabrechnung&quot; + test + &quot;.xlsx&quot;);
  56. workbook.write(outputStream);
  57. workbook.close();
  58. } catch (Exception e) {
  59. e.printStackTrace();
  60. }
  61. System.out.println(&quot;ExcelFile is created succsessfully&quot;);

And this is the code for the result:

  1. XSSFRow rowTotal = sheet.createRow(24);
  2. XSSFCell totalText = rowTotal.createCell(2);
  3. totalText.setCellValue(&quot;&#220;berstunden:&quot;);
  4. XSSFCell totalValue = rowTotal.createCell(3);
  5. totalValue.setCellFormula(&quot;SUM(D2:D20)&quot;);

I tried putting the ResultCode into the if-else statment but it didn't work.

This is what the ExcelFile looks like:
Apache-POI/ Java/ 写入Excel文件时跳过行

I hope someone can help me because this is the last problem I have to solve. After this my program is finished.

thanks!!!

答案1

得分: 1

我认为你从第3行开始,请尝试将getLastRowNum()更改为getPhysicalNumberOfRows()。

英文:

i think you are starting with row number 3 try changing getLastRowNum() from getPhysicalNumberOfRows().

huangapple
  • 本文由 发表于 2020年8月17日 22:02:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63452469.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定