注册 登录
编程论坛 C# 论坛

C# 生成 PDF 表格

lzrcoming 发布于 2014-08-19 10:41, 583 次点击
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");
只有本站会员才能查看附件,请 登录


[ 本帖最后由 lzrcoming 于 2014-8-19 10:43 编辑 ]
4 回复
#2
lzrcoming2014-08-19 10:42
有所有代码,有帮忙的朋友可以说下,我把所有代码都附上
#3
邓士林2014-08-20 07:50
什么意思?
#4
鹏哥v52014-08-20 09:24
这是要干嘛
#5
bluesky29922019-01-10 21:54
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");
1