public void witeExcel(ArrayList<String> B,ArrayList<String> P, ArrayList<String> H) throws WriteException, IOException { WorkbookSettings wbSettings = new WorkbookSettings(); wbSettings.setLocale(new Locale("en", "EN")); WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings); workbook.createSheet("Serial Numbers", 0); Write_Excel test = new Write_Excel(); test.setOutputFile("C://Users//Tanmay//Desktop//Export.xls"); for (int index = 0; index < B.size(); index++) { box = B.get(index); plc = P.get(index); hmi = H.get(index); test.write(workbook); }System.out.println("Please check the result file under C://Users//Tanmay//Desktop//Export.xls "); }
在for循环之前移动逻辑以创建Workbook
更改写入方法以传递工作簿
public void write(WritableWorkbook workbook) throws IOException, WriteException { File file = new File(inputFile); WritableSheet excelSheet = workbook.getSheet(0); ................
原因是每次调用时都要在方法中创建工作簿,以便清理旧数据并添加新数据。
从方法中删除它并移动到其他地方执行一次。
File file = new File(inputFile);//From File WorkbookSettings wbSettings = new WorkbookSettings(); wbSettings.setLocale(new Locale("en", "EN")); WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings); workbook.createSheet("Serial Numbers", 0);/To configuration
我建议你将初始化移动到构造函数。
writeExcel
wordExcel