簡體   English   中英

如何使用Java從數據庫讀取數據來生成Excel文件,並且該數據應該在Excel文件中分散到多個工作表中

[英]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.

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