问题描述
大家好,
我想将datagridview导出为PDF文件,生成pdf后我无法打开该文件..当我尝试打开它显示文件已损坏的错误消息,也没有得到Pdf生成成功消息。
这是我的代码:
private void btnExportPdf_Click(string heading,string filename)
{
try
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter =All Files | *。*;
if(saveFileDialog.ShowDialog ()== DialogResult.OK)
{
string path = saveFileDialog.FileName;
文件pdfdoc =新文件(PageSize.A4); //设置PDF的页面大小
PdfWriter writer = PdfWriter.GetInstance(pdfdoc,new FileStream(path +。pdf,FileMode.Create)); //使用PDF Writer类生成PDF
writer.PageEvent = new PDFFooter();
//打开PDF以从文本框中写入数据
PdfPTable table = new PdfPTable(dataGridView1.Columns.Count);
//table.TotalWidth = GridView 。宽度;
float [] widths = new float []
{
dataGridView1.Columns [0] .Width,dataGridView1.Columns [1] .Width,dataGridView1.Columns [2] .Width
};
table.SetWidths(widths);
table.HorizontalAlignment = 1; // 0 - 左,1 - 中,2 - 右;
table.SpacingBefore = 2.0F;
PdfPCell cell = null;
pdfdoc.Open();
//doc.Open();
//短语p =新短语(new Chunk(heading,titleFont));
// doc.Add(p);
foreach(DataGridViewColumn c in dataGridView1.Columns)
{
cell = new PdfPCell(new Phrase(new Chunk(c.HeaderText)));
cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
cell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
table.AddCell(cell);
}
if( dataGridView1.Rows.Count> 0)
{
for(int i = 0; i< dataGridView1.Rows.Count; i ++)
{
PdfPCell [] objcell = new PdfPCell [dataGridView1.Columns.Count];
for(int j = 0; j< dataGridView1.Columns.Count - 1; j ++)
{
cell = new PdfPCell(new Phrase(dataGridView1.Rows [ i] .Cells [j] .Value.ToString()));
cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
cell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
table.AddCell(cell);
//lstCells.Add(cell);
objcell [j] = cell;
}
PdfPRow newrow = new PdfPRow(objcell);
table.Rows.Add(newrow);
}
}
pdfdoc.Add(table);
MessageBox .Show(Pdf Generation Successfully。);
pdfdoc.Close();
}
}
catch(Exception ex)
{
MessageBox.Show(pdf Generation中的错误。);
}
}
Hi everyone ,
I want to export datagridview to PDF file,After generating the pdf I'm not able to open that file..when i try to open it shows file corrupted error message and also not getting "Pdf Generation successfully " Message.
Here is my code :
private void btnExportPdf_Click(string heading, string filename)
{
try
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "All Files | *.* ";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
string path = saveFileDialog.FileName;
Document pdfdoc = new Document(PageSize.A4); // Setting the page size for the PDF
PdfWriter writer = PdfWriter.GetInstance(pdfdoc, new FileStream(path + ".pdf", FileMode.Create)); //Using the PDF Writer class to generate the PDF
writer.PageEvent = new PDFFooter();
// Opening the PDF to write the data from the textbox
PdfPTable table = new PdfPTable(dataGridView1.Columns.Count);
//table.TotalWidth = GridView.Width;
float[] widths = new float[]
{
dataGridView1.Columns[0].Width, dataGridView1.Columns[1].Width, dataGridView1.Columns[2].Width
};
table.SetWidths(widths);
table.HorizontalAlignment = 1; // 0 - left, 1 - center, 2 - right;
table.SpacingBefore = 2.0F;
PdfPCell cell = null;
pdfdoc.Open();
//doc.Open();
// Phrase p = new Phrase(new Chunk(heading, titleFont));
// doc.Add(p);
foreach (DataGridViewColumn c in dataGridView1.Columns)
{
cell = new PdfPCell(new Phrase(new Chunk(c.HeaderText)));
cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
cell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
table.AddCell(cell);
}
if (dataGridView1.Rows.Count > 0)
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
PdfPCell[] objcell = new PdfPCell[dataGridView1.Columns.Count];
for (int j = 0; j < dataGridView1.Columns.Count - 1; j++)
{
cell = new PdfPCell(new Phrase(dataGridView1.Rows[i].Cells[j].Value.ToString()));
cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
cell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
table.AddCell(cell);
//lstCells.Add(cell);
objcell[j] = cell;
}
PdfPRow newrow = new PdfPRow(objcell);
table.Rows.Add(newrow);
}
}
pdfdoc.Add(table);
MessageBox.Show("Pdf Generation Successfully.");
pdfdoc.Close();
}
}
catch (Exception ex)
{
MessageBox.Show("Error in pdf Generation.");
}
}
这篇关于尝试从Datagridview打开导出的PDF文件时出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!