Jfinal框架下现实Excel导出

本文介绍了一个使用Java实现从数据库查询数据并导出到Excel文件的示例。该示例利用MyBatis进行数据库操作,通过HSSFWorkbook创建Excel工作簿,再将查询结果填充到Excel表格中,最后提供文件下载功能。
摘要由CSDN通过智能技术生成

public void download() throws UnsupportedEncodingException{

        String[] title={"id","时间","银行卡号","开户人","联系方式","申请金额","实际可得","备注"}; 


        String sql="select a.*,b.name,b.tel,b.role,c.area "
                + "from t_Withdrawal_form a left join (t_wap_user b left join t_branch c on b.id=c.user_id)on a.user_id=b.id where b.role in (1,2,4) ";

         System.out.println(sql);
          List<Record> list=Db.find(sql);

        //创建工作表
        HSSFWorkbook workbook=new HSSFWorkbook();
        //创建一个工作表sheet
        HSSFSheet sheet=workbook.createSheet();
        //创建第一行
        HSSFRow row=sheet.createRow(0);
        //创建单元格
        HSSFCell cell=null;
        //插入第一行数据
        for (int i = 0; i < title.length; i++) {
            cell=row.createCell(i);
            cell.setCellValue(title[i]);
        }
        //追加数据


        for (int i = 0; i <=list.size()-1; i++) {
              row = sheet.createRow((int) i+1 );  
              Record tx = (Record) list.get(i);  
               // 第四步,创建单元格,并设置值  

               row.createCell(0).setCellValue(i+1);
               row.createCell(1).setCellValue(tx.get("txtime").toString());
               row.createCell(2).setCellValue(tx.get("card").toString());
               row.createCell(3).setCellValue(tx.get("cname").toString());
               row.createCell(4).setCellValue(tx.get("tel")+"/"+tx.get("name"));
               row.createCell(5).setCellValue((tx.getInt("amount")));
               row.createCell(6).setCellValue((tx.getInt("amount")-(tx.getInt("amount")*0.1)));
               row.createCell(7).setCellValue("  ");

        }

String realPath =getRequest().getRealPath(“”);
String fileName = “提现订单”+abc.getday()+”.xls”;
File file1 = new File(realPath + “//ddt//tempPath//”);//导出文件存放的位置
if (!file1.exists()) {
file1.mkdirs();
}
realPath = realPath + “//ddt//tempPath//” + fileName;

        File file=new File(realPath);
        try{
            file.createNewFile();
            //将Excel内容存盘
            FileOutputStream stream = FileUtils.openOutputStream(file);
            workbook.write(stream);
            stream.close();
        }catch(IOException e){
            e.printStackTrace();
        }






        try {
               // 下载
               FacesContext ctx = FacesContext.getCurrentInstance();
               String contentType = "application/x-download";

// HttpServletResponse response = (HttpServletResponse) ctx
// .getExternalContext().getResponse();
HttpServletResponse response=getResponse();
response.setContentType(contentType);
response.setHeader(“Content-Disposition”, “attachment;filename=”
+ new String(fileName.getBytes(“gb2312”), “ISO8859-1”));
// HttpServletRequest request1 = (HttpServletRequest) FacesContext
// .getCurrentInstance().getExternalContext().getRequest();
ServletOutputStream out = response.getOutputStream();
byte[] bytes = new byte[0xffff];
InputStream is = new FileInputStream(new File(realPath));
int b = 0;
while ((b = is.read(bytes

                       , 0, 0xffff)) > 0) {
                out.write(bytes, 0, b);
               }
               is.close();
               out.flush();
               //ctx.responseComplete();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }






        redirect("/userTx/list");
    }

导出Excel

function onExcel(){ window.location.href="
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值