和POI一样好用得iText

POI 可以方便EXCLE的输出,而iText对于PDF的输出也是很完美的.下面看一个Sample的代码和效果

package com.fhway.iTextSample;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

public class HelloWorld {
    
public static void main(String[] args) {
        
try {
            
// 创建一个文档对象,定义纸张类型及方向,页边距
            Document document = new Document(PageSize.A4.rotate(), 101010,
                    
10); 
            
// 定义输出位置并把文档对象装入输出对象中
            PdfWriter.getInstance(document,
                    
new FileOutputStream("c:/hello.pdf"));
            System.out.println(
"document.add(BigTable)");
            
// 打开文档对象
            document.open();
            
//定义表格填充内容
            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[] = 9481081197910410 };
            
            datatable.setWidths(headerwidths);
            datatable.setWidthPercentage(
100);
            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);

            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(1f);
                }

            }

            document.add(datatable);
            
//关闭
            document.close();
        }
 catch (FileNotFoundException e) {
            e.printStackTrace();
        }
 catch (DocumentException e) {
            e.printStackTrace();
        }

    }

}

输出效果为由于他的输出一样可以定制,我们可以考虑一个通用的设置处理
posted @ 2006-11-28 12:15  fhway  阅读(664)  评论(0)    收藏  举报