使用ApachePOI将结果集从Java数据库导出到Excel
我在将结果集导出到excel的项目中遇到了帮助。。以前的解决方案没有回答我的问题,但它们有帮助。。这是我的代码,到目前为止,它只在数据库中显示一行。 我的代码
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
public class Plexada2 {
public static void main(String[] args) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:Storeway","root", "");
Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = st.executeQuery("Select * from Storeway.order");
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("lawix10");
Row row = sheet.createRow(0);
int x=0;
while (rs.next()){
String crate_id= rs.getString(2);
String content=rs.getString(3);
String Order_type= rs.getString(4);
java.sql.Date date= rs.getDate(5);
String datex= String.valueOf(date);
row.createCell(0).setCellValue(crate_id);
row.createCell(1).setCellValue(content);
row.createCell(2).setCellValue(Order_type);
row.createCell(3).setCellValue(datex);
}
x+=1;
String yemi = "C:\\Users\\lawix10\\Desktop\\testlno9.xls";
FileOutputStream fileOut;
try {
fileOut = new FileOutputStream(yemi);
workbook.write(fileOut);
fileOut.close();
}
# 1 楼答案
我们可以让for循环在getString()中输入动态值吗?我试过了,但没有得到确切的结果
我的代码: 公共静态void excel(字符串appDB) { List headerValues=new ArrayList()
# 2 楼答案
上述问题得到解决
# 3 楼答案
我建议另一种解决方案,利用MemPOIlib。 看一看:
# 4 楼答案
啊,很难检查代码,在发布之前尝试格式化代码,关于查询,尝试类似的方法