简体   繁体   English

使用结果集将大数据从数据库导出到excel

[英]Exporting large data from database into excel using resultset

I need to export result of a resultset into an excel sheet using java. 我需要使用java将结果集的结果导出到excel表中。 Resultset contains a table with thousands of rows and multiple columns. Resultset包含一个包含数千行和多列的表。 If someone could provide a sample code that will be very useful as I am stuck with this problem. 如果有人可以提供一个非常有用的示例代码,因为我遇到了这个问题。 Below is the sample code that I am using to achieve this but somehow not able to get correct data in excel file:- 下面是我用来实现此目的的示例代码,但不知何故无法在excel文件中获取正确的数据: -

SSFWorkbook workbook = new XSSFWorkbook();
    try {
    setConnection(appDB);

    String queryName="SELECT * FROM ALL_TABLES where table_name='table_name'";

    Reporter.addStepLog("----------------------------------- " + queryName.toUpperCase()
            + "\n - Validation Start" + " -----------------------------------");
    ps = con.prepareStatement(queryName, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
    rs = ps.executeQuery();

    Statement statement = con.createStatement();
    XSSFSheet spreadsheet = workbook.createSheet("employedb");
      ResultSet resultSet = statement.executeQuery("select * from all_tab_columns where table_name='table_name'"); 
      XSSFRow row = spreadsheet.createRow(0);
      XSSFCell cell;
      int cc=resultSet.getMetaData().getColumnCount();
      for(int i=1;i<=cc;i++)
      {
          String headerVal=resultSet.getMetaData().getColumnName(i);
          headerValues.add(headerVal);
          cell = row.createCell(i-1);
          cell.setCellValue(resultSet.getMetaData().getColumnName(i));
      }
      System.out.println(headerValues);
      int i = 1;
      while (resultSet.next())
      {  
          for(int j=1;j<=cc;j++)
          {  
          System.out.println(resultSet.getString(j));
          XSSFRow row1 = spreadsheet.createRow((short) i);
          row1.createCell((short) i).setCellValue(resultSet.getString(resultSet.getMetaData().getColumnName(j)));
          i++;

      }  
      }

      FileOutputStream out = new FileOutputStream(new File("S:\\Downloads\\excel.xlsx"));
      workbook.write(out);
      out.close();  
      System.out.println("exceldatabase.xlsx written successfully");

}catch(Exception e){}
}

Please let me know if the question is not clear as I am new to this forum. 如果问题不明确,请告诉我,因为我是这个论坛的新手。

To me the Question is not so clear. 对我来说,问题不是那么清楚。 You want to put calculate data to excel sheet? 您想将计算数据放入Excel工作表吗? or you want to get calculatet data from an excel sheet? 或者您想从Excel工作表中获取计算数据?

I understood you want to put some data into the sheet. 我知道你想把一些数据放到表格中。 So you can use several Frameworks to do this. 因此,您可以使用多个框架来执行此操作。 Eg smartXLS or Apache POI library 例如smartXLS或Apache POI库

    List<Double> resultList = new ArrayList();
    resultList.add(250.0);
    resultList.add(300.5);
    resultList.add(500.9);


public void setResultsToSheet(List results) throws Exception {
    WorkBook workBook = new WorkBook();
    int row = 0; //index of first row is 0
    for (int i = 0; i < results.size(); i++) {
        double result = (double) results.get(i);
        workBook.setNumber(row, 1, result);
        row++;
    }
    workBook.write("X\\yourDestination\\excel.xlsx");
}

Hope that can help you anyway. 希望无论如何都可以帮到你。

You can try using MemPOI . 您可以尝试使用MemPOI Take a look: 看一看:

File file = new File("S:\\Downloads\\excel.xlsx")

PreparedStatement prepStmt = this.connection.prepareStatement("SELECT * FROM ALL_TABLES where table_name='table_name'");
new MempoiBuilder()
                .setFile(file)
                .addMempoiSheet(new MempoiSheet(prepStmt, "employedb"))
                .build()
                .prepareMempoiReportToFile()
                .get();

You can easily add a template. 您可以轻松添加模板。 Take a look at the doc 看看文档

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

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