封装jasperreport的接口,通过jasperreport来生成excel,pdf,html文件,也可生成StringBuffer在jsp中显示出来。ireport一般用于界面设计
解决方案 »
- JS怎么得到某天所在周的起始日期
- 到底什么是JavaBean
- Error listenerStart
- SQLServerException大家帮忙找找错呀
- Hibernate 中的 merge方法何解?
- 求助:j2ee编译出现问题??
- 小女子求救:谁能给一个用普通的类测试已经用jboss部署的ejb的例子啊?
- 很菜很菜的问题 - javabean怎么样连数据库啊?
- 关于Struts的一个小问题,请指教
- 为什么我的ejb容器初始化的时候会出现java.lang.ClassNotFoundException: ptpchatbean.nptpchatHelper异常,如何解决啊?(这是本人第一个ejb程序)
- XML Notepad 2007 v2.5如何使用
- 急问Hibernate的多对多关系?
import java.io.IOException;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;import java.awt.Color;
import java.io.File;import com.lowagie.text.Cell;
import com.lowagie.text.Chapter;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.List;
import com.lowagie.text.ListItem;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Section;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.PdfWriter;
/**
* MyEclipse Struts
* Creation date: 11-28-2007
*
* XDoclet definition:
* @struts.action path="/pdf" name="pdfForm" input="/pdf.jsp" scope="request" validate="true"
*/
public class PdfAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String fileName = "C:\\Program Files\\apache-tomcat-6.0.14\\apache-tomcat-6.0.14\\webapps\\strutsireport\\temp.pdf";
File file = new File(fileName);
FileOutputStream out = null;
try{
//创建一个空的pdf文件
file.createNewFile();
//(1)实例化文档对象
//第一个参数是页面大小。接下来的参数分别是左,右,上和下页的边距
Document document = new Document(PageSize.A4,50,50,50,50);
//(2)创建写入器。第一个参数是对文档对象的引用
//第二个参数是输出的文件,将out和document连接起来
out = new FileOutputStream(file);
PdfWriter writer = PdfWriter.getInstance(document, out);
//打开文档准备写内容
document.open();
//(3)下面创建章节对象
//首先创建段落对象,作为章节的标题,FontFactory用于指定段落的字体
Font font = FontFactory.getFont(FontFactory.HELVETICA,18,Font.BOLDITALIC,new Color(0,0,255));
Paragraph chapter1_title = new Paragraph("Chapter 1",font);
//创建了一个章节对象标题为chapter1
Chapter chapter1 = new Chapter(chapter1_title,1);
//将编号级别设为0就不会在页面上显示编号
chapter1.setNumberDepth(0);
//(4)创建小结对象
font = FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD,new Color(255,0,0));
//创建小结对象的标题
Paragraph sectionl_title1 = new Paragraph("Section 1 in Chapter1",font);
//创建一个小结对象,标题为“This is Section1 in Chapter1”,属于chapter1
Section section1 = chapter1.addSection(sectionl_title1);
//(5)往小结中添加内容
Paragraph text = new Paragraph("This is the first text in section 1 of chapter1.");
section1.add(text);
text = new Paragraph("Following is a 5*5 table:");
//(6)往小结中写表格
Table table = new Table(5,5);//创建表格对象
table.setBorderColor(new Color(220,255,100));// 设置表格边框颜色
//设置单元格的边距间隔等
table.setPadding(1);
table.setSpacing(1);
table.setBorderWidth(1);
Cell cell = null;//单元格创建
//添加表头
// for(int colnum = 0;colnum<5;colnum++){
// cell = new Cell("header-"+colnum);
// table.addCell(cell);
// }
cell = new Cell("高");
table.endHeaders();
//添加表的内容
for(int rowNum=1;rowNum<5;rowNum++){
for(int colNum=0;colNum<5;colNum++){
cell = new Cell("value-"+rowNum+"-"+colNum);
table.addCell(cell);
}
}
section1.add(table);//将表格对象添加到小结对象中
//(7)添加列表
//列表包含一定数量的ListItem。可以对列表进行编号,也可以不编号
//将第一个参数设置为true表明想创建一个进行编号的列表
//将第二个参数设置为true表示列表采用字母进行编号,为false则用数字进行编号;
//将第三个参数为列表内容与编号之间的距离
List list = new List(true,false,20);
ListItem item = new ListItem("First item of List");
list.add(item);
item = new ListItem("Second item of List");
list.add(item);
item = new ListItem("Third item of List");
list.add(item);
section1.add(list);//将列表对象添加大小结中
/**
//(8)添加中文
//允许在PDF中写入中文,把字体文件放在classpath中
//simfang.ttf 是一个中文字体文件
BaseFont bfChinese = BaseFont.createFont("simfang.ttf",BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
//中文大小为20,加粗
font = new Font(bfChinese,20,Font.BOLD);
text = new Paragraph("PDF中文测试",font);
section1.add(text);
*/
//(9)将章节对象加入到文档中
document.add(chapter1);
//(10) 关闭文档
document.close();
System.out.println("PDF文件生成成功,PDF文件名为:"+file.getAbsolutePath() );
}catch(DocumentException e){
System.out.println("PDF文件生成失败");
e.printStackTrace();
}catch(IOException ee){
System.out.println("PDF文件生成失败");
ee.printStackTrace();
}finally{
if(out!=null){
try{
out.close();
}catch(IOException e){
e.printStackTrace();
}
}
} return mapping.findForward("success");
}
}