| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 583 人关注过本帖
标题:C# 生成 PDF 表格
只看楼主 加入收藏
lzrcoming
Rank: 2
等 级:论坛游民
帖 子:33
专家分:12
注 册:2014-4-21
结帖率:28.57%
收藏
已结贴  问题点数:8 回复次数:4 
C# 生成 PDF 表格
PdfPTable table = new PdfPTable(10);
                table.WidthPercentage = 323 / 5.23f;
               // table.SetWidths(new int[] { 2, 1, 1 });


                PdfPCell cell;
                cell=new PdfPCell(new Phrase("Cell with colspan 1"));
                cell.Colspan = 10;
                table.addCell(cell);

                cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
              //  Rectangle rect = new Rectangle(523, 770);
               // table.SetWidthPercentage(new float[] { 144, 72, 72 }, rect);
               // cell.Rowspan = 2;  为什么cell.Colspan可以用,而这句不能用,这样画出来的表格都是一样大的,我想在一行表格中,前面是一行,后面分成两行

                table.addCell(cell);
                table.addCell("row 1; cell 1");
                table.addCell("row 1; cell 2");
                table.addCell("row 1; cell 3");
                table.addCell("row 1; cell 4");
                table.addCell("row 1; cell 5");
                table.addCell("row 1; cell 6");
                table.addCell("row 1; cell 7");
                table.addCell("row 1; cell 8");
                table.addCell("row 1; cell 9");
               
      

                cell = new PdfPCell(new Phrase("Cell with rowspan 3"));
                table.addCell(cell);
                table.addCell("row 1; cell 1");
                table.addCell("row 1; cell 2");
                table.addCell("row 1; cell 3");
                table.addCell("row 1; cell 4");
                table.addCell("row 1; cell 5");
                table.addCell("row 1; cell 6");
                table.addCell("row 1; cell 7");
                table.addCell("row 1; cell 8");
                table.addCell("row 1; cell 9");
代码.rar (1.65 KB)


[ 本帖最后由 lzrcoming 于 2014-8-19 10:43 编辑 ]
2014-08-19 10:41
lzrcoming
Rank: 2
等 级:论坛游民
帖 子:33
专家分:12
注 册:2014-4-21
收藏
得分:0 
有所有代码,有帮忙的朋友可以说下,我把所有代码都附上
2014-08-19 10:42
邓士林
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:淮河河畔
等 级:贵宾
威 望:61
帖 子:2392
专家分:13384
注 册:2013-3-3
收藏
得分:4 
什么意思?

Maybe
2014-08-20 07:50
鹏哥v5
Rank: 2
等 级:论坛游民
帖 子:18
专家分:80
注 册:2014-8-3
收藏
得分:4 
这是要干嘛
2014-08-20 09:24
bluesky2992
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2019-1-10
收藏
得分:0 
Spire.PDF提供了两种类PdfTable和PdfGrid用于创建PDF表格。
第一种:通过 PdfTable 类创建表格
static void Main(string[] args)
{
    //创建一个PDF文档
    PdfDocument doc = new PdfDocument();
    //添加一页
    PdfPageBase page = doc.Pages.Add();
    //创建一个PdfTable对象
    PdfTable table = new PdfTable();
    //设置字体
    table.Style.DefaultStyle.Font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 9f), true);
    table.Style.HeaderStyle.Font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 9f), true);
    //创建一个DataTable并写入数据
    DataTable dataTable = new DataTable();
    dataTable.Columns.Add("名字");
    dataTable.Columns.Add("年龄");
    dataTable.Columns.Add("性别");
    dataTable.Rows.Add(new string[] { "张红", "22", "女" });
    dataTable.Rows.Add(new string[] { "王东", "25", "男" });
    //填充数据到PDF表格
    table.DataSource = dataTable;
    //显示表头(默认不显示)
    table.Style.ShowHeader = true;
    //在BeginRowLayout事件处理方法中注册自定义事件
    table.BeginRowLayout += Table_BeginRowLayout;
    //将表格绘入PDF并指定位置和大小
    table.Draw(page, new RectangleF(0, 20, 200, 90));
    //保存到文档
    doc.SaveToFile("PDF表格_1.pdf");
}
//在自定义事件中设置行高
private static void Table_BeginRowLayout(object sender, BeginRowLayoutEventArgs args)
{
    args.MinimalHeight = 20f;
}
第二种:通过 PdfGrid 创建表格
//创建一个PDF文档
PdfDocument doc = new PdfDocument();
//添加一页
PdfPageBase page = doc.Pages.Add();
//创建一个PdfGrid对象
PdfGrid grid = new PdfGrid();
//设置单元格边距
grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1);
//添加2行4列
PdfGridRow row1 = grid.Rows.Add();
PdfGridRow row2 = grid.Rows.Add();
grid.Columns.Add(4);

//设置列宽
foreach (PdfGridColumn col in grid.Columns)
{
    col.Width = 60f;
}

//写入数据
for (int i = 0; i < grid.Columns.Count; i++)
{
    row1.Cells[i].Value = String.Format("col{0}", i + 1);
    row2.Cells[i].Value = String.Format("{0}", i + 1);
}
//将表格绘入文档
grid.Draw(page, new PointF(0, 20));
//保存到文档
doc.SaveToFile("PDF表格_2.pdf");
2019-01-10 21:54
快速回复:C# 生成 PDF 表格
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012386 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved