尝试从Datagridview打开导出的PDF文件时出错? [英] Error while trying to open exported PDF file from Datagridview?

查看:120
本文介绍了尝试从Datagridview打开导出的PDF文件时出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我想将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.Horizo​​ntalAlignment = 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.Horizo​​ntalAlignment = 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.Horizo​​ntalAlignment = 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中的错误。);

}



}

解决方案

使用c#从asp.net的网格视图导出PDF文件。





转到此链接,它将为您提供完整的帮助。完整的源代码可在此处获得演示



http://aspdotnet-alam.com/Article/export-pdf-file-from-grid-view-in-Asp-Net-using- C-夏普-38.aspx

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.");
}

}

解决方案

Export PDF file from grid view in asp.net using c#.


go to this link ,it will help full for you.full source code available here with demo

http://aspdotnet-alam.com/Article/export-pdf-file-from-grid-view-in-Asp-Net-using-C-Sharp-38.aspx


这篇关于尝试从Datagridview打开导出的PDF文件时出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆