当前位置: 代码迷 >> Java Web开发 >> jsp 制作网页xls下载的有关问题
  详细解决方案

jsp 制作网页xls下载的有关问题

热度:37   发布时间:2016-04-17 11:14:17.0
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);            cell.setCellStyle(cs2);            cell.setCellValue("此申请为第_____次发送  上次发送日期:____________.");                        sheet.addMergedRegion(new CellRangeAddress(pageFrom+1, pageFrom+1, 0, 6));                        row = sheet.createRow(pageFrom+2);            cell = row.createCell(0);            cell.setCellValue("TO:中国银行银行卡部");            sheet.addMergedRegion(new CellRangeAddress(pageFrom+2, pageFrom+2, 0, 2));                        cell  = row.createCell(3);            cell.setCellValue("FROM: 汇付天下有限公司");            sheet.addMergedRegion(new CellRangeAddress(pageFrom+2, pageFrom+2, 3, 6));                        row = sheet.createRow(pageFrom+3);            cell = row.createCell(0);            cell.setCellValue("电话:63219234");            sheet.addMergedRegion(new CellRangeAddress(pageFrom+3, pageFrom+3, 0, 2));                        cell = row.createCell(3);            cell.setCellValue("*商户编号:104310148160006");            sheet.addMergedRegion(new CellRangeAddress(pageFrom+3, pageFrom+3, 3, 6));                        row = sheet.createRow(pageFrom+4);            cell = row.createCell(0);            cell.setCellValue("传真:63291037/63291049");            sheet.addMergedRegion(new CellRangeAddress(pageFrom+4, pageFrom+4, 0, 2));                        cell = row.createCell(3);            cell.setCellValue("电话:021-61613999*6209 传真:021-61613099");            sheet.addMergedRegion(new CellRangeAddress(pageFrom+4, pageFrom+4, 3, 6));                        row = sheet.createRow(pageFrom+5);            row.setHeightInPoints((short)30);            cell = row.createCell(0);            cell = row.createCell(0);            cell.setCellStyle(cs3);            cell.setCellValue("");                        cell = row.createCell(1);            cell.setCellStyle(cs3);            cell.setCellValue("*卡号");                        cell = row.createCell(2);            cell.setCellStyle(cs3);            cell.setCellValue("*原交易日期");                        cell = row.createCell(3);            cell.setCellStyle(cs3);            cell.setCellValue("*原交易金额");                        cell = row.createCell(4);            cell.setCellStyle(cs3);            cell.setCellValue("*原交易授权号");                        cell = row.createCell(5);            cell.setCellStyle(cs3);            cell.setCellValue("*退款金额");                        cell = row.createCell(6);            cell.setCellStyle(cs3);            cell.setCellValue("◇原因");                        for(i=0;i<rndNum;i++){                str = refundLogs[j*10+i].split("\\|");                cardNo = str[1];                bk_date = str[12];                sysSeqId = str[10];                sysDt = str[11];                if(!"".equals(sysDt))                    orgData = sysDt;                if(!"".equals(sysSeqId))                    orgData = orgData + sysSeqId;                if(!"".equals(bk_date))                    orgData = orgData + bk_date;                txnDt = orgData.substring(0,8);                txnAmt = str[2];                refAmt = str[3];                authCd = str[9];                                row = sheet.createRow(pageFrom+6+i);                row.setHeightInPoints((short)30);                cell = row.createCell(0);                cell.setCellStyle(cs3);                cell.setCellValue(i+1);                                cell = row.createCell(1);                cell.setCellStyle(cs3);                cell.setCellValue(cardNo);                                cell = row.createCell(2);                cell.setCellStyle(cs3);                cell.setCellValue(txnDt);                                cell = row.createCell(3);                cell.setCellStyle(cs3);                cell.setCellValue(txnAmt);                                cell = row.createCell(4);                cell.setCellStyle(cs3);                cell.setCellValue(authCd);                                cell = row.createCell(5);                cell.setCellStyle(cs3);                cell.setCellValue(refAmt);                                cell = row.createCell(6);                cell.setCellStyle(cs3);                cell.setCellValue("持卡人退货");                                totalTxnAmt += Double.parseDouble(txnAmt);                totalRefAmt += Double.parseDouble(refAmt);            }                        //最后一页补充            if(j==((pageNo-1))){                rndNum = 10-rndNum;                for(k=0;k<rndNum;k++){                    row = sheet.createRow(pageFrom+6+i+k);                    row.setHeightInPoints((short)30);                    row.createCell(0).setCellStyle(cs3);                    row.createCell(1).setCellStyle(cs3);                    row.createCell(2).setCellStyle(cs3);                    row.createCell(3).setCellStyle(cs3);                    row.createCell(4).setCellStyle(cs3);                    row.createCell(5).setCellStyle(cs3);                    row.createCell(6).setCellStyle(cs3);                }            }                        row = sheet.createRow(pageFrom+16);            row.setHeightInPoints((short)30);            cell = row.createCell(0);            cell.setCellStyle(cs3);            cell.setCellValue("合计");                        cell = row.createCell(1);            cell.setCellStyle(cs3);            cell.setCellValue("退款总金额:");            row.createCell(2).setCellStyle(cs3);            row.createCell(3).setCellStyle(cs3);            row.createCell(4).setCellStyle(cs3);            sheet.addMergedRegion(new CellRangeAddress(pageFrom+16, pageFrom+16, 1, 4));                        cell = row.createCell(5);            cell.setCellStyle(cs3);            cell.setCellValue(format.format(totalRefAmt));            row.createCell(6).setCellStyle(cs3);            sheet.addMergedRegion(new CellRangeAddress(pageFrom+16, pageFrom+16, 5, 6));                        row = sheet.createRow(pageFrom+17);            cell = row.createCell(0);            cs2.setWrapText(true);            cell.setCellStyle(cs2);            cell.setCellValue("◇原因: A.持卡人退货   B.刷卡后付现金   C.银行重复清算   D.收银重复刷卡 E.POS机误操作     F.有查询编号                    G.其他");                        sheet.addMergedRegion(new CellRangeAddress(pageFrom+17, pageFrom+18, 0, 6));                        row = sheet.createRow(pageFrom+19);            cell = row.createCell(0);            cell.setCellStyle(cs4);            cell.setCellValue("其中加*号的项目为必填项");                        row = sheet.createRow(pageFrom+20);            cell = row.createCell(0);            cell.setCellStyle(cs4);            cell.setCellValue("公司财务章或公章:");            sheet.addMergedRegion(new CellRangeAddress(pageFrom+20, pageFrom+26, 0, 6));                        row = sheet.createRow(pageFrom+27);            cell = row.createCell(0);            cell.setCellValue("备注:1.上述退款金额将从信用卡交易中抵扣,轧差后净额结算。");            row = sheet.createRow(pageFrom+28);            cell =  row.createCell(0);            cell.setCellValue("      2.每份退款申请表的退款笔数请勿超过10笔,如有超过请换页,退款总金额请单页统计。");                        subAmt += totalRefAmt;        }                        row = sheet.createRow(29);        cell = row.createCell(0);        cell.setCellStyle(cs4);        cell.setCellValue("账号:455959692203");        sheet.addMergedRegion(new CellRangeAddress(29, 29, 0, 6));                 row = sheet.createRow(30);        cell = row.createCell(0);        cell.setCellStyle(cs4);        cell.setCellValue("一共"+pageNo+"张退款表,共"+len+"笔");        sheet.addMergedRegion(new CellRangeAddress(30, 30, 0, 2));                row = sheet.createRow(31);        cell = row.createCell(0);        cell.setCellStyle(cs4);        cell.setCellValue("退款总金额:"+format.format(subAmt)+"元;扣除手续费后金额:"+format.format(subAmt*0.008)+"元");        sheet.addMergedRegion(new CellRangeAddress(31, 31, 0, 6));                sheet.setColumnWidth(0, 4*256);        sheet.setColumnWidth(1, 20*256);        sheet.setColumnWidth(2, 13*256);        sheet.setColumnWidth(3, 13*256);        sheet.setColumnWidth(4, 13*256);        sheet.setColumnWidth(5, 12*256);        sheet.setColumnWidth(6, 12*256);                wb.write(os);                return 0;    }
  相关解决方案