按列写入数据apache poi

问题描述 投票:0回答:1

我很挣扎,不知道如何解决这个问题。我的问题是我正在迭代文件夹中的大量文件。对于每个文件,我将数据放入特定于列的列表中。我想要的是使用 Apache POI 在 Excel 中的输出,但我可以在按行打印但不能按列打印时执行此操作。有人可以帮忙吗?基本上我希望为与其关联的每个相应列表打印文件名。

输出:

FileName,ObjectData,FieldData,AppData
Foo,"Obj1","F1","App1"
Foo,"Obj2","","App2"
Foo,"","","App3"
Foo,"","","App4"
Foo,"","","App5"
Test,"","F1","App1"
Test,"","F2",""
Test,"","F3",""
Test,"","F4",""
package cruft;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Main {
    private static String outPath = "path";

    public static void main(String[] args) throws Exception{
        SXSSFWorkbook wb = new SXSSFWorkbook();
        Sheet sheet = wb.createSheet("Analysis.xlsx");
        List<String> fileList = Arrays.asList("Foo","Test");
        List<String> fooObjectData = Arrays.asList("Obj1","Obj2");
        List<String> fooFieldData = Arrays.asList("F1");
        List<String> fooAppData = Arrays.asList("App1","App2","App3","App4","App5");
        
        List<String> testObjectData = Arrays.asList("");
        List<String> testFieldData = Arrays.asList("F1","F2","F3","F4","F5");
        List<String> testAppData = Arrays.asList("App1");
        for(String fileName: fileList) {
            Row row = sheet.createRow(0);
            for (int i = 0; i < 3; i++) {
                Cell cell = row.createCell(i);
                if(fileName.equals("Foo")) {
                    for(String s : fooObjectData) {
                        cell.setCellValue(s);
                    }
                    for(String s : fooFieldData) {
                        cell.setCellValue(s);
                    }
                    for(String s : fooAppData) {
                        cell.setCellValue(s);
                    }
                }
                else {
                    for(String s : testObjectData) {
                        cell.setCellValue(s);
                    }
                    for(String s : testFieldData) {
                        cell.setCellValue(s);
                    }
                    for(String s : testAppData) {
                        cell.setCellValue(s);
                    }
                }

            }
        }
        writeToFile(wb);
    }
    
    private static void writeToFile(SXSSFWorkbook wb) throws IOException {
        File f = new File(outPath);
        if (!f.exists()) {
            f.createNewFile();
        }
        FileOutputStream out = new FileOutputStream("foo.xlsx");
        wb.write(out);
        out.close();

        //After everything is written, then we dispose the temp file.
        wb.dispose();
    }
    
   
}

当我运行上面的代码时,它只打印 应用1 应用1 应用1

我尝试运行上面的命令,但得到以下输出 应用1 应用1 应用1

java apache java-8 apache-poi
1个回答
0
投票

给你。


public class Main {
    private static String outPath = "path";

    public static void main(String[] args) throws Exception{
        SXSSFWorkbook wb = new SXSSFWorkbook();
        Sheet sheet = wb.createSheet("Analysis.xlsx");
        List<String> fooObjectData = Arrays.asList("Obj1","Obj2");
        List<String> fooFieldData = Arrays.asList("F1");
        List<String> fooAppData = Arrays.asList("App1","App2","App3","App4","App5");
        int maxFooRowCount = max(fooObjectData.size(), fooFieldData.size(), fooAppData.size());
        
        List<String> testObjectData = Arrays.asList("");
        List<String> testFieldData = Arrays.asList("F1","F2","F3","F4","F5");
        List<String> testAppData = Arrays.asList("App1");
        int maxTestRowCount = max(testObjectData.size(), testFieldData.size(), testAppData.size());
        
        for(int i=0;i<maxFooRowCount;i++) {
            Row row = sheet.createRow(i);
            row.createCell(0).setCellValue("Foo");
            row.createCell(1).setCellValue(getOrBlank(fooObjectData, i));
            row.createCell(2).setCellValue(getOrBlank(fooFieldData, i));
            row.createCell(3).setCellValue(getOrBlank(fooAppData, i));
        }
        

        for(int i=0;i<maxTestRowCount;i++) {
            Row row = sheet.createRow(i+maxFooRowCount);
            row.createCell(0).setCellValue("Test");
            row.createCell(1).setCellValue(getOrBlank(testObjectData, i));
            row.createCell(2).setCellValue(getOrBlank(testFieldData, i));
            row.createCell(3).setCellValue(getOrBlank(testAppData, i));
        }
        writeToFile(wb);
    }
    
    private static String getOrBlank(List<String> list, int index) {
        if(list.size()<=index) return "";
        return list.get(index);
    }

    private static int max(int size, int... others) {
        if(others==null) return size;
        for(int c: others) if(size<c) size = c;
        return size;
    }

    private static void writeToFile(SXSSFWorkbook wb) throws IOException {
        File f = new File(outPath);
        if (!f.exists()) {
            f.createNewFile();
        }
        FileOutputStream out = new FileOutputStream("foo.xlsx");
        wb.write(out);
        out.close();

        //After everything is written, then we dispose the temp file.
        wb.dispose();
    }
    
   
}
© www.soinside.com 2019 - 2024. All rights reserved.