itextpdf 表格生成与跨行跨列

itextpdf 表格跨行跨列操作

由于前段时间需要做需求接触pdf表格,表格需要跨行跨列操作,写了个工具类,代码如下:

static class PdfUtil {

        /**

         * 生成一个表格

         * @author hou_fx

         * @param total  总列数

         * @param textFont 字体

         * @param data   表格数据     X行    Y列

         * @param doc    PDF文档对象

         * @throws DocumentException

         */

        public static void TableBule(int total,Font textFont,List> data,Document doc) throws DocumentException{

            // 创建一个有N列的表格

            PdfPTable table = new PdfPTable(total);

            table.setPaddingTop(20);

            table.setSpacingAfter(20);

            table.setTotalWidth(530); //设置列宽

            // table.setTotalWidth(new float[]{ 100, 165, 100, 165 }); //设置列宽

            table.setLockedWidth(true); //锁定列宽

            PdfPCell cell;

            for (int i = 0; i < data.size(); i++) {  //遍历数据行   每个数据行都是一个list

                Iterator it = data.get(i).iterator();

                int count=0;

                while (it.hasNext()) {               //遍历每行数据,每个数据都是一个单元格

                    cell = new PdfPCell(new Phrase(it.next(), textFont));

                    cell.setMinimumHeight(17); //设置单元格高度

                    cell.setUseAscender(true); //设置可以居中

                    //第一个单元格背景色

                    if (count%2==0) {

                        cell.setBackgroundColor(new BaseColor(231,230,230));

                    }

                    cell.setHorizontalAlignment(Element.ALIGN_LEFT); //左对齐

                    cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //设置垂直居中

                    table.addCell(cell);

                    count++;

                }

            }

            doc.add(table);

        }
        /**
         * 生成一个表格
         * @author hou_fx
         * @param total 总列数
         * @param textFont 字体
         * @param data  表格数据     X行    Y列
         * @param doc PDF文档对象
         * @param colspan 第几列
         * @param rowspan 第几行
         * @param number  跨几列
         * @throws DocumentException
         */
        public static void TableColspan(int total,Font textFont,List<List<String>> data,Document doc,int[] rowspan,int[] colspan,int[] number) throws DocumentException{
            // 创建一个有N列的表格
            PdfPTable table = new PdfPTable(total);
            table.setPaddingTop(20);
            table.setSpacingAfter(20);
            table.setTotalWidth(530); //设置列宽
            // table.setTotalWidth(new float[]{ 100, 165, 100, 165 }); //设置列宽
            table.setLockedWidth(true); //锁定列宽
            PdfPCell cell;
            //数组下标
            int cos=0;
            for (int i = 0; i < data.size(); i++) {  //遍历数据行   每个数据行都是一个list
                Iterator<String> it = data.get(i).iterator();
                int count=0;
                while (it.hasNext()) {               //遍历每行数据,每个数据都是一个单元格
                    cell = new PdfPCell(new Phrase(it.next(), textFont));
                    cell.setMinimumHeight(17); //设置单元格高度
                    cell.setUseAscender(true); //设置可以居中
                    if (cos<rowspan.length && i==rowspan[cos]-1 && count==colspan[cos]-1) {
//					if (i==rowspan[cos]-1) {//行
//						if (count==colspan[cos]-1) {//列
                        cell.setColspan(number[cos]);//跨单元格
                        cos++;
//						}
//					}
                    }
                    cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                    cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //设置垂直居中
                    table.addCell(cell);
                    count++;
                }
            }
            doc.add(table);
        }
    }

部分代码,可根据实际需求进行扩展。
效果图如下:

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值