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();

           }


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


          網站導航:
           
          主站蜘蛛池模板: 精河县| 横峰县| 昌乐县| 黔西县| 资兴市| 乌恰县| 华容县| 田林县| 四子王旗| 潜江市| 韶关市| 蒙自县| 奉新县| 凤山县| 迭部县| 灵台县| 朔州市| 利津县| 乐至县| 施秉县| 西乡县| 永宁县| 贵州省| 兰考县| 新邵县| 定结县| 屏山县| 九江市| 西城区| 六枝特区| 江门市| 仪征市| 腾冲县| 农安县| 寻甸| 五指山市| 大同县| 孟州市| 渝中区| 崇信县| 望都县|