c*********c 发帖数: 113 | 1 VS2010, 做的一个windows application
需要实现的一个功能是,把一个已经拿到的dataTable, 以PDF的形式保存到本地,用
的iTextSharp
一开始说我column的number不对,然后各种在网上找,然后改的面目全非以后又出现了
如题所示的错误。。。改疯了,求高手指点。。。
以下为代码:
public string ExportToPDF(DataTable dt, ReportEnums reportType, DateTime
fromDate, DateTime toDate)
{
//这句没关系
string pdfName = utls.GetFileNameandPath(reportType);
Document document = new Document();
MemoryStream inputPDF = new MemoryStream();
MemoryStream output = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(document, output);
PdfReader reader = new PdfReader(inputPDF);
document.Open();
PdfContentByte canvas = writer.DirectContent;
PdfImportedPage page;
for (int i = 1; i <= reader.NumberOfPages; i++)
{
page = writer.GetImportedPage(reader, i);
canvas.AddTemplate(page, 1f, 0, 0, 1, 0, 0);
}
var titleFont = FontFactory.GetFont("Arial", 18, Font.BOLD);
var dateFont = FontFactory.GetFont("Arial", 14, Font.BOLDITALIC);
var tableFont = FontFactory.GetFont("Arial", 11);
//title for the pdf report
document.Add(new Paragraph(utls.GetReportTitle(reportType),
titleFont));
//date for the PDF report
string fDate = fromDate.ToString();
string tDate = toDate.ToString();
document.Add(new Paragraph("From " + fDate + " To " + tDate,
dateFont));
//create the table
int cols = dt.Columns.Count;
PdfPTable reportTable = new PdfPTable(cols);
reportTable.HorizontalAlignment = 0;
reportTable.SpacingBefore = 10;
reportTable.SpacingAfter = 10;
reportTable.DefaultCell.Border = 0;
reportTable.SetWidths(new int[] { 1, 4 });
//这个是我原来自己写的方法,不知道对不对
//for (int i = 0; i < cols; i++)
//{
// reportTable.AddCell(dt.Columns[i].ColumnName.ToString().
ToUpper());
// reportTable.CompleteRow();
//}
//for (int i = 1; i < dt.Rows.Count; i++)
//{
//
// for (int j = 0; j < cols; j++)
// {
// reportTable.AddCell(dt.Columns[j].ColumnName.ToString(
));
// }
// reportTable.CompleteRow();
//}
//这个是网上搜到的方法。。。
foreach (DataRow dr in dt.Rows)
{
foreach (object o in dr.ItemArray)
{
PdfPCell c = new PdfPCell();
c.AddElement(new Chunk(o.ToString()));
reportTable.AddCell(c);
}
reportTable.CompleteRow();
}
document.Add(reportTable);
document.Close();
inputPDF.Close();
output.Close();
writer.Close();
reader.Close();
return pdfName;
} | c*********c 发帖数: 113 | | c*********c 发帖数: 113 | 3 VS2010, 做的一个windows application
需要实现的一个功能是,把一个已经拿到的dataTable, 以PDF的形式保存到本地,用
的iTextSharp
一开始说我column的number不对,然后各种在网上找,然后改的面目全非以后又出现了
如题所示的错误。。。改疯了,求高手指点。。。
以下为代码:
public string ExportToPDF(DataTable dt, ReportEnums reportType, DateTime
fromDate, DateTime toDate)
{
//这句没关系
string pdfName = utls.GetFileNameandPath(reportType);
Document document = new Document();
MemoryStream inputPDF = new MemoryStream();
MemoryStream output = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(document, output);
PdfReader reader = new PdfReader(inputPDF);
document.Open();
PdfContentByte canvas = writer.DirectContent;
PdfImportedPage page;
for (int i = 1; i <= reader.NumberOfPages; i++)
{
page = writer.GetImportedPage(reader, i);
canvas.AddTemplate(page, 1f, 0, 0, 1, 0, 0);
}
var titleFont = FontFactory.GetFont("Arial", 18, Font.BOLD);
var dateFont = FontFactory.GetFont("Arial", 14, Font.BOLDITALIC);
var tableFont = FontFactory.GetFont("Arial", 11);
//title for the pdf report
document.Add(new Paragraph(utls.GetReportTitle(reportType),
titleFont));
//date for the PDF report
string fDate = fromDate.ToString();
string tDate = toDate.ToString();
document.Add(new Paragraph("From " + fDate + " To " + tDate,
dateFont));
//create the table
int cols = dt.Columns.Count;
PdfPTable reportTable = new PdfPTable(cols);
reportTable.HorizontalAlignment = 0;
reportTable.SpacingBefore = 10;
reportTable.SpacingAfter = 10;
reportTable.DefaultCell.Border = 0;
reportTable.SetWidths(new int[] { 1, 4 });
//这个是我原来自己写的方法,不知道对不对
//for (int i = 0; i < cols; i++)
//{
// reportTable.AddCell(dt.Columns[i].ColumnName.ToString().
ToUpper());
// reportTable.CompleteRow();
//}
//for (int i = 1; i < dt.Rows.Count; i++)
//{
//
// for (int j = 0; j < cols; j++)
// {
// reportTable.AddCell(dt.Columns[j].ColumnName.ToString(
));
// }
// reportTable.CompleteRow();
//}
//这个是网上搜到的方法。。。
foreach (DataRow dr in dt.Rows)
{
foreach (object o in dr.ItemArray)
{
PdfPCell c = new PdfPCell();
c.AddElement(new Chunk(o.ToString()));
reportTable.AddCell(c);
}
reportTable.CompleteRow();
}
document.Add(reportTable);
document.Close();
inputPDF.Close();
output.Close();
writer.Close();
reader.Close();
return pdfName;
} | c*********c 发帖数: 113 | | w*******j 发帖数: 70 | 5 when building table header, should this line be outside of for loop?
reportTable.CompleteRow();
add try catch to the code |
|