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

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

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

问题

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

try {
    boolean fileExists = new File("Stundenabrechnung" + test + ".xlsx").exists();
    XSSFWorkbook workbook;
    XSSFSheet sheet;

    if (fileExists) {
        // ... (之前的代码)
    } else {
        workbook = new XSSFWorkbook();
        sheet = workbook.createSheet("Stundenabrechnung");
        sheet.setDefaultColumnWidth(18);

        Map<String, Object[]> data = new TreeMap<>();
        data.put("1", new Object[]{"DATUM:", " INS. ABG. STUNDEN:", " ABGR. STUNDEN:", "ÜBERSTUNDEN:", " BESCHREIBUNG:"});
        data.put("2", new Object[]{datum, ergebnis + "0", LHabg.getText(), Lueber.getText(), taBes.getText()});

        Set<String> keyset = data.keySet();
        int rownum = 0;
        for (String key : keyset) {
            XSSFRow row = sheet.createRow(rownum++);
            Object[] objArr = data.get(key);
            int cellnum = 0;
            for (Object obj : objArr) {
                XSSFCell cell = row.createCell(cellnum++);
                CellStyle cellStyle = workbook.createCellStyle();
                cellStyle.setAlignment(HorizontalAlignment.CENTER);
                cellStyle.setVerticalAlignment(VerticalAlignment.TOP);
                cell.setCellStyle(cellStyle);
                if (obj instanceof String) {
                    cell.setCellValue((String) obj);
                } else if (obj instanceof Integer) {
                    cell.setCellValue((Integer) obj);
                }
            }
        }

        // ... (之后的代码)
    }
    System.out.println("ExcelFile is created successfully");
} catch (Exception e) {
    e.printStackTrace();
}

XSSFRow rowTotal = sheet.createRow(24);
XSSFCell totalText = rowTotal.createCell(2);
totalText.setCellValue("Überstunden:");
XSSFCell totalValue = rowTotal.createCell(3);
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:

try {
boolean fileExists = new File(&quot;Stundenabrechnung&quot; + test + &quot;.xlsx&quot;).exists();
XSSFWorkbook workbook;
XSSFSheet sheet;
if (fileExists) {
workbook = new XSSFWorkbook(new FileInputStream(new File(&quot;Stundenabrechnung&quot; + test + &quot;.xlsx&quot;)));
sheet = workbook.getSheetAt(0);
Map&lt;String, Object[]&gt; data = new TreeMap&lt;&gt;();
data.put(&quot;3&quot;, new Object[]{datum, ergebnis + &quot;0&quot;, LHabg.getText(), &#252;ber, taBes.getText()});
Set&lt;String&gt; keyset = data.keySet();
int rownum = sheet.getPhysicalNumberOfRows();
for (String key : keyset) {
XSSFRow row = sheet.createRow(rownum++);
Object[] objArr = data.get(key);
int cellnum = 0;
for (Object obj : objArr) {
XSSFCell cell = row.createCell(cellnum++);
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.TOP);
cell.setCellStyle(cellStyle);
if (obj instanceof String) {
cell.setCellValue((String) obj);
} else if (obj instanceof Integer) {
cell.setCellValue((Integer) obj);
}
}
}
} else {
workbook = new XSSFWorkbook();
sheet = workbook.createSheet(&quot;Stundenabrechnung&quot;);
sheet.setDefaultColumnWidth(18);
Map&lt;String, Object[]&gt; data = new TreeMap&lt;&gt;();
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;});
data.put(&quot;2&quot;, new Object[]{datum, ergebnis + &quot;0&quot;, LHabg.getText(), Lueber.getText(), taBes.getText()});
Set&lt;String&gt; keyset = data.keySet();
int rownum = 0;
for (String key : keyset) {
XSSFRow row = sheet.createRow(rownum++);
Object[] objArr = data.get(key);
int cellnum = 0;
for (Object obj : objArr) {
XSSFCell cell = row.createCell(cellnum++);
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.TOP);
cell.setCellStyle(cellStyle);
if (obj instanceof String) {
cell.setCellValue((String) obj);
} else if (obj instanceof Integer) {
cell.setCellValue((Integer) obj);
}
}
}
FileOutputStream outputStream = new FileOutputStream(&quot;Stundenabrechnung&quot; + test + &quot;.xlsx&quot;);
workbook.write(outputStream);
workbook.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(&quot;ExcelFile is created succsessfully&quot;);

And this is the code for the result:

XSSFRow rowTotal = sheet.createRow(24);
XSSFCell totalText = rowTotal.createCell(2);
totalText.setCellValue(&quot;&#220;berstunden:&quot;);
XSSFCell totalValue = rowTotal.createCell(3);
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:

确定