我正在尝试使用Apache POI
将SQLite
数据库导出为.xlsx或xls。它似乎没有遇到问题,但在XSSFWorkbook workbook = new XSSFWorkbook();
之后发生错误。它确实表明我关闭了workbook
,但这并没有解决我的问题..
我的代码:
import java.io.File;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
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 Export
{
public static void main(String[] args) throws Exception
{
Connection connection = null;
//@Override
//public Connection getConection() throws SQLException {
try {
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:" +"C:/Test/DATABASE.db");
throw new SQLException(e.getMessage());
} catch (Exception e) {
throw new SQLException(e.getCause());
}
DriverManager.getConnection("jdbc:sqlite:" + "resources\\COPY_TRACARGODB.db");
Statement statement = connection.createStatement();
ResultSet resultSet = statement
.executeQuery("select * from BILL_LADING");
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet spreadsheet = workbook
.createSheet("Tracargo EXPORTED DB");
XSSFRow row=spreadsheet.createRow(1);
XSSFCell cell;
cell=row.createCell(1);
cell.setCellValue("First Name");
cell=row.createCell(2);
cell.setCellValue("Last Name");
cell=row.createCell(3);
cell.setCellValue("Date");
cell=row.createCell(4);
cell.setCellValue("Time");
cell=row.createCell(5);
cell.setCellValue("Destination");
int i=2;
while(resultSet.next())
{
row=spreadsheet.createRow(i);
cell=row.createCell(1);
cell.setCellValue(resultSet.getInt("first_name"));
cell=row.createCell(2);
cell.setCellValue(resultSet.getString("last_name"));
cell=row.createCell(3);
cell.setCellValue(resultSet.getString("date"));
cell=row.createCell(4);
cell.setCellValue(resultSet.getString("time"));
cell=row.createCell(5);
cell.setCellValue(resultSet.getString("destination"));
i++;
}
FileOutputStream out = new FileOutputStream(
new File("exceldatabase.xlsx"));
workbook.write(out);
out.close();
// workbook.close();
System.out.println(
"exceldatabase.xlsx written successfully");
}
}
我的错误:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject
at Export.main(Export.java:38)
Caused by: java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlObject
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more