java - 如何使用java创建可追加的excelsheet

标签 java excel apache-poi jxl

我想创建一个可追加的 Excel 工作表。 就像我有四列 stream1 stream2 stream3 stream4

我第一次只在第一列(stream1)插入数据 之后我想一一填满其他列。

这是我正在使用的代码:

 public void createFile(Jqueue stream1, Jqueue stream2, Jqueue stream3, Jqueue stream4) {
    try {

        String filename = "path";
        boolean alreadyExists = (new File(filename)).exists();

        HSSFRow rowhead = sheet1.createRow((short) 0);
        rowhead.createCell((short) 0).setCellValue("Stream 1");
        rowhead.createCell((short) 1).setCellValue("Stream 2");
        rowhead.createCell((short) 2).setCellValue("Stream 3");
        rowhead.createCell((short) 3).setCellValue("Stream 4");


        int i = 1;
        while (!stream1.isEmpty()) {
            String urlstream1 = "";
            String urlstream2 = "";
            String urlstream3 = "";
            String urlstream4 = "";
            HSSFRow row = sheet1.createRow((short) i);
            try {
                if (stream1.size() > 0) {
                    urlstream1 = stream1.dequeue().toString();
                }
            } catch (Exception ex) {
            }
            try {
                if (stream2.size() > 0) {
                    urlstream2 = stream2.dequeue().toString();
                }
            } catch (Exception ex) {
            }
            try {
                if (stream3.size() > 0) {
                    urlstream3 = stream3.dequeue().toString();
                }
            } catch (Exception ex) {
            }
            try {
                if (stream4.size() > 0) {
                    urlstream4 = stream4.dequeue().toString();
                }
            } catch (Exception ex) {
            }
            row.createCell((short) 0).setCellValue(urlstream1);
            row.createCell((short) 1).setCellValue(urlstream2);
            row.createCell((short) 2).setCellValue(urlstream3);
            row.createCell((short) 3).setCellValue(urlstream4);
            i++;
        }

        FileOutputStream fileOut = new FileOutputStream(filename);
        hwb.write(fileOut);
        fileOut.close();

但这不是可追加的代码。它逐行插入数据。

谢谢。

最佳答案

我想您的代码稍作改动就可以做到这一点。

尝试替换

int i = 1;

具有以下内容:

int i = sheet1.getLastRowNum() + 1;

您还需要稍微更改读取和写入文件的实现。

关于java - 如何使用java创建可追加的excelsheet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11272221/

相关文章:

java - 确定 JButton 是否已被按下

Excel VBA - 如何找到字典中最小和最大的键

c# - 如何使用 C# 在 Excel 中更改系列颜色?

vba - Excel 2013 VBA 清除事件过滤器

java - 在流管道中保存到数据库

swing - 使用 AspectJ 加载 javax.swing.* 类

java - 分配日历值的问题

java - 处理大型 xlsx 文件

java - NoClassDefFoundError : UnsupportedFileFormatException while using apache poi to write to an excel file

java - 我们如何从 Apache POI 中的 Excel 读取默认单元格格式?