日期:2014-05-17  浏览次数:20707 次

jsp 制作网页xls下载的问题
怎么实现 从网页导出xsl文件 就是下载从数据库读出的数据 然后生成xsl文件 在下载到客户端

------解决方案--------------------
response.setHeader("Content-disposition", "attachment;filename="test.xls");
response.setContentType("application/msexcel");
OutputStream os = response.getOutputStream();

使用POI生成xls文件,生成时使用到Workbook wb = new HSSFWorkbook();
最后wb.write(os);即可。

附上我在项目中使用的代码:
Java code

public static int createSheet(HttpServletResponse response,Form1324 form)throws Exception {
        response.setHeader("Content-disposition", "attachment; filename="+form.getGate_id()+"_"+form.getDate_begin()+"_"+form.getDate_end()+".xls");
        response.setContentType("application/msexcel");
        OutputStream os = response.getOutputStream();
        HSSFWorkbook wb = new HSSFWorkbook();
        Sheet sheet = wb.createSheet("T中行");
        Row row = null;
        Cell cell = null;
        String[] refundLogs = form.getRefund_logs();
        String[] str = null;
        int len = refundLogs.length;
        int i = 0,j=0,k=0;
        int pageNo = (len -1)/10 + 1;
        int rndNum = 0;
        String cardNo = "";
        String txnDt = "";
        String txnAmt = "";
        String refAmt = "";
        String authCd = "";
        String bk_date = "";
        String sysSeqId = "";
        String sysDt = "";
        String orgData = "";
        int pageFrom = 0;
        double totalTxnAmt = 0.00;
        double totalRefAmt = 0.00;
        double subAmt = 0.00;
        
        DecimalFormat format = new DecimalFormat("0.00");
        
        CellStyle cs1 = wb.createCellStyle();
        Font font1 = wb.createFont();
        font1.setFontName("宋体");
        font1.setFontHeightInPoints((short)20);
        font1.setBoldweight(Font.BOLDWEIGHT_BOLD);
        cs1.setFont(font1);
        cs1.setAlignment(CellStyle.ALIGN_CENTER);
        
        CellStyle cs2 = wb.createCellStyle();
        Font font2 = wb.createFont();
        font2.setFontName("宋体");
        font2.setFontHeightInPoints((short)10);
        cs2.setFont(font2);
        cs2.setAlignment(CellStyle.ALIGN_CENTER);
        
        CellStyle cs3 = wb.createCellStyle();
        cs3.setFont(font2);
        cs3.setBorderTop(HSSFCellStyle.BORDER_THIN);
        cs3.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        cs3.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        cs3.setBorderRight(HSSFCellStyle.BORDER_THIN);
        cs3.setAlignment(CellStyle.ALIGN_CENTER);
        cs3.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        
        CellStyle cs4 = wb.createCellStyle();
        Font font4 = wb.createFont();
        font4.setFontHeightInPoints((short)14);
        font4.setFontName("宋体");
        font4.setBoldweight(Font.BOLDWEIGHT_BOLD);
        cs4.setFont(font4);
        cs4.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    
        for(j=0;j<pageNo;j++){
            totalTxnAmt = 0;
            totalRefAmt = 0;
            pageFrom = j*37;
            
            if(j==(pageNo-1)){
                if(len % 10 == 0 ){
                    rndNum = 10;
                }else{
                    rndNum = len % 10;
                }
            }else{
                rndNum = 10;
            }
            
            row = sheet.createRow(pageFrom);
            row.setHeightInPoints((short)40);
            cell = row.createCell(0);
            cell.setCellStyle(cs1);
            cell.setCellValue("中国银行人民币卡退款申请表");
            
            sheet.addMergedRegion(new CellRangeAddress(pageFrom, pageFrom, 0, 6));
            
            row = sheet.createRow(pageFrom+1);
            row.setHeightInPoints((short)20);
            cell = row.createCell(0);