[英]How to write an array data into excel using java
I have an array of Strings that needs to be inserted into an Excel sheet 我有一个字符串数组,需要将其插入Excel工作表
I tried with the below code snippet, but only the first value is inserting and for the rest, I am getting null in my console. 我尝试使用下面的代码片段,但是只有第一个值正在插入,而其余的则在控制台中为空。
String[] excelData = {"first", "second", "third", "fourth", "fifth"};
int rowStart = 1;
for (int count = 0; count <= excelData.length; count++) {
writeExcel(fileName, excelData[count], rowStart, 0);
rowStart++;
}
UPDATE 更新
Added writeExcel
code. 添加了
writeExcel
代码。
public void writeExcel(String excelFile, String userNumber, int rows, int cols) {
try{
//Create an object of File class to open xlsx file
File file = new File(excelFile);
//Create an object of FileInputStream class to read excel file
FileInputStream inputStream = new FileInputStream(file);
Workbook requestsFile = null;
//Find the file extension by splitting file name in substring and getting only extension name
String fileExtensionName = excelFile.substring(excelFile.indexOf("."));
//Check condition if the file is xlsx file
if(fileExtensionName.equals(".xlsx")){
//If it is xlsx file then create object of XSSFWorkbook class
requestsFile = new XSSFWorkbook(inputStream);
} else if(fileExtensionName.equals(".xls")){
//If it is xls file then create object of XSSFWorkbook class
requestsFile = new HSSFWorkbook(inputStream);
} else {
System.out.println("File format is invalid");
}
//Read excel sheet by sheet name
Sheet sheet = requestsFile.getSheetAt(0);
Cell val1 = sheet.getRow(rows).getCell(cols);
val1.setCellValue(userNumber);
//Close input stream
inputStream.close();
//Create an object of FileOutputStream class to create write data in excel file
FileOutputStream outputStream = new FileOutputStream(file);
//write data in the excel file
requestsFile.write(outputStream);
//close output stream
outputStream.close();
} catch(Exception e){
System.out.println(e.getMessage());
}
}
First of all we all don't know what your function writeExcel do and what parameters accepts , as you havn't post here 首先,我们都不知道您的函数writeExcel是做什么的,哪些参数可以接受,因为您没有在此处发布
taking the example as below of poi: 以poi的示例如下:
wb.getSheet(sheetName).getRow(rowNum).createCell(cellNum).setCellValue(input);
in above code, your loop must select one raw and increment the cellNum till you have data in array 在上面的代码中,循环必须选择一个原始数据并递增cellNum直到数组中包含数据
You are getting null
because at the line: 您得到
null
原因是:
Cell val1 = sheet.getRow(rows).getCell(cols);
This is get cell value from your workbook, not create. 这是从您的工作簿中获取单元格值,而不是创建。 The
null
appear indicate the cell blank, and this causes your code flow to: 出现的
null
表示单元格为空白,这会使您的代码流达到:
} catch(Exception e){
System.out.println(e.getMessage());
}
So please try change this line with: 因此,请尝试通过以下方式更改此行:
Cell val1 = sheet.createRow(rows).createCell(cols);
And change your loop count: 并更改您的循环计数:
count <= excelData.length
It will cause an error out of range
like: java.lang.ArrayIndexOutOfBoundsException: 5
它将导致错误
out of range
例如: java.lang.ArrayIndexOutOfBoundsException: 5
Change with: 更改:
count < excelData.length
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.