Я пытаюсь распечатать данные из базы данных на лист Excel, используя Java, но так как данных очень много, я получаю «Исключение в потоке java.lang.OutOfMemoryError: пространство кучи Java». Я хочу, чтобы после 40000 строкданные должны быть напечатаны на следующем листе.
public class ExcelSheetGenerator {
public static String generateExcelSheetReport(List<Employee> employeeList, String filePath, String fileName)
throws Exception {
Set<Employee> uniqueStrings = new HashSet<Employee>();
uniqueStrings.addAll(employeeList);
// create WorkbookSettings object
WorkbookSettings ws = new WorkbookSettings();
WritableWorkbook workbook = null;
// Workbook workbook = new HSSFWorkbook();
try {
// File file = new File("D:\\tmpFolder\\Production\\StoreVisitDailyReport.xls");
File file = new File(filePath + fileName);
System.out.println("FIle is::::::" + file + ":::::" + filePath + "::::" + fileName);
System.out.println("FIle is::::::" + file + ":::::" + filePath + "::::" + fileName);
// create work sheet
workbook = Workbook.createWorkbook(file, ws);
WritableSheet workSheet;
workSheet = workbook.createSheet("Employee", 0);
SheetSettings sh = workSheet.getSettings();
// workSheet.setName("StoreVisitReport");
// Creating Writable font to be used in the report
WritableFont headerFont = new WritableFont(WritableFont.createFont("Arial"),
WritableFont.DEFAULT_POINT_SIZE, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE);
WritableFont normalFont = new WritableFont(WritableFont.createFont("Arial"),
WritableFont.DEFAULT_POINT_SIZE, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE);
// creating plain format to write data in excel sheet
WritableCellFormat headerFormat = new WritableCellFormat(headerFont);
headerFormat.setBackground(Colour.GRAY_50);
headerFormat.setShrinkToFit(true);
headerFormat.setWrap(true);
headerFormat.setAlignment(jxl.format.Alignment.CENTRE);
headerFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
headerFormat.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN, jxl.format.Colour.BLACK);
WritableCellFormat dataFormat = new WritableCellFormat(normalFont);
dataFormat.setAlignment(jxl.format.Alignment.CENTRE);
dataFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
dataFormat.setWrap(true);
dataFormat.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN, jxl.format.Colour.BLACK);
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));
// HSSFWorkbook workbook1 = new HSSFWorkbook();
}
horizCount = 1;
for (Employee employee : uniqueStrings) {
if (horizCount % 40000 == 0) {
workSheet = workbook.createSheet("Employee", 1);
}
verticalCount = 0;
workSheet.addCell(new Label(verticalCount++, horizCount, employee.getEmployeeId(), dataFormat));
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.");
} catch (IOException e) {
// workbook.write();
// close the workbook
workbook.close();
throw new IOException(e.getMessage());
} catch (Exception e) {
// workbook.write();
// close the workbook
workbook.close();
throw new Exception(e.getMessage());
}
System.out.println("<======Inside generateExcelSheetReport=====end");
System.out.println("<======Inside generateExcelSheetReport=====end");
return "success";
}
private static void workSheet() {
// TODO Auto-generated method stub
}
}