报表制作Itext

/**
 * 
 */
package com.csk;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

/**
 * @author 310059
 *
 */
public class PDFtest {

    /**
     * @param args
     */
    static PdfPTable aTable = null;
    static PdfWriter writer = null;
    public static void main(String[] args) {
        // TODO 自動生成されたメソッド・スタブ
       
        try {
            
            BufferedReader br = new BufferedReader(new FileReader("D:/new.csv"));
            String str = "";
            List<String[]> header = new ArrayList<String[]>();
            List<String[]> footer = new ArrayList<String[]>();
            List<String[]> title = new ArrayList<String[]>();
            
            String[] strarray = null;
            
            String[] listsize = null;
            
            while((str=br.readLine())!=null){
                
                strarray = str.split(",");
                if(strarray[0].equals("header")){
                    header.add(strarray);
                    continue ;
                }
                if(strarray[0].equals("title")){
                    title.add(strarray);
                    continue ;
                }
                if(strarray[0].equals("title_column")){
                    listsize = strarray;
                    continue ;
                }
                if(strarray[0].equals("footer")){
                    footer.add(strarray);
                    continue ;
                }
                if(strarray.length>2){
                    title.add(strarray);
                    continue ;
                }
            }
            doshow(header, footer, title, listsize);
            br.close();
        } catch (FileNotFoundException e) {
            // TODO 自動生成された catch ブロック
            e.printStackTrace();
        } catch (IOException e) {
            // TODO 自動生成された catch ブロック
            e.printStackTrace();
        }
    }
    
    
    public static void doshow( List header, List footer, List title, String[] listsize){
       
        
        Document document = new Document(PageSize.A4,45, 45, 45, 50);
        try {
            writer =  PdfWriter.getInstance(document, new FileOutputStream("Chap01.pdf"));
            document.open();
            // first header 
            if(header.size()!=0){
                    String[] str = (String[]) header.get(0);
                    Paragraph p1 = new Paragraph(str[1],FontCreate.geheadertFont());
                    p1.setSpacingAfter(15f); 
                    document.add(p1);
                for (int i = 1; i < header.size(); i++) {
                   
                    String[] str1 = (String[]) header.get(i);
                    Paragraph p2 = new Paragraph(str1[1],FontCreate.geheadertFont1());
                    p2.setAlignment(Element.ALIGN_RIGHT); 
                    p2.setSpacingAfter(5f); 
                    document.add(p2);
                    
                    }
                   
             }
            
            
            
            //second title  
            int width[] = {15,0,25,0,10,0,5,5}; 
            int size = 0;
            int j = 0;
            String[] sizestr = (String[]) title.get(0);
            size = sizestr.length;
            aTable = new PdfPTable(size-1);
            aTable.setWidthPercentage(100);
            aTable.setWidths(width);
            aTable.flushContent();
            aTable.setSkipFirstHeader(false);
            for(int i=0;i<title.size();i++){
               
                String[] str = (String[]) title.get(i);
                for (int k = 1; k < str.length; k++) {
                   if(i==0){
                       Paragraph paragraph =new Paragraph(str[k],FontCreate.getFisrtFont1());   
                       paragraph.setAlignment(Element.ALIGN_CENTER);
                       PdfPCell cell = new PdfPCell(paragraph);
                       cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
                       cell.addElement(paragraph);   
                       cell.setPadding(5);
                       aTable.addCell(cell);
                       aTable.setHeaderRows(1);
                   }else {
                       if(str[1].equals("")&&k==1&&i!=title.size()-1){
                           Phrase phrase = new Phrase(str[k],FontCreate.geheadertFont1());
                           PdfPCell cell = new PdfPCell(phrase);
                           cell.setBorder(4);
                           aTable.addCell(cell);
                       }else if((!str[1].equals(""))){
                           Phrase phrase = new Phrase(str[k],FontCreate.geheadertFont1());
                           PdfPCell cell = new PdfPCell(phrase);
                           cell.setBorder(Element.SECTION);
                           aTable.addCell(cell);
                           
                       }else if(i==title.size()-1){
                           
                           Phrase phrase = new Phrase(str[k],FontCreate.geheadertFont1());
                           PdfPCell cell = new PdfPCell(phrase);
                           cell.setBorder(Element.RECTANGLE);
                           aTable.addCell(cell);
                           
                       }else{
                           Phrase phrase = new Phrase(str[k],FontCreate.geheadertFont1());
                           PdfPCell cell = new PdfPCell(phrase);
                           //cell.setBorder(4);
                           aTable.addCell(cell);
                           }
                     
                       }
                   }
            }  
            document.add(aTable);
            
            
            
            // third footer 
            if(header.size()!=0){
                
                for (int i = 0; i < footer.size(); i++) {
                    String[] str1 = (String[]) footer.get(i);
                    
                            if(str1.length<2){
                                Paragraph p2 = new Paragraph(" ");
                                document.add(p2);
                            }else {
                                
                                Paragraph p2 = new Paragraph(str1[1],FontCreate.geheadertFont1());
                                document.add(p2);
                            }
                    }
             }
        } catch (FileNotFoundException e) {
            // TODO 自動生成された catch ブロック
            e.printStackTrace();
        } catch (DocumentException e) {
            // TODO 自動生成された catch ブロック
            e.printStackTrace();
        }
        document.close();
    }        
}

 从CSV文件读取 然后再进行PDF制作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值