封装jasperreport的接口,通过jasperreport来生成excel,pdf,html文件,也可生成StringBuffer在jsp中显示出来。ireport一般用于界面设计

解决方案 »

  1.   

    package action;import java.io.FileOutputStream;
    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");
    }
    }
      

  2.   

    现在没有办法输出中文的,正在研究,有解决方案的话,告诉一声,这只是代码,建好web工程以后把ireport的包导入就可以运行了