[英]How to test the method that reads the data from the excel file in java?
[英]How to generate an excel file using java which reads data from a database and the data should we spread into multiple sheets within the excel file
我想要在將數據從數據庫中寫入1000行后將其寫入excel時,數據應自動移至下一張紙。我們將如何在Java中編寫代碼(邏輯)
List<String> header = new ArrayList<String>();
header.add("EmployeeId");
header.add("EmployeeEmailId");
header.add("EmployeeAddress");
header.add("EmployeePhonenumber");
header.add("EmployeePincode");
int horizCount = 0;
int verticalCount = 0;
for ( String head : header ) {
workSheet.addCell(new Label(verticalCount++,horizCount,head,headerFormat));
}
horizCount = 1 ;
for( Employee employee: uniqueStrings ){
verticalCount = 0;
workSheet.addCell(new Label(verticalCount++,horizCount,employee.getEmployeeId(),dataFormat));`enter code here`
workSheet.addCell(new Label(verticalCount++,horizCount,employee.getEmployeeEmailId(),dataFormat));
workSheet.addCell(new Label(verticalCount++,horizCount,employee.getEmployeeadddress(),dataFormat));
workSheet.addCell(new Label(verticalCount++,horizCount,employee.getEmployeephoneno(),dataFormat));
workSheet.addCell(new Label(verticalCount++,horizCount,employee.getEmployeepincode(),dataFormat));
horizCount++;
}
//write to the excel sheet
workbook.write();
//close the workbook
workbook.close();
}
catch(FileNotFoundException e)
{
workbook.write();
//close the workbook
workbook.close();
throw new IOException("File Not found exception occured.");
}
從您的代碼中,我假設您是在for循環上方的某個地方創建工作表。 如果寫入的記錄數= 1000,則在循環中創建一個新的工作表。 像這樣的東西
if ((horizCount % 1000) == 0) {
// Create a new workSheet
workSheet = new WorkSheet();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.