给自己一点时间接受自己,爱自己,趁着下午茶的时间来学习图老师推荐的Tomcat 下使用itext生成报表,过去的都会过去,迎接崭新的开始,释放更美好的自己。
【 tulaoshi.com - 编程语言 】
1.Tomcat的安装不描述.现在假如Tomcat 安装目录为C:Tomcat 5.0,新建C: /Tomcat 5.0 /webapps /pdfservlet目录.
安装JDK 安装目录C:j2sdk1.4.2_09 去http://www.lowagie.com/iText/download.html下载itext1.3.0.jar,后解压 缩,将itext-1.3.0.jar 放到C: Tomcat 5.0 webapps myServlets WEB-INF lib下(当然还有一些必须的包如servlet.jar) 和 C: j2sdk1.4.2_09 lib下.并将C:/j2sdk1.4.2_09/ lib/ itext-1.3.0.jar加到CLASSPATH中.确保能正确编译.(我也是一名JAVA初学者,我想起码的JAVA环境配置要会)
2.下面就是itext 的例子,我在网上查过关于ITEXT的贴子,大部份都是转贴,一个贴子被一帮所谓的JAVA爱好者转了又转,难得出一份原创贴,可能那些JAVA高手有点低估我们初学者的水平,来个Helloworld字符串加到PDF中就算过了,TMD那个在实际中顶个叉用.我说这些话也只是希望那些精通JAVA的人能对我们这些初学负一点责任,写一点好贴,给我们参考一下.废话不多讲,下面是代码.:
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)import java.io.IOException;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.html.HtmlWriter;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.rtf.RtfWriter;
import com.lowagie.text.Element;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.PdfPTable;
/**
* Hello World example as a Servlet.
*
* @author blowagie
*/
public class HelloWorldServlet extends HttpServlet {
/**
* Returns a PDF, RTF or HTML document.
*
* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
System.out.println("document.add(BigTable)");
// step1
Document document = new Document(PageSize.A4.rotate(), 10, 10, 10, 10); //定义纸张类型及方向,页边距
// step 1
try {
// step 2: we set the ContentType and create an instance of the corresponding Writer
response.setContentType("application/pdf");
PdfWriter.getInstance(document, response.getOutputStream());
// step3
document.open();
// step4 定义表格填充内容
String[] bogusData = { "M0065920", "SL", "FR86000P", "PCGOLD",
"119000", "96 06", "2001-08-13", "4350", "6011648299",
"FLFLMTGP", "153", "119000.00" };
int NumColumns = 12; //定义表格列数
PdfPTable datatable = new PdfPTable(NumColumns); //创建新表.
int headerwidths[] = { 9, 4, 8, 10, 8, 11, 9, 7, 9, 10, 4, 10 }; // percentage 定义表格头宽度
datatable.setWidths(headerwidths);
datatable.setWidthPercentage(100); // percentage
datatable.getDefaultCell().setPadding(3);
datatable.getDefaultCell().setBorderWidth(2);
datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
//以下是填充表头
datatable.addCell("Clock #");
datatable.addCell("Trans Type");
datatable.addCell("Cusip");
datatable.addCell("Long Name");
datatable.addCell("Quantity");
datatable.addCell("Fraction Price");
datatable.addCell("Settle Date");
datatable.addCell("Portfolio");
datatable.addCell("ADP Number");
datatable.addCell("Account ID");
datatable.addCell("Reg Rep ID");
datatable.addCell("Amt To Go ");
datatable.setHeaderRows(1); // this is the end of the table header
datatable.getDefaultCell().setBorderWidth(1);
for (int i = 1; i 750; i++) {
if (i % 2 == 1) {
datatable.getDefaultCell().setGrayFill(0.9f);
}
for (int x = 0; x NumColumns; x++) {
datatable.addCell(bogusData[x]);
}
if (i % 2 == 1) {
datatable.getDefaultCell().setGrayFill(0.0f);
}
}
document.add(datatable); //加载新表
}
catch(DocumentException de) {
de.printStackTrace();
System.err.println("document: " + de.getMessage());
}
// step 5: we close the document (the outputstream is also closed internally)
document.close();
}
}
正确编译它,我试过了没问题,我itext REV.为1.3.0 放到C: Tomcat 5.0 webapps pdfservlet WEB-INF classes
2.由于是Servlet 我们创建一个WEB.XML 放到C: Tomcat 5.0 webapps pdfservlet WEB-INF下
?xml version="1.0" encoding="ISO-8859-1"?
!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2.2.dtd"
web-app
servlet
servlet-name
HelloWorldServlet
/servlet-name
servlet-class
HelloWorldServlet
/servlet-class
/servlet
servlet-mapping
servlet-nameHelloWorldServlet/servlet-name
url-pattern/HelloWorldServlet/url-pattern
/servlet-mapping
/web-app
3.重新启动TOMCAT
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)http://IP地址:8080/pdfservlet/HelloWorldServlet
如果顺利的话你可以看到很漂亮的PDF报表
来源:http://www.tulaoshi.com/n/20160219/1609906.html
看过《Tomcat 下使用itext生成报表》的人还看了以下文章 更多>>