gpt4 book ai didi

c# - 将数据表转换为 PDF

转载 作者:可可西里 更新时间:2023-11-01 08:52:21 34 4
gpt4 key购买 nike

我可以在 Asp.net Web 应用程序中获取将数据表转换为 pdf 的代码吗?我想要将 datatable 导出到 PDF 的功能。我找到了 this文章,但它使用 gridview 导出

最佳答案

使用 iTextSharp,你可以做到。它可以从互联网上下载,而且是免费的。请找到下面的代码,

   public void ExportToPdf(DataTable dt,string strFilePath)
{
Document document = new Document();
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(strFilePath, FileMode.Create));
document.Open();
iTextSharp.text.Font font5 = iTextSharp.text.FontFactory.GetFont(FontFactory.HELVETICA, 5);

PdfPTable table = new PdfPTable(dt.Columns.Count);
PdfPRow row = null;
float[] widths = new float[dt.Columns.Count];
for (int i = 0; i < dt.Columns.Count; i++)
widths[i] = 4f;

table.SetWidths(widths);

table.WidthPercentage = 100;
int iCol = 0;
string colname = "";
PdfPCell cell = new PdfPCell(new Phrase("Products"));

cell.Colspan = dt.Columns.Count;

foreach (DataColumn c in dt.Columns)
{
table.AddCell(new Phrase(c.ColumnName, font5));
}

foreach (DataRow r in dt.Rows)
{
if (dt.Rows.Count > 0)
{
for (int h = 0; h < dt.Columns.Count; h++)
{
table.AddCell(new Phrase(r[h].ToString(), font5));
}
}
}
document.Add(table);
document.Close();
}

关于c# - 将数据表转换为 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13927675/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com