1、文件模版准备

POI根据模版生成Excel数据并下载
2、接下来是生成Excel数据
//获取excel模版路径
String filePath=“com”+File.separator+“deom”+File.separator+“excel”+File.separator+“test”+File.separator+“export”+File.separator+“excel”+File.separator+“ALLREPORT.xls”;
String modelFile = PathUtil.getClassesFilePath(filePath);//得到模版绝对路径
File file = new File(modelFile );//获取文件对象
FileInputStream inputStream = new FileInputStream(file);//获取流
//创建工作薄
HSSFWorkbook workbook= new HSSFWorkbook(inputStream);
//创建目录
String dir=String.valueOf(new Date().getTime());
String dirTemp=exportPath+"\"+dir;//exportPath是配置盘符路径入C:\
file1 = new File(dirTemp);
if (!file1.exists()){
Boolean mk=file1.mkdirs();// true
if (!mk){
hrow new RuntimeException(“目录创建失败…”);
}
}
//创建文件
String timeStr=new SimpleDateFormat(“yyyyMMddHHmm”).format(new Date());
String fileName=fillUnit.getFid()+"-001"+"-"+timeStr+".xls";
File file2 = new File(dirTemp+"\"+fileName);
f(!file2.exists()){
file2.createNewFile();
}
//文件输出流
OutputStream outputStream=new FileOutputStream(file2);
//获取excel模版具体的工作薄
Sheet sheet=workbook.getSheet(“货币资金清查登记表”);
Row row=sheet.getRow(7-1);//第7行,从0开始
Cell cell=row.getCell(2);第二列,从0开始
cell.setCellValue(value);//设置值

//关闭流
outputStream.close();
inputStream.close();
POI根据模版生成Excel数据并下载

 下载方法
 private void download(String  filePath, String title, HttpServletRequest request, HttpServletResponse response) {
    OutputStream out = null;
    InputStream inputStream=null;
    // 读取文件
    File file = null;
    try {
        file=new File(filePath);
        if (!file.exists()){
            response.setHeader("content-type", "text/html;charset=UTF-8");
            PrintWriter print=response.getWriter();
            print.println("<p>没有需要下载的数据</p>");
            //print.println("<a href=\"javascript:parent.$win.window(\'close\');\">关闭</a>");
            print.flush();
            print.close();
            throw  new RuntimeException("没有找到下载的文件");
        }
        StringBuilder fileName = new StringBuilder();
        String userAgent = request.getHeader("User-Agent");
        // 针对IE或者以IE为内核的浏览器文件名:
        if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
            fileName.append(java.net.URLEncoder.encode(title, "UTF-8"));
        } else {
            // 非IE浏览器的处理:
            fileName.append(new String(title.getBytes("UTF-8"), "iso8859-1"));
        }
        fileName.append(".xls");
        out = response.getOutputStream();
        response.reset();
        response.setContentType("multipart/form-data");
        String headStr = "attachment; filename=\"" + fileName.toString() + "\"";
        response.setHeader("Content-Disposition", headStr);

        inputStream = new FileInputStream(file);
        int b;
        // 写文件
        while ((b = inputStream.read()) != -1) {
            out.write(b);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try{
            if (out!=null){
                out.close();
            }
        }catch (IOException e) {
            e.printStackTrace();
        }
        try{
            if (inputStream!=null){
                inputStream.close();
            }
        }catch (IOException e) {
            e.printStackTrace();
        }
        file.delete();//删除文件
    }
}

相关文章: