导出格式为WORD文档

时间:2022-10-03 05:43:41
HttpServletResponse response = ServletActionContext.getResponse();
            try {
                FileOutputStream outputStream =new FileOutputStream("D:\\b.doc");
                Document document=new Document(PageSize.A4, 50, 50, 50, 50);
//                PdfWriter.getInstance(document, outputStream);
                RtfWriter2.getInstance(document, outputStream);
                
                document.open();
                
                //定义字体
                BaseFont bfChinese =BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
                Font fontCN = new Font(bfChinese,10,Font.BOLD);
                
                Paragraph paragraph =new Paragraph("资产管理",fontCN);
                paragraph.setAlignment(Element.ALIGN_CENTER);
                document.add(paragraph);
                
                List<Map> list = selectDataService.queryForList("select a.column_name,a.comments from user_col_comments a where a.table_name ='T_ASSET' and a.comments is not null");
                //字段名存入colNameList
                List colNameList = new ArrayList();
                //colMap中字段名为键,备注名为值
                HashMap colMap = new HashMap();
                for (int i = 0; i < list.size(); i++) {
                    Map map = list.get(i);
                    colNameList.add(map.get("COLUMN_NAME").toString());
                    colMap.put(map.get("COLUMN_NAME").toString(), map.get("COMMENTS").toString());
                }
                PdfPTable table =new PdfPTable(colNameList.size());
                
                for (int i = 0; i < colNameList.size(); i++) {
                    table.addCell(colMap.get(colNameList.get(i)).toString());
                }
                
                //添加数据行
                String sql1 = "select a.id \"id\",a.num \"num\",a.name \"name\",t.name \"typeid\",a.status \"status\"  ,a.buydate \"buydate\",u.realname \"userid\",a.price \"price\",w.name \"factory\",a.content \"content\" from t_asset a ,t_assettype t ,t_factory w,t_user u where a.typeid = t.id(+) and  a.factory = w.id(+) and a.userid = u.id(+)";
                List<Map> datalist = selectDataService.queryForList(sql1);
                for (int i = 0; i < datalist.size(); i++) {
                    Map map = datalist.get(i);
                    for (int j = 0; j <colNameList.size() ; j++) {
                        table.addCell(map.get(colNameList.get(j)).toString());
                    }
                }
                
                document.add(table);
                document.close();
                outputStream.close();
            }  catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            FileUtil.downloadFile(response,"D:\\b.doc", "资产列表.doc");