Posted on 2011-10-07 14:16
中天下 閱讀(1946)
評論(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();
}