1.请教iText生成word文档的小问题
使用iText生成word,struts2框架,使用iText-2.1.7.jar和iText-rtf-2.1.7.jar 方法如下:import java.io.*;import java.awt.Color;import com.lowagie.text.*;import com.lowagie.text.rtf.RtfWriter2;import com.lowagie.text.rtf.headerfooter.*;import com.lowagie.text.rtf.field.*;import com.lowagie.text.rtf.table.*;import com.lowagie.text.rtf.style.RtfFont;public class RtfClass { public static String rtf() { com.lowagie.text.Document document = new com.lowagie.text.Document(); try {System.out.println("try1"); RtfWriter2.getInstance(document, newFileOutputStream("C:\rhDocs\testRTFdocument.rtf")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } document.open(); try {System.out.println("try2"); document.add(new Paragraph("Hello World!")); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } document.close(); return "ok"; }}我用这个生成绝对不会有问题。
2.可以把你用IText生成word文档例子发给我吗
你好,很意外的收到你的求助,其实导出word,很少有人使用IText,不过我是一个例外,呵呵。
对于IText,我也仅仅是自学认识一些,并试着使用它,在导出的上,与大多数网络上的做法不同,先告诉你我的做法的基本思路,然后我再通过邮箱传给你。1)在我搭建的工程中,一般除了主工程以外,还会有一个配置文件夹,用于存放配置文件.2)先将表格以XML文件的形式保存在配置文件中,主要模仿html中的table,保留table、tr、td等属性。
3)在项目启动的时候,加入监听,去加载xml文件。当你在页面需要点击按钮需要使用的时候,在后台的action(我的项目框架使用的是structs+jdbc)中解析xml,并将数据封装到tablebean中4)最后在实现类中,解析tablebean,使用IText内部的合并行列等方法,对xml表格进行完全解析。
这样开发有一个好处,也有一个不好的地方,好处是你想要的开发效果,你都可以根据自己的需求对后台进行改造,不像有些依赖报表引擎等,不能达到自己满意的效果。坏处是,由于可借鉴资料基本没有,你能够依赖的,只有自己,所以在前期的开发效率上,会比较耗时,特别是项目紧急的时候,经不起失败的考验。
例子和jar包,我整理一份后会发给你了。
3.java itext 生成word 如何控制word 横向和分栏,可以实现么
要加一些文本到一个矩形框的内部,希望文本到达右边界时自动换行。
超出矩形部分将不显示,可以通过ColumnText类实现。举个例子:为显示一个指定的短句在坐标(100, 300)和(200,500)间的矩形内居中,使用下面的代码:PdfContentByte cb =writer.DirectContent;ColumnText ct = new ColumnText(cb);ct.setSimpleColumn(phrase,60, 300, 100, 500, 15, Element.ALIGN_CENTER);ct.go();。
4.java itext 导出word 设置字符边框
亲,这个是我给你写的设置边框和背景色的例子,请参考package org.kodejava.example.itextpdf;import com.itextpdf.text.BaseColor;import com.itextpdf.text.Document;import com.itextpdf.text.DocumentException;import com.itextpdf.text.Phrase;import com.itextpdf.text.pdf.PdfPCell;import com.itextpdf.text.pdf.PdfPTable;import com.itextpdf.text.pdf.PdfWriter;import java.io.FileNotFoundException;import java.io.FileOutputStream;public class TableCellBorderColor { public static void main(String[] args) { Document document = new Document(); try { PdfWriter.getInstance(document, new FileOutputStream("TableCellBorder.doc")); document.open(); PdfPTable table = new PdfPTable(3); PdfPCell cell1 = new PdfPCell(new Phrase("Cell 1")); cell1.setUseBorderPadding(true); // // Setting cell's border width and color // cell1.setBorderWidth(5f); cell1.setBorderColor(BaseColor.BLUE); table.addCell(cell1); PdfPCell cell2 = new PdfPCell(new Phrase("Cell 2")); cell2.setUseBorderPadding(true); // // Setting cell's background color // cell2.setBackgroundColor(BaseColor.GRAY); // // Setting cell's individual border color // cell2.setBorderWidthTop(1f); cell2.setBorderColorTop(BaseColor.RED); cell2.setBorderColorRight(BaseColor.GREEN); cell2.setBorderColorBottom(BaseColor.BLUE); cell2.setBorderColorLeft(BaseColor.BLACK); table.addCell(cell2); PdfPCell cell3 = new PdfPCell(new Phrase("Cell 3")); cell3.setUseBorderPadding(true); // // Setting cell's individual border width // cell3.setBorderWidthTop(2f); cell3.setBorderWidthRight(1f); cell3.setBorderWidthBottom(2f); cell3.setBorderWidthLeft(1f); table.addCell(cell3); table.completeRow(); document.add(table); } catch (DocumentException | FileNotFoundException e) { e.printStackTrace(); } finally { document.close(); } }}希望可以帮助到你,记得采纳哦。
5.java 代码生成word文件
没定格式的话可以再jsp页面添加这段:
<%
response.reset();
response.setContentType("application/vnd.ms-excel;charset=utf-8");
java.util.Date date = new Date();
DateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");
String Starttime = df.format(date).toString();
response.setHeader("Content-disposition", "attachment; filename=GroupingInformation_"+ Starttime + ".doc");
%>
如果要定好格式的话,只有自己去看poi比较复杂。(但是用习惯了就没大问题了。)
poi后台:
导出第三句是格式,这个自己去试试。
转载请注明出处51数据库 » itext生成word文档