Jfinal框架下现实Excel导出

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 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="

你可能感兴趣的:(excel,download)