简体   繁体   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

I am trying to print data from a database to excel sheet using Java but since the data is very large in number i am getting "Exception in thread java.lang.OutOfMemoryError: Java heap space".I want that after 40000 rows the data should be printed in the next 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

    }
}

If CSV, better tab-separated values in a text file would be feasible, then that would be best: you can write it out sequentially which is fastest. 如果使用CSV,则在文本文件中使用更好的制表符分隔值将是可行的,那将是最好的:您可以按顺序将其写出,这是最快的。 (Disadvantage: probably cannot compress, which is what .xlsx does.) (缺点:.xlsx可能无法压缩。)

Use the database with a sequential query, uniqueStrings should not be necessary. 将数据库与顺序查询一起使用,则uniqueStrings

Increase the memory of the application java -Xmx2g . 增加应用程序java -Xmx2g的内存。

.xslx is a zip format and probably fits best. .xslx是zip格式,可能最适合。 However try .xls too, it generally is faster and might surprise us. 但是也尝试.xls ,它通常更快,可能会让我们感到惊讶。

Try use SXSSFWorkbook , as this streaming version should not keep the entire DOM, object model, in memory . 请尝试使用SXSSFWorkbook ,因为此流版本不应将整个DOM对象模型保留在内存中

Streaming version of XSSFWorkbook implementing the "BigGridDemo" strategy. 实施“ BigGridDemo”策略的XSSFWorkbook的流版本。 This allows to write very large files without running out of memory as only a configurable portion of the rows are kept in memory at any one time. 这允许写入非常大的文件而不会用完内存,因为任何时候只有一行的可配置部分都保留在内存中。

I would stay with the simplest methods, and not profoundly style Excel. 我会保留最简单的方法,而不是深刻地设计Excel。

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

Discard new Label and dataFormat . 丢弃new LabeldataFormat

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM