求一个从数据库中读出列表记录。然后把记录列表用JSP来导出到excel表中:要求实例可行。可以的话。马上给分:

解决方案 »

  1.   

    有两种方法可以用来保存成本地(即下载到客户端)excel文件。
    一种是使用IE提供的excelObject,用script就可以创建。
    另一种是用POI生成输出流并用写到response中,客户端会提示下载保存页面。
    说明:
    第一方法不通用,而且需要开发activex
      

  2.   

    我以前做的只要在jsp 中加如:
     response.setContentType("application/msexcel;charset=UTF-8");
     response.setHeader("Content-Disposition",";filename=\treport.xls");
    就可以满足要求了
      

  3.   

    下面的程序我是用别人的,楼主可以看看就用:
    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();
            }
        }