I am struggling a lot and I am not sure how to solve the problem. My issue is that I am iterating lots of files from a folder. For each file, I am putting data in column specific list. What I want is below output in the Excel using Apache POI but I am able to do it when printing by row but not by column. Can someone help ? Basically I want the fileName to be printed for every corresponding list associated to it.
Output:
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();
}
}
When i run the above, it only prints App1 App1 App1
I tried running the above command but get below output App1 App1 App1
Here you go.
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();
}
}