標籤:
/** * <p>expWordByItext方法主要用於-採用itext匯出文檔到word.</p> * <p>優缺點-可將隨意控制產生word的格式,但代碼控制上比採用模板的方式稍微複雜,可設定頁首頁尾等其他文件屬性.</p> * <p>第三方jar-itext-2.1.7.jar、itext-rtf-2.1.7.jar.<br> * import com.lowagie.text.Document;<br> * import com.lowagie.text.DocumentException;<br> * import com.lowagie.text.Element;<br> * import com.lowagie.text.HeaderFooter;<br> * import com.lowagie.text.Paragraph;<br> * import com.lowagie.text.Phrase;<br> * import com.lowagie.text.Rectangle;<br> * import com.lowagie.text.rtf.RtfWriter2; * </p> * <p> * 山河戀夢 Jul 30, 2015 - 5:07:55 PM * </p> * @param content * @param filePath * @throws FileNotFoundException * @throws DocumentException */private static void expWordByItext(String content, String filePath) throws FileNotFoundException, DocumentException {Document doc = new Document(PageSize.A4);RtfWriter2.getInstance(doc, new FileOutputStream(filePath));doc.open();Paragraph p = new Paragraph(content);doc.add(p);doc.close();}
/** * <p> * dowmload方法主要用於-下載. * </p> */public String dowmload(HttpServletRequest request, HttpServletResponse response) throws IOException {String fileName = "匯出測試.doc";String fullPath = "D:/" + fileName;File file = new File(fullPath);InputStream in = new BufferedInputStream(new FileInputStream(fullPath));// ContentType參見http://tool.oschina.net/commonsresponse.setContentType("application/octet-stream");fileName = new String(fileName.replaceAll(" ", "").getBytes("gb2312"), "ISO8859-1");response.addHeader("Content-Disposition", "attachment;filename=" + fileName);response.addHeader("Content-Length", "" + file.length());// 方式一OutputStream out = new BufferedOutputStream(response.getOutputStream());byte[] buffer = new byte[in.available()];in.read(buffer);out.write(buffer);in.close();out.flush();out.close();// 方式二String line = null;OutputStreamWriter writer = new OutputStreamWriter(response.getOutputStream(), "UTF-8");BufferedReader reader = new BufferedReader(new InputStreamReader(in));while ((line = reader.readLine()) != null) {writer.write(line);writer.write("\r\n");}in.close();writer.flush();writer.close();return "download";}
java產生word文檔【二】