[英]How to convert data from dataGridView to a PDF file?
I have this dataGridView which I would like to convert to a PDF file.我有这个 dataGridView,我想将其转换为 PDF 文件。 How do I do it?
我该怎么做? I searched online and found some codes but it seems like the website I found missed out on a lot of things.
我在网上搜索并找到了一些代码,但我发现的网站似乎遗漏了很多东西。
using System.IO;
using System.Data;
using System.Reflection;
using iTextSharp.text.pdf;
using iTextSharp.text;
private void btnExportPdf_Click(object sender, EventArgs e)
{
//Creating iTextSharp Table from the DataTable data
PdfPTable pdfTable = new PdfPTable(dataGridView1.ColumnCount);
pdfTable.DefaultCell.Padding = 3;
pdfTable.WidthPercentage = 30;
pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;
pdfTable.DefaultCell.BorderWidth = 1;
//Adding Header row
foreach (DataGridViewColumn column in dataGridView1.Columns)
{
PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText));
cell.BackgroundColor = new iTextSharp.text.Color(240, 240, 240);
pdfTable.AddCell(cell);
}
//Adding DataRow
foreach (DataGridViewRow row in dataGridView1.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
pdfTable.AddCell(cell.Value.ToString());
}
}
//Exporting to PDF
string folderPath = "C:\\PDFs\\";
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
using (FileStream stream = new FileStream(folderPath + "DataGridViewExport.pdf", FileMode.Create))
{
Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
PdfWriter.GetInstance(pdfDoc, stream);
pdfDoc.Open();
pdfDoc.Add(pdfTable);
pdfDoc.Close();
stream.Close();
}
}
I Converted this:我转换了这个:
//Adding DataRow
foreach (DataGridViewRow row in dataGridView1.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
pdfTable.AddCell(cell.Value.ToString());
}
}
TO:
//Adding DataRow
foreach (DataGridViewRow row in dataGridView1.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
try
{
pdfTable.AddCell(cell.Value.ToString());
}
catch { }
}
}
And in new version that I downloaded this must changed:在我下载的新版本中,必须更改:
cell.BackgroundColor = new iTextSharp.text.Color(240, 240, 240);
to:到:
cell.BackgroundColor = new iTextSharp.text.BaseColor(240, 240, 240);
{
if (dataGridView1.Rows.Count > 0)
{
SaveFileDialog save = new SaveFileDialog();
save.Filter = "PDF (*.pdf)|*.pdf";
save.FileName = "Envanter.pdf";
bool ErrorMessage = false;
if (save.ShowDialog() == DialogResult.OK)
{
if (File.Exists(save.FileName))
{
try
{
File.Delete(save.FileName);
}
catch (Exception ex)
{
ErrorMessage = true;
MessageBox.Show("Diskteki Veriler Yazılamıyor!"+ex.Message);
}
}
if (!ErrorMessage)
{
try
{
PdfPTable pTable = new PdfPTable(dataGridView1.Columns.Count);
pTable.DefaultCell.Padding = 2;
pTable.WidthPercentage = 100;
pTable.HorizontalAlignment = Element.ALIGN_LEFT;
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
PdfPCell pCell = new PdfPCell(new Phrase(col.HeaderText));
pTable.AddCell(pCell);
}
foreach (DataGridViewRow viewRow in dataGridView1.Rows)
{
foreach (DataGridViewCell dcell in viewRow.Cells)
{
pTable.AddCell(dcell.Value.ToString());
}
}
using (FileStream fileStream=new FileStream(save.FileName,FileMode.Create))
{
Document document = new Document(PageSize.A4,12f,20f,20f,12f);
document.Open();
document.Add(pTable);
document.Close();
fileStream.Close();
}
MessageBox.Show("İşlem Başarıyla Gerçekleştirildi","Bilgi");
}
catch (Exception ex)
{
MessageBox.Show("Veri Dışa Aktarılırken Bir Hata Meydana Geldi!"+ex.Message);
}
}
}
}
else
{
MessageBox.Show("Kayıt Bulunamadı.","Bilgi");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.