【发布时间】:2014-07-24 12:35:44
【问题描述】:
帮助我坚持这个从结果集导出到 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();
}
【问题讨论】:
-
每次迭代 ResultSet 对象后,您在哪里创建新行?更好的是,您在第 0 行创建标题并开始在 while 循环中创建行,使用 x 进行迭代
-
您在 excel 表中看到了什么,您可以使用 java 程序访问该位置吗?
-
你能分享一下你在 excel 表中得到了什么吗?另外,如果可能,请正确格式化上述代码。
-
我只能从 excel 的数据库中看到一行
-
有点晚了,但是看看 MemPOI 的未来github.com/firegloves/MemPOI
标签: java apache-poi resultset