Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if (dgvFuncionarios.Rows.Count > 0)
- {
- SaveFileDialog sfd = new SaveFileDialog();
- sfd.Filter = "PDF (*.pdf)|*.pdf";
- sfd.FileName = "Output.pdf";
- bool fileError = false;
- if (sfd.ShowDialog() == DialogResult.OK)
- {
- if (File.Exists(sfd.FileName))
- {
- try
- {
- File.Delete(sfd.FileName);
- }
- catch (IOException ex)
- {
- fileError = true;
- MessageBox.Show("It wasn't possible to write the data to the disk." + ex.Message);
- }
- }
- if (!fileError)
- {
- try
- {
- PdfPTable pdfTable = new PdfPTable(dgvFuncionarios.Columns.Count);
- pdfTable.DefaultCell.Padding = 3;
- pdfTable.WidthPercentage = 100;
- pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;
- foreach (DataGridViewColumn column in dgvFuncionarios.Columns)
- {
- PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText));
- pdfTable.AddCell(cell);
- }
- foreach (DataGridViewRow row in dgvFuncionarios.Rows)
- {
- foreach (DataGridViewCell cell in row.Cells)
- {
- pdfTable.AddCell(cell.Value.ToString());
- }
- }
- using (FileStream stream = new FileStream(sfd.FileName, FileMode.Create))
- {
- Document pdfDoc = new Document(PageSize.A4, 10f, 20f, 20f, 10f);
- PdfWriter.GetInstance(pdfDoc, stream);
- pdfDoc.Open();
- pdfDoc.Add(pdfTable);
- pdfDoc.Close();
- stream.Close();
- }
- MessageBox.Show("Data Exported Successfully !!!", "Info");
- }
- catch (Exception ex)
- {
- MessageBox.Show("Error :" + ex.Message);
- }
- }
- }
- }
- else
- {
- MessageBox.Show("No Record To Export !!!", "Info");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement