[英]Assistance required in writing list of data to a particular column in an excel file using java
[英]Writing list of data in excel file using JAVA is entering the data only in the last column
我正在遍历从运行程序文件( FunctionVerifier.java )发送的数据列表。 当我在excelHandler.java中调用函数writeExcel()时,它只输入我正在迭代的列表中的最后一个数据。
有人可以让我知道原因以及如何解决这个问题
public void writeExcel(String sheetName, int r, int c, String data) throws IOException {
file = new FileInputStream(new File(inFilePath));
wb = new XSSFWorkbook(file);
Sheet sh;
sh = wb.getSheet(sheetName);
Row row = sh.createRow(r);
row.createCell(c).setCellValue(data);
closeExcelInstance();
FileOutputStream outputStream = new FileOutputStream(inFilePath);
wb.write(outputStream);
wb.close();
outputStream.close();
}
public void closeExcelInstance() {
try {
file.close();
} catch (Exception e) {
System.out.println(e);
}
}
FunctionVerifier.java
package Sterling.oms;
import Sterling.oms.Process.CouponValidationProcess;
import Sterling.oms.Utilities.ExcelHandler;
import java.util.ArrayList;
public class FuncVerify {
public static void main(String args[]) throws Exception {
String filePath = System.getProperty("user.dir") + "/src/test/resources/TestData/test.xlsx";
ExcelHandler excelHandler = new ExcelHandler(filePath);
excelHandler.readExcelData("Validation");
CouponValidationProcess couponValidationProcess = new CouponValidationProcess("OMS-T781");
excelHandler.createSheet();
// couponValidationProcess.enterValidationHeaderRowInExcel(filePath);
String sheet = "ValidationData";
if (excelHandler.getRowCountWhenNull(sheet) < 1) {
ArrayList<String> header = new ArrayList<>();
header.add("Test Case");
header.add("Coupon ID");
header.add("Grand Total");
header.add("Manual Grand Total");
for (int i = 0; i < header.size(); i++) {
// excelHandler = new ExcelHandler(filePath);
excelHandler.writeExcel(sheet, 0, i, header.get(i));
// excelHandler.closeExcelInstance();
}
}
}
}
只存储最后一项的原因是Sheet.createRow
和Row.createCell
完全按照它们的方法名称执行。 他们每次被调用时都会创建一个新的空行或单元格。 因此,每次调用Row row = sh.createRow(r)
时,它都会在行索引r
处创建一个新的空行,并丢失该行中所有以前创建的单元格。
使用行的正确方法是首先尝试从工作表中获取行。 并且仅当它不存在( null
)时,才创建一个新行。 行中的单元格也是如此。 首先尝试获取它们。 只有在不存在的情况下,才创建它们。
...
Sheet sh;
sh = wb.getSheet(sheetName);
Row row = sh.getRow(r); if (row == null) row = sh.createRow(r);
Cell cell = row.getCell(c); if (cell == null) cell = row.createCell(c);
cell.setCellValue(data);
...
这就是您当前问题的答案。
但是整个方法,打开 Excel 文件以创建一个Workbook
,然后只设置一个单元格的数据,然后将整个工作簿写入文件,并为每个单元格执行此操作,是非常次优的。 相反,应该打开工作簿,然后将所有已知的新单元格值设置到工作表中,然后写出工作簿。
您的方法是错误的,您为要写入Excel
的每一行再次打开文件,然后再次保存。 您只需创建一个FileInputStream
并将其发送到您的Workbook
,您可以在其中完成所有 Excel 工作。 完成所有行的编写后,您只能创建一个FileOutputStream
并将您在 Workbook 中的更改导出到您选择的文件中。
writeExcel()
public void writeExcel(String sheetName, int r, int c, ArrayList<String> data) throws IOException {
file = new FileInputStream(new File(inFilePath));
wb = new XSSFWorkbook(file);
Sheet sh;
sh = wb.getSheet(sheetName);
Row row = sh.createRow(r);
//Adding data column wise
for (String h : data) {
row.createCell(c++).setCellValue(h);
}
closeExcelInstance();
FileOutputStream outputStream = new FileOutputStream(inFilePath);
wb.write(outputStream);
wb.close();
outputStream.close();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.