无法创建WorkbookObject

时间:2018-09-26 13:56:19

标签: java apache-poi xssf

我正在尝试将ResultSet写入Excel工作簿。  为此,我正在使用poi-ooxml-3.17 lib。 但是我无法创建XSSFWorkbook的对象。 我的控制权不在线以下。我也没有得到exception

XSSFWorkbook workbook = new XSSFWorkbook();

此问题的任何解决方法。 下面是我在应用程序中使用的jar。

enter image description here

下面是我的代码:

包javaapplication1;

import com.mysql.jdbc.Statement;
import java.io.File;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class JavaApplication1 {

    public static void main(String[] args){

        try{

        Class.forName("com.mysql.jdbc.Driver");
      Connection connect = DriverManager.getConnection( 
         "jdbc:mysql://localhost:3306/database" , 
         "root" , 
         "pass"
      );

      Statement statement = (Statement) connect.createStatement();
      ResultSet resultSet = statement.executeQuery("SELECT col1,col2, col3 FROM table");

      System.out.println("Before");

      XSSFWorkbook workbook = new XSSFWorkbook(); 
         System.out.println("After");
      XSSFSheet spreadsheet = workbook.createSheet("table_data");

      XSSFRow row = spreadsheet.createRow(1);
      XSSFCell cell;
      cell = row.createCell(1);
      cell.setCellValue("COL1");
      cell = row.createCell(2);
      cell.setCellValue("COL2");
      cell = row.createCell(3);
      cell.setCellValue("COL3");

      int i = 2;

      while(resultSet.next()) {
         row = spreadsheet.createRow(i);
         cell = row.createCell(1);
         cell.setCellValue(resultSet.getString("col1"));
         cell = row.createCell(2);
         cell.setCellValue(resultSet.getString("col2"));
         cell = row.createCell(3);
         cell.setCellValue(resultSet.getString("col3"));

         i++;
      }

      FileOutputStream out = new FileOutputStream(new File("exceldatabase.xlsx"));
      workbook.write(out);
      out.close();
      System.out.println("exceldatabase.xlsx written successfully");
   }
        catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null, ex);
        }
    }

}

谢谢..

0 个答案:

没有答案