Excel2PdfDemo

jar

iText-2.0.8.jar

iTextAsian.jar

 效果

 

package com.demo;

import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
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.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;

public class PdfDemo {

    public static void main(String[] args) throws DocumentException, IOException {
        // 创建Document对象(页面的大小为A4,左、右、上、下的页边距为10)
        Document document = new Document(PageSize.A4, 10, 10, 10, 10);
        // 建立书写器
        String path="E:\\Test\\";
        PdfWriter.getInstance(document, new FileOutputStream(path+"poi.PDF"));
        // 打开文档
        document.open();
        // 使用iTextAsian.jar中的字体
        BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        Font font = new Font(baseFont);
        
        Table table = PdfDemo.setTable();
        document.add(new Paragraph("用户信息如下:",PdfDemo.setFont()));
        document.add(table);
        
        // 关闭文档
        document.close();
    }
    
    public static Table setTable() throws BadElementException{
        //创建一个有n列的表格
        Table table = new Table(14);
        table.setBorderColor(new Color(0, 0, 255));
        table.setBorderWidth(0);
        table.setPadding(0);
        table.setSpacing(0);
        
        /*// 创建表头
        Cell cell1 = POI3.setTableHeader("编号ID");
        Cell cell2 = POI3.setTableHeader("基本信息");
        Cell cell3 = POI3.setTableHeader("姓名");
        table.addCell(cell1);
        table.addCell(cell2);
        table.addCell(cell3);
        // 添加此代码后每页都会显示表头
        table.endHeaders();*/
        
        for (int i = 0; i < 3; i++) {
            if(i==0){
                setCells(table,new String[] {"有权机关类型","操作部门"}, 2, 3);
                setCells(table,new String[] {"查询次数统计","查询结果统计"}, 1, 6);
            }else if(i==1){
                setCells(table,new String[] {"查询类型","零售客户","对公客户"}, 1, 2);
                setCells(table,new String[] {"零售客户","对公客户"}, 1, 3);
            }else if(i==2){
                setCells(table, new String[] {"批量","单笔"}, 0, null);//普通
                for(int j=0;j<4;j++){
                    setCells(table, new String[] {"账户数","客户数"}, 0, null);//普通
                    if(j==2||j==3){
                        setCell(table, "余额总数", 0, null);//普通
                    }
                }
            }
        }
        return table;
    }
    
    public static void setCells(Table table,String[] conts,int type,Integer num) throws BadElementException{
        for(int i=0,length=conts.length;i<length;i++){
            setCell(table, conts[i], type, num);
        }
    }
    /**
     * @category 
     * @param table1
     * @param cont
     * @param type 0-普通,1-合并列,2-合并行,值为0时num参数失效
     * @param num 合并单元格数
     * @throws BadElementException 
     */
    public static void setCell(Table table,String cont,int type,Integer num) throws BadElementException{
        Cell cellTemp= PdfDemo.setTableHeader(cont);
        if(type==1){
            cellTemp.setColspan(num);
        }else if(type==2){
            cellTemp.setRowspan(num);
        }
        table.addCell(cellTemp);
    }

    /**
     * 设置cell
     * @param name
     * @return
     * @throws BadElementException
     */
    public static Cell setTableHeader(String name) throws BadElementException{
        Cell cell = new Cell(new Phrase(name,PdfDemo.setFont()));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);//单元格水平对齐方式
        cell.setVerticalAlignment(Element.ALIGN_CENTER);//单元格垂直对齐方式
//        cell.setHeader(true);
        cell.setBackgroundColor(Color.WHITE);
        return cell;
    }
    
    /**
     * 设置字体编码格式
     * @return
     */
    public static Font setFont(){
        BaseFont baseFont = null;
        try {
            baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Font font = new Font(baseFont, 4, Font.NORMAL,Color.BLUE);
        return font;
    }

}
View Code

 

posted on 2018-03-18 13:00  调适hanxs4  阅读(162)  评论(0)    收藏  举报

导航