posts - 0, comments - 0, trackbacks - 0, articles - 1
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

Java Html转Word

Posted on 2011-10-07 14:16 中天下 阅读(1945) 评论(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();

 }


只有注册用户登录后才能发表评论。


网站导航: