[英]Writing excel sheet with LinkedHashmap using Java POI
I am almost there to solve my problem, but just got stuck in small issue. 我几乎可以在那里解决我的问题,但是只是遇到了小问题。 I am trying to print a excel file from LinkedHashmap
. 我正在尝试从LinkedHashmap
打印一个excel文件。 I use Java and POI library for my purpose. 我出于目的使用Java和POI库。
I am extracting some data from another excel file in LinkedHashmap and printing this data to new excel file. 我正在从LinkedHashmap中的另一个excel文件中提取一些数据,并将这些数据打印到新的excel文件中。
Data stored in LinkedHashmap
is large and listed in following manner: 存储在LinkedHashmap
数据很大,并按以下方式列出:
Keys | Values
-----------------------------------
Key1 : value1, value1, value1
Key2 : value2, value2, value2
key3 : value3, value3, value3
What I want: 我想要的是:
Output in excel file should be displayed in following format: Excel文件中的输出应以以下格式显示:
Excel_file_out_deserved.xslx:
+
key1 | value1
key1 | value1
key1 | value1
| // (if possible, empty cell space here will look good)
key2 | value2
key2 | value2
key2 | value2
| // (if possible, empty cell space here will look good)
key3 | value3
key3 | value3
key3 | value3
What I am getting: 我得到的是:
My_excel_file_out.xslx:
| value1 // (why my first column is empty?!, it should print keys there)
| value1
| value1
| value2
| value2
| value2
| value3
| value3
| value3
What I tried: 我试过的
FileOutputStream out = new FileOutputStream(new File("my_excel.xlsx"));
XSSFWorkbook newWorkbook = new XSSFWorkbook();
XSSFSheet sheet = newWorkbook.createSheet("print_vertical");
int row = 0;
// loop through all the keys
for(String key : linkMap.keySet()){
List<String> values = linkMap.get(key);
for (String value:values){
// print keys in 1st column
sheet.createRow(row).createCell(0).setCellValue(key); // why this doesn't work? :-/
// print values in 3rd column
sheet.createRow(row).createCell(2).setCellValue(value); // this works fine
row++;
}
}
newWorkbook.write(out);
out.close();
Mysteriously, when I remove inner for-loop of values without omitting sheet.createRow(row).createCell(0).setCellValue(key);
神秘地,当我删除值的内部for-loop而没有忽略sheet.createRow(row).createCell(0).setCellValue(key);
line, the outer loop will correctly print key values in first column row wise. 行,则外循环将在第一列的行中正确打印键值。 I cannot figure out where I am making mistake. 我不知道自己在哪里犯错。 Thanks. 谢谢。
you create your row twice by calling 您通过调用两次来创建行
sheet.createRow(row)
You will want to store the row in a variable and calling the createCell(x)
there 您将需要将行存储在变量中,然后在其中调用createCell(x)
for (String value:values){
Row newRow = sheet.creatRow(row);
newRow.createCell(0).setCellValue(key);
newRow.createCell(2).setCellValue(value);
row++;
}
Problem was: 问题是:
sheet.createRow(row) .createCell(0).setCellValue(key); sheet.createRow(row) .createCell(0).setCellValue(key); // create new row at index 'row' sheet.createRow(row) .createCell(2).setCellValue(value); //在索引“行” 表上创建新行。createRow(row) .createCell(2).setCellValue(value); // create new row at same row index //在同一行索引处创建新行
so your first row will be overridden. 因此您的第一行将被覆盖。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.