Java POI导出Word表格并使用输出流下载文件弹出打开保存框

/**
     * 导出Word表格
     * @author Saffi
     */
    @RequestMapping("exportWord.action")
    public void exportWord(HttpServletResponse response) throws IOException {
        try {
        List<Entity> docuList =entityService.listDocumentaryForProduct();
        // 获取当前时间
        Date date = new Date();
        // 将时间格式定义为年-月-日
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        // 将时间格式转换为String类型
        String createtime = format.format(date);
           //创建一个Word文件
         XWPFDocument xdoc = new XWPFDocument();  
            XWPFParagraph xp = xdoc.createParagraph();  
            XWPFRun r1 = xp.createRun();  
            //设置表格标题
            r1.setText("发货核对表");  
            r1.setFontFamily("微软雅黑");  //设置字体
            r1.setFontSize(12);  //设置字体大小
            r1.setTextPosition(10);  
            r1.setBold(true);  
            xp.setAlignment(ParagraphAlignment.CENTER);  
        //创建一个表格
            XWPFTable xTable = xdoc.createTable(docuList.size()+1, 8);  
            CTTbl ttbl = xTable.getCTTbl();  
            CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl  
                    .getTblPr();  
            CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr  
                    .addNewTblW();  
            tblWidth.setW(new BigInteger("11000"));  
            tblWidth.setType(STTblWidth.DXA);  
            //创建标题
            exportWordUtil wutil = new exportWordUtil();
            int i = 0;  
            xTable.getRow(i).setHeight(380);  
            wutil.setCellText(xdoc, xTable.getRow(i).getCell(0), "日期", "FFFFFF ",  
                    wutil.getCellWidth(0));  
            wutil.setCellText(xdoc, xTable.getRow(i).getCell(1), "姓名", "FFFFFF",  
                    wutil.getCellWidth(1));  
            wutil.setCellText(xdoc, xTable.getRow(i).getCell(2), "单号", "FFFFFF",  
                    wutil.getCellWidth(2));  
         
            i++;  

            //创建行
            for (int i2 = i; i2 <= docuList.size(); i2++) {
                int i1=i2-1;
                                
                    wutil.setCellText(xdoc, xTable.getRow(i2).getCell(0), docuList.get(i1).getRecorddate(), "FFFFFF",wutil.getCellWidth(i));
                    wutil.setCellText(xdoc, xTable.getRow(i2).getCell(1), docuList.get(i1).getName(),"FFFFFF", wutil.getCellWidth(i));
                    wutil.setCellText(xdoc, xTable.getRow(i2).getCell(2), docuList.get(i1).getNumber(), "FFFFFF",wutil.getCellWidth(i));
           
                
            }
            // 设置Word文件名,并以中文进行编码
                String codedFileName = new String("Word文件名".getBytes("gbk"), "iso-8859-1");
                response.setHeader("Content-Disposition", "attachment;filename=" + codedFileName + createtime+".docx");
                // 响应类型,编码
                response.setContentType("application/octet-stream;charset=UTF-8");
                // 形成输出流
                OutputStream osOut = response.getOutputStream();
                
                xdoc.write(osOut);
                // 刷新此输出流并强制将所有缓冲的输出字节被写出
                osOut.flush();
                // 关闭流
                osOut.close();        
        
        } catch (Exception e) {
            e.printStackTrace();
            response.sendRedirect("error.action");
            log.error("系统错误", e.fillInStackTrace());
        }
    }

原文地址:https://www.cnblogs.com/shoose/p/7744510.html