1,pdf生成。pdf的生成要利用iText。 package pdfOutPut; import java.awt.Color; import java.io.FileOutputStream; import java.util.Date; import java.io.File; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; //以下是iText包中的类 import com.lowagie.text.Cell; import com.lowagie.text.Document; import com.lowagie.text.Element; import com.lowagie.text.Font; import com.lowagie.text.PageSize; import com.lowagie.text.Paragraph; import com.lowagie.text.Phrase; import com.lowagie.text.Rectangle; import com.lowagie.text.Table; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.PdfWriter; public class RepeatingTable { //生成的pdf名称,以时间命名 private static String pdfName = getNowTime() + ".pdf"; //当前的项目路径 private String PDF_PATH = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath(); //get方法,获取pdf名称 public String getPdfName() { return pdfName; } public RepeatingTable() { //首先生成一个document对象 Document document = new Document(PageSize.A4); try { //我准备把pdf文件放在项目文件夹中的WEB-INF//resources/PDF/ 下面 if (PDF_PATH.indexOf("WEB-INF") > 0) { PDF_PATH = PDF_PATH.substring(0, PDF_PATH.indexOf("/WEB-INF/")) + "/resources/PDF/"; } System.out.println(PDF_PATH + "_PDF_PATH"); File file = new File(PDF_PATH); //利用iText的PdfWriter对象生成pdf文件,文件路径与名称如:WEB-INF//resources/PDF/2010-06-28 10:10:10.pdf PdfWriter.getInstance(document, new FileOutputStream(file + "/" + pdfName)); //一些简单的pdf文件信息,这是对生成的pdf文件右键属性看的一些信息 document.addAuthor("ZhouFly@osunda.com"); document.addSubject("资源列表"); //打开pdf文件,准备写操作 document.open(); //生成一个4列的表格 int row = 4; Table datatable = new Table(row); //定义没列宽度 int headerwidths[] = {10, 20, 20, 50}; datatable.setWidths(headerwidths); datatable.setWidth(100); //边线宽度 datatable.setPadding(3); //修改编码,由于iText不支持亚洲语言,需要添加亚洲语言包iTextAsian.jar 可以在google上自己搜索,然后导入即可 BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); //字体的一个设置 Font FontChinese = new Font(bfChinese, 24, Font.BOLD); //生成一个小单元 此单元占4列 其实这时生成表头,表头以endHeaders()方法结束 Cell cell = new Cell(new Phrase( "设备报表", FontChinese)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setLeading(30); cell.setColspan(row); cell.setBorder(Rectangle.NO_BORDER); cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0)); datatable.addCell(cell); datatable.setBorderWidth(2); datatable.setAlignment(1); FontChinese = new Font(bfChinese, 16, Font.BOLD); //由于不支持亚洲语言,每词写入内容的时候都要采用FontChinese编码 datatable.addCell(new Phrase("资源Id", FontChinese)); datatable.addCell(new Phrase("资源描述", FontChinese)); datatable.addCell(new Phrase("资源名称", FontChinese)); datatable.addCell(new Phrase("资源内容", FontChinese)); datatable.endHeaders(); datatable.setBorderWidth(2); //重新设置字体格式 FontChinese = new Font(bfChinese, 12, Font.NORMAL); //数据列表 List ar = getAllResource(); for (int i = 0; i < ar.size() - 1; i = i + 4) { datatable.setAlignment(Element.ALIGN_LEFT); datatable.addCell(new Phrase(ar.get(0 + i).toString(), FontChinese)); datatable.addCell(new Phrase(ar.get(1 + i).toString(), FontChinese)); datatable.addCell(new Phrase(ar.get(2 + i).toString(), FontChinese)); datatable.addCell(new Phrase(ar.get(3 + i).toString(), FontChinese)); } FontChinese = new Font(bfChinese, 14, Font.NORMAL); document.add(new Paragraph("报表生成时间:" + getNowTime(), FontChinese)); document.add(datatable); } catch (Exception e) { e.printStackTrace(); } document.close(); } //全局的获取当前时间 public static String getNowTime() { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String now = formatter.format(new Date()); return now; } //生成伪数据测试 public static List getAllResource(){ List l = new ArrayList(); int i = 64; while(i>0){ l.add(i); i--; } return l; } } 2,pdf的导出。pdf的导出我用到了一个叫icefaces的jsf框架。也可以用javascript写一个导出的方法。 1)在jsp页面上,代码如下: <ice:outputResource id="pdfbutton" fileName="#{PdfResources.pdfName}" mimeType="application/pdf" resource="#{PdfResources.pdfResourceDynFileName}" shared="false" label="下载" type="button"/> 2)java后台代码: package pdfOutPut; import com.icesoft.faces.context.Resource; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.Serializable; import java.util.Date; import javax.faces.FacesException; import javax.faces.context.ExternalContext; import javax.faces.context.FacesContext; public class PdfResources{ private Resource pdfResourceDynFileName; //这个目录就是我们存放pdf文件的目录 public static final String RESOURCE_PATH = "/resources/PDF/"; public static String pdfName = "下载"; public String getPdfName() { return pdfName; } public static void setPdfName(String pdfName) { PdfResources.pdfName = pdfName; } public PdfResources() { //在这里调用pdf数据表的生成方法 RepeatingTable rt = new RepeatingTable(); //然后获取pdf文件名称 pdfName = rt.getPdfName(); System.out.println("pdfName_" + pdfName); try { FacesContext fc = FacesContext.getCurrentInstance(); ExternalContext ec = fc.getExternalContext(); pdfResourceDynFileName = new MyResource(ec, pdfName); } catch (Exception e) { e.printStackTrace(); } } public static byte[] toByteArray(InputStream input) throws IOException { ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] buf = new byte[4096]; int len = 0; while ((len = input.read(buf)) > -1) { output.write(buf, 0, len); } return output.toByteArray(); } public Resource getPdfResourceDynFileName() { return pdfResourceDynFileName; } public void setPdfResourceDynFileName(Resource pdfResourceDynFileName) { this.pdfResourceDynFileName = pdfResourceDynFileName; } } class MyResource implements Resource, Serializable { private String resourceName; private InputStream inputStream; private final Date lastModified; private ExternalContext extContext; public MyResource(ExternalContext ec, String resourceName) { this.extContext = ec; this.resourceName = resourceName; this.lastModified = new Date(); } public InputStream open() throws IOException { if (inputStream == null) { //PdfResources.RESOURCE_PATH + resourceName 就是我们生成的pdf文件 InputStream stream = extContext.getResourceAsStream(PdfResources.RESOURCE_PATH + resourceName); byte[] byteArray = PdfResources.toByteArray(stream); inputStream = new ByteArrayInputStream(byteArray); } return inputStream; } public String calculateDigest() { return resourceName; } public Date lastModified() { return lastModified; } public void withOptions(Options arg0) throws IOException { } }