[英]Not able to write data in xlsx file using java?
I Have a data stored in variables and then i want to write my data to excel file(.xlsx). 我有一个存储在变量中的数据,然后我想将数据写入excel文件(.xlsx)。 (ie) I use automation testing tools like selenium to get data from webpage and i store it in variable which i want to wrie in xlsx file
(ie)我使用诸如selenium之类的自动化测试工具从网页获取数据,并将其存储在要在xlsx文件中存储的变量中
After a lot of google search I found many of users uses list or objects to write into .xlsx file. 经过大量的google搜索,我发现许多用户使用列表或对象将其写入.xlsx文件。
I created a list and add my variable to that list and using looping statements (for loop) i checked whether my data is stored in list by printing it. 我创建了一个列表,并将变量添加到该列表中,并使用循环语句(for循环)通过打印将其数据存储在列表中,从而检查了我的数据。
Then I created XSSFWorkbook and XSSFSheet and XSSFRow and XSSFCell to write data. 然后,我创建了XSSFWorkbook和XSSFSheet以及XSSFRow和XSSFCell来写入数据。 I write a cell by using setCellValue method to my cell.
我通过使用setCellValue方法向单元格写入一个单元格。
My code successfully creates a xlsx file and sheet in it but after execution i could not able to find any data in it. 我的代码在其中成功创建了一个xlsx文件和表格,但是执行后我无法在其中找到任何数据。
Source code: 源代码:
ArrayList<String> head = new ArrayList<String>();
head.add("Register Number");
head.add(subject1);
head.add(subject2); //subject1 and subject2 are variable i created
System.out.println(head.get(1)); //To check if my list has value
XSSFWorkbook workbook = new XSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream("/home/st.xlsx");
for (int i = 0; i < head.size(); i++)
{
XSSFRow Row = sheet1.createRow(1);
XSSFCell cell = Row.createCell(1);
cell.setCellValue(head.get(1));
sheet1.autoSizeColumn(1);
}
workbook.write(fileOut);
fileOut.close();
I expect my code add data to my file. 我希望代码将数据添加到文件中。
Main thing is During execution when i try to open my .xlsx file it has data in it. 最主要的是在执行期间,当我尝试打开.xlsx文件时,文件中有数据。
But after the complete execution i get with the empty xlsx file. 但是在完成执行之后, 我得到了空的xlsx文件。
I don't know why i'm getting this and What wrong in my code? 我不知道为什么得到这个,我的代码有什么问题?
Thanks in advance! 提前致谢!
ArrayList<String> head = new ArrayList<String>();
head.add("Register Number");
head.add(subject1);
head.add(subject2); // subject1 and subject2 are variable i created
System.out.println(head.get(0)); // To check if my list has value
System.out.println(head.get(1)); // To check if my list has value
System.out.println(head.get(2)); // To check if my list has value
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet1 = wb.createSheet("Sheet1");
for (int r = 0; r < head.size(); r++)
{
XSSFRow row = sheet1.createRow(r);
XSSFCell cell = row.createCell(0);
cell.setCellValue(head.get(r));
sheet1.autoSizeColumn(0);
}
// Write this workbook to a FileOutputStream.
FileOutputStream fileOut = new FileOutputStream("/home/st.xlsx");
workbook.write(fileOut);
fileOut.flush();
fileOut.close();
More info here: https://gist.github.com/madan712/3912272 更多信息在这里: https : //gist.github.com/madan712/3912272
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.