簡體   English   中英

如何使用Java將數據庫中的數據打印到Excel到多張紙中,每張紙的范圍為40000行

[英]How to print data from a Database to Excel using Java into multiple sheets with a range of 40000 rows per sheet

我正在嘗試使用Java將數據從數據庫打印到excel工作表,但是由於數據數量很大,我遇到了“線程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

    }
}

如果使用CSV,則在文本文件中使用更好的制表符分隔值將是可行的,那將是最好的:您可以按順序將其寫出,這是最快的。 (缺點:.xlsx可能無法壓縮。)

將數據庫與順序查詢一起使用,則uniqueStrings

增加應用程序java -Xmx2g的內存。

.xslx是zip格式,可能最適合。 但是也嘗試.xls ,它通常更快,可能會讓我們感到驚訝。

請嘗試使用SXSSFWorkbook ,因為此流版本不應將整個DOM對象模型保留在內存中

實施“ BigGridDemo”策略的XSSFWorkbook的流版本。 這允許寫入非常大的文件而不會用完內存,因為任何時候只有一行的可配置部分都保留在內存中。

我會保留最簡單的方法,而不是深刻地設計Excel。

  • Workbook.createSheet
  • Sheet.createRow
  • Row.createCell
  • Cell.setCellValue

丟棄new LabeldataFormat

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM