怎么用java poi导出execl文件 小弟刚学很菜 那位大侠给全部源码包括js里的调用 谢谢很急
------解决方案--------------------
[code=Java][/code]package com.javaeye.yongsky;
import java.sql.*;
import java.util.*;
import com.microsoft.jdbc.*;
public class SheetDataSource {
private static Connection con ;
private static final String DatabaseName = "POIDEMO";
private static final String userName = "sa";
private static final String password = "123";
public SheetDataSource()
{
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
try {
con = DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName="+DatabaseName,userName,password);
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static ResultSet selectAllDataFromDB() throws SQLException
{
new SheetDataSource();
Statement stmt = con.createStatement();
return stmt.executeQuery("select * from COMPANY");
}
}
------解决方案--------------------
//创建工作薄
HSSFWorkbook wb = new HSSFWorkbook();
//创建工作表
HSSFSheet sheet =wb.createSheet("公司受理案件统计");
//列宽
sheet.setColumnWidth((short)0, 5000);
sheet.setColumnWidth((short)1, 5000);
sheet.setColumnWidth((short)2, 5000);
sheet.setColumnWidth((short)3, 5000);
sheet.setColumnWidth((short)4, 5000);
sheet.setColumnWidth((short)5, 5000);
sheet.setColumnWidth((short)6, 5000);
sheet.setColumnWidth((short)7, 5000);
sheet.setColumnWidth((short)8, 5000);
//在表中建行
HSSFRow row = sheet.createRow(0);
HSSFCell cell[] = new HSSFCell[9];
for(short i=0; i<9; i++){
cell[i]=row.createCell(i);
}
cell[0].setCellValue("公司");
cell[1].setCellValue("待处理");
cell[2].setCellValue("立案处理中");
cell[3].setCellValue("已结案");
cell[4].setCellValue("已结案(信息不完整)");
cell[5].setCellValue("非客户投诉退回");
cell[6].setCellValue("退回修改");
cell[7].setCellValue("已归并");
cell[8].setCellValue("合计");
//将数据以Excel导出
OutputStream sos = response.getOutputStream();
wb.write(sos);
sos.close();
你可以在页面上定义一个按钮,然后让它跳转到你的action里的方法,方法里可以写如何导出excel,将你查出来的数据分别写入到你创建的Excel的单元格里。就OK了。