posts - 0, comments - 0, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          Java Html轉Word

          Posted on 2011-10-07 14:16 中天下 閱讀(1951) 評論(0)  編輯  收藏

          public void createDocContext(HttpServletRequest request,
             HttpServletResponse response) throws DocumentException, IOException {
            String info = request.getParameter("infoTxt");

            // 處理中文亂碼
            if (info != null) {
             info = new String(info.getBytes("iso8859-1"), "UTF-8");
            }

            
            // 生成導出文件的文件名
             String filePath = "QrySaleMainTestFileMS" + ".doc";
             response.setHeader("Content-disposition", "attachment;filename="
               + filePath);
             response.setContentType("application/vnd.ms-excel");
            OutputStream out = response.getOutputStream();

            //POI導出Word
             // String content = "<html>" + "<head>你好</head>" + "<body>" + "<table>"
            // + "<tr>" + "<td>信息1</td>" + "<td>信息2</td>" + "<td>t3</td>"
            // + "<tr>" + "</table>" + "</body>" + "</html>";
            //
            // byte b[] = content.getBytes();
            
            byte b[] = info.getBytes();
            ByteArrayInputStream bais = new ByteArrayInputStream(b);
            POIFSFileSystem poifs = new POIFSFileSystem();
            DirectoryEntry directory = poifs.getRoot();
            DocumentEntry documentEntry = directory.createDocument("WordDocument",
              bais);
            poifs.writeFilesystem(out);
            bais.close();

            out.close();

            // itext導出word
            // // 設置紙張大小
            // Document document = new Document(PageSize.A4);
            //
            // // 建立一個書寫器(Writer)與document對象關聯,通過書寫器(Writer)可以將文檔寫入到磁盤中
            // RtfWriter2.getInstance(document, response.getOutputStream());
            //
            // document.open();
            //
            // // 設置中文字體
            // BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",
            // "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            //
            // // 標題字體風格
            // Font titleFont = new Font(bfChinese, 12, Font.BOLD);
            //
            // // 正文字體風格
            // Font contextFont = new Font(bfChinese, 10, Font.NORMAL);
            //
            // Paragraph title = new Paragraph("主套餐評估報告");
            //
            // // 設置標題格式對齊方式
            // title.setAlignment(Element.ALIGN_CENTER);
            //
            // // title.setFont(titleFont);
            //
            // document.add(title);
            //  
            // Paragraph context = new Paragraph(info);
            //
            // // 正文格式左對齊
            // context.setAlignment(Element.ALIGN_LEFT);
            //
            // // context.setFont(contextFont);
            //
            // // 離上一段落(標題)空的行數
            // context.setSpacingBefore(5);
            //
            // // 設置第一行空的列數
            // context.setFirstLineIndent(20);
            //
            // document.add(context);
            //
            // document.close();

           }


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 阜平县| 乡宁县| 临海市| 淳安县| 江都市| 吴堡县| 时尚| 和顺县| 泸定县| 潞城市| 吉首市| 祥云县| 泰州市| 大余县| 静乐县| 霍山县| 沂源县| 潢川县| 微山县| 建平县| 读书| 南岸区| 临武县| 铁岭县| 永宁县| 精河县| 日喀则市| 南充市| 虎林市| 安龙县| 江西省| 嘉黎县| 蚌埠市| 尼木县| 横峰县| 修文县| 泾阳县| 涞源县| 冀州市| 滁州市| 霍邱县|