将数据导出pdf表格工具类

package cn.piesat.tj.common.utils;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import jxl.write.Label;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Description: pdf工具类
 * @author: lygigsh
 * @create: 2022-01-5 16:22
 **/
public class PDFUtil {

    /**
     * 获取指定内容与字体的单元格
     * @param string
     * @param font
     * @return
     */
    public static PdfPCell getPDFCell(String string, Font font)
    {
        //创建单元格对象,将内容与字体放入段落中作为单元格内容
        PdfPCell cell=new PdfPCell(new Paragraph(string,font));

        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

        //设置最小单元格高度
        cell.setMinimumHeight(25);
        return cell;
    }

    /**
     * 合并行的静态函数
     * @param str
     * @param font
     * @param i
     * @return
     */
    public static PdfPCell mergeRow(String str,Font font,int i) {

        //创建单元格对象,将内容及字体传入
        PdfPCell cell=new PdfPCell(new Paragraph(str,font));
        //设置单元格内容居中
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        //将该单元格所在列包括该单元格在内的i行单元格合并为一个单元格
        cell.setRowspan(i);

        return cell;
    }

    /**
     * 合并列的静态函数
     * @param str
     * @param font
     * @param i
     * @return
     */
    public static PdfPCell mergeCol(String str,Font font,int i) {

        PdfPCell cell=new PdfPCell(new Paragraph(str,font));
        cell.setMinimumHeight(25);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        //将该单元格所在行包括该单元格在内的i列单元格合并为一个单元格
        cell.setColspan(i);

        return cell;
    }
    public static void createPDF(HttpServletResponse response, List prList, String[] columns,String titleName) {

        // 新建document对象
        Document document = new Document(PageSize.A4,36,36,126,36);
        try {
            //添加中文字体
            BaseFont bfChinese=BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            //设置字体样式
            Font textFont = new Font(bfChinese,12,Font.NORMAL);//正常
            Font firsetTitleFont = new Font(bfChinese,22,Font.BOLD); //一级标题
            //设置表格样式,大小
            PdfPTable dataTable = new PdfPTable(columns.length);
            dataTable.setSpacingBefore(10);
            dataTable.setHorizontalAlignment(Element.ALIGN_CENTER);
            dataTable.setTotalWidth(500); //设置列宽
            dataTable.setLockedWidth(true); //锁定列宽
            dataTable.setSpacingAfter(30);
            List<String>  thead= Arrays.asList(columns);
            //创建输出流
            PdfWriter.getInstance(document, response.getOutputStream());
            //判断是否打开
            if (!document.isOpen()){
                document.open();
            }
            //开始写入数据
            Paragraph paragraph = new Paragraph(10);// 边距
            // 1 2 3 中右左
            paragraph.setAlignment(1); // 对齐方式
            paragraph.setFont(firsetTitleFont);// 设置段落字体
            Chunk chunk = new Chunk(titleName);
            paragraph.add(chunk);
            document.add(paragraph);
            //添加表头
            for(String p:thead){
                PdfPCell cell=new PdfPCell(new Phrase(p,textFont));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setBackgroundColor(new BaseColor(216, 218, 220));
                dataTable.addCell(cell);
            }
            //添加表内容
            //判断表中是否有数据
            if (prList != null && prList.size() > 0) {
                //循环写入表中数据
                for (int i = 0; i < prList.size(); i++) {

                    //转换成map集合{activyName:测试功能,count:2}
                    Map map = (Map)prList.get(i);

                    //循环输出map中的子集:既列值
                    int j=0;
                    for(Object o:map.keySet()){
                        //ps:因为要“”通用”“导出功能,所以这里循环的时候不是get("Name"),而是通过map.get(o)
                        if (StringUtils.isEmpty(map.get(o))){
                            map.put(o,"");
                        }
                        PdfPCell cell=new PdfPCell(new Phrase(String.valueOf(map.get(o)),textFont));
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        dataTable.addCell(cell);
                        j++;
                    }
                }
            }
            document.add(dataTable);


        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            document.close();
        }

    }


    public static void exportPDF(HttpServletResponse response,String filename,List listData,String[] columns,String titleName)throws Exception
    {
        //调用上面的方法、生成Excel文件
        response.setContentType("application/pdf;charset=UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename="+new String( filename.getBytes("gb2312"), "ISO8859-1" ));
        createPDF(response,listData,columns,titleName);

    }

}

调用exportPDF();传入对应的参数即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值