求一个从数据库中读出列表记录。然后把记录列表用JSP来导出到excel表中 求一个从数据库中读出列表记录。然后把记录列表用JSP来导出到excel表中:要求实例可行。可以的话。马上给分: 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 有两种方法可以用来保存成本地(即下载到客户端)excel文件。一种是使用IE提供的excelObject,用script就可以创建。另一种是用POI生成输出流并用写到response中,客户端会提示下载保存页面。说明:第一方法不通用,而且需要开发activex 我以前做的只要在jsp 中加如: response.setContentType("application/msexcel;charset=UTF-8"); response.setHeader("Content-Disposition",";filename=\treport.xls");就可以满足要求了 下面的程序我是用别人的,楼主可以看看就用:import java.io.*;import java.sql.*;import org.apache.poi.hssf.model.*; //Workbook.class, include workbookimport org.apache.poi.hssf.usermodel.*; //HSSFSheet.class, include sheetpublic class MyExcel { public static void main(String[] args) throws IOException { String dbDriver ="com.microsoft.jdbc.sqlserver.SQLServerDriver"; String dbUrl ="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs"; String userName ="sa"; String password ="test"; try { Class.forName(dbDriver); System.out.println("Drier is OK! "); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } try { Connection conn =DriverManager.getConnection(dbUrl, userName, password); System.out.println("Connection is OK! "); Statement stmt =conn.createStatement(); ResultSet rs =stmt.executeQuery("SELECT * FROM stores"); System.out.println("Query data is OK! "); resultSetToExcel(rs, "pubs.xls"); rs.close(); conn.close(); } catch (SQLException ex) { ex.printStackTrace(); } } public static void resultSetToExcel(ResultSet rs, String fileName) throws IOException { HSSFWorkbook workbook =new HSSFWorkbook(); HSSFSheet sheet =workbook.createSheet(); HSSFRow row =null; HSSFCell cell =null; HSSFFont cf =workbook.createFont(); HSSFCellStyle cs =workbook.createCellStyle(); cf.setFontHeightInPoints((short)12); cf.setColor(HSSFFont.COLOR_RED); cf.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); cs.setFont(cf); short currRowNum =(short) 0; try { ResultSetMetaData metaData =rs.getMetaData(); int columnCount =metaData.getColumnCount(); row =sheet.createRow(currRowNum); System.out.println(columnCount +" Columns"); for (int i=1; i<=columnCount; i++) { cell =row.createCell((short)(i-1)); cell.setCellValue(metaData.getColumnName(i)); cell.setCellStyle(cs); } while (rs.next()) { row =sheet.createRow(++currRowNum); for (short i=0; i<columnCount; i++) { cell =row.createCell(i); cell.setCellValue(rs.getString(i+1)); } } } catch (SQLException ex) { ex.printStackTrace(); } try { FileOutputStream out =new FileOutputStream(fileName); workbook.write(out); out.close(); System.out.println(fileName +" is created!"); } catch (FileNotFoundException ex) { ex.printStackTrace(); } }} Java字符编码转换中JIS8和UTF8互相转换 一个随机产生若干字母(A-Z间),直到出现字母Q停止,怎么写呀??? SQLServer2000和6.5的jdbc连接有什么区别 求一个比较全的java面试题库 压缩成jar包的三个命令的区别 如何获得一个变量长度的数组 如何区别一个字符是英文还是汉字?? ****************ORACLE与JAVA组合难题**************************** 模拟公共汽车的思路问题?????????????????? 今天是我生日!普天同庆!送分了! 字符串的调用~~ 上传附件如何管理
一种是使用IE提供的excelObject,用script就可以创建。
另一种是用POI生成输出流并用写到response中,客户端会提示下载保存页面。
说明:
第一方法不通用,而且需要开发activex
response.setContentType("application/msexcel;charset=UTF-8");
response.setHeader("Content-Disposition",";filename=\treport.xls");
就可以满足要求了
import java.io.*;
import java.sql.*;import org.apache.poi.hssf.model.*; //Workbook.class, include workbook
import org.apache.poi.hssf.usermodel.*; //HSSFSheet.class, include sheetpublic class MyExcel {
public static void main(String[] args) throws IOException {
String dbDriver ="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String dbUrl ="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
String userName ="sa";
String password ="test"; try {
Class.forName(dbDriver);
System.out.println("Drier is OK! ");
}
catch (ClassNotFoundException ex) {
ex.printStackTrace();
} try {
Connection conn =DriverManager.getConnection(dbUrl, userName, password);
System.out.println("Connection is OK! ");
Statement stmt =conn.createStatement();
ResultSet rs =stmt.executeQuery("SELECT * FROM stores");
System.out.println("Query data is OK! ");
resultSetToExcel(rs, "pubs.xls"); rs.close();
conn.close();
}
catch (SQLException ex) {
ex.printStackTrace();
}
} public static void resultSetToExcel(ResultSet rs, String fileName) throws IOException {
HSSFWorkbook workbook =new HSSFWorkbook();
HSSFSheet sheet =workbook.createSheet();
HSSFRow row =null;
HSSFCell cell =null;
HSSFFont cf =workbook.createFont();
HSSFCellStyle cs =workbook.createCellStyle(); cf.setFontHeightInPoints((short)12);
cf.setColor(HSSFFont.COLOR_RED);
cf.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); cs.setFont(cf); short currRowNum =(short) 0; try {
ResultSetMetaData metaData =rs.getMetaData();
int columnCount =metaData.getColumnCount();
row =sheet.createRow(currRowNum);
System.out.println(columnCount +" Columns");
for (int i=1; i<=columnCount; i++) {
cell =row.createCell((short)(i-1));
cell.setCellValue(metaData.getColumnName(i));
cell.setCellStyle(cs);
} while (rs.next()) {
row =sheet.createRow(++currRowNum);
for (short i=0; i<columnCount; i++) {
cell =row.createCell(i);
cell.setCellValue(rs.getString(i+1));
}
}
}
catch (SQLException ex) {
ex.printStackTrace();
} try {
FileOutputStream out =new FileOutputStream(fileName);
workbook.write(out);
out.close();
System.out.println(fileName +" is created!");
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
}