日期:2014-05-17 浏览次数:20807 次
public static void createReport(HttpServletRequest request, HttpServletResponse response, List<Diary> list) throws Exception{ String title = null; if(list.size() != 0){ title = list.get(list.size() - 1).getCreateDate() + " ~ " + list.get(0).getCreateDate() + "日志"; } System.out.println(title); //设置文件响应信息 String showFileName =URLEncoder.encode(title + ".doc", "UTF-8"); showFileName = new String(showFileName.getBytes("iso8859-1"), "gb2312"); //定义输出类型 response.reset(); response.setContentType("application/vnd.ms-word;charset=utf-8"); response.setHeader("Pragma", "public"); response.setHeader("Cache-Control", "max-age=30"); response.setHeader("Content-disposition", "attachment; filename="+ new String(showFileName.getBytes("gb2312"), "iso8859-1")); Document doc=new Document(PageSize.A4,50,50,50,50); ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream(); RtfWriter2.getInstance(doc, byteArrayOutputStream); doc.open(); //添加标题 Paragraph paragraph=new Paragraph(title,ReportFontFactory.getFontChinese(Font_Type.TITLE)); paragraph.getFont().setColor(230, 20, 220); paragraph.getFont().setSize(18); paragraph.setAlignment(Paragraph.ALIGN_CENTER); doc.add(paragraph); for(Diary d : list){ doc.add(new Paragraph()); Paragraph p = new Paragraph(" 标题:" + d.getTitle()+" 类型:" + d.getType()+" 天气:" + d.getWeather() +" 日期:" + d.getCreateDate()); p.getFont().setStyle(Font.BOLD); p.getFont().setColor(100, 0, 0); doc.add(p); doc.add(new Paragraph(" " + d.getContent())); } doc.close(); ServletOutputStream outputStream=response.getOutputStream(); byteArrayOutputStream.writeTo(outputStream); outputStream.flush(); outputStream.close(); }