简体   繁体   中英

Export Table from Sql Server to PDF file using c#

i have few tables in sql and i want to export them to PDF file after i click the form button does anyone knows how can i do it?

i have this code when i export table from SQL to Excel:

protected void insertBTN(object sender, EventArgs e)
{
string conString = @"Data Source =XXXX; Initial Catalog=XXXX;     Persist Security     Info=True;User ID=XXXX; Password=XXXX";SqlConnection sqlCon     = new     SqlConnection(conString);
sqlCon.Open();

SqlDataAdapter da = new SqlDataAdapter("SELECT * from InjuryScenario", sqlCon);
System.Data.DataTable dtMainSQLData = new System.Data.DataTable();
da.Fill(dtMainSQLData);
DataColumnCollection dcCollection = dtMainSQLData.Columns;
// Export Data into EXCEL Sheet
Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new                                            
Microsoft.Office.Interop.Excel.ApplicationClass();
ExcelApp.Application.Workbooks.Add(Type.Missing);
// ExcelApp.Cells.CopyFromRecordset(objRS);
for (int i = 1; i < dtMainSQLData.Rows.Count + 2; i++)
{
    for (int j = 1; j < dtMainSQLData.Columns.Count + 1; j++)
    {
        if (i == 1)
        {
            ExcelApp.Cells[i, j] = dcCollection[j - 1].ToString();
        }
        else
            ExcelApp.Cells[i, j] = dtMainSQLData.Rows[i - 2][j - 1].ToString();
    }
}
ExcelApp.ActiveWorkbook.SaveCopyAs("C:\\Users\\Mor Shivek\\Desktop\\test.xls");
ExcelApp.ActiveWorkbook.Saved = true;
ExcelApp.Quit();
}

What do you mean by "export to PDF"? Wouldn't it be the easiest way just to use your excel export above and then sending a print command for the file using a PDF printer? If you want to create the PDF natively you will most likely have some effort to layout the document.

//edit: just a little research on SO would have brought this up as well: Best C# API to create PDF

Refer to this

using System;
using System.Windows.Forms;
using System.Diagnostics;
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using System.Data.SqlClient;
using System.Data;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            string connetionString = null;
            SqlConnection connection ;
            SqlCommand command ;
            SqlDataAdapter adapter = new SqlDataAdapter();
            DataSet ds = new DataSet();
            int i = 0;
            string sql = null;
            int yPoint = 0;
            string pubname = null;
            string city = null;
            string state = null;

            connetionString = "Data Source=YourServerName;Initial Catalog=pubs;User ID=sa;Password=zen412";
            sql = "select pub_name,city,country from publishers";
            connection = new SqlConnection(connetionString);
            connection.Open();
            command = new SqlCommand(sql, connection);
            adapter.SelectCommand = command;
            adapter.Fill(ds);
            connection.Close();

            PdfDocument pdf = new PdfDocument();
            pdf.Info.Title = "Database to PDF";
            PdfPage pdfPage = pdf.AddPage();
            XGraphics graph = XGraphics.FromPdfPage(pdfPage);
            XFont font = new XFont("Verdana", 20, XFontStyle.Regular );

            yPoint = yPoint + 100;

            for (i = 0; i < = ds.Tables[0].Rows.Count - 1; i++)
            {
                pubname = ds.Tables[0].Rows[i].ItemArray[0].ToString ();
                city = ds.Tables[0].Rows[i].ItemArray[1].ToString();
                state = ds.Tables[0].Rows[i].ItemArray[2].ToString();

                graph.DrawString(pubname, font, XBrushes.Black, new XRect(40, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);

                graph.DrawString(city, font, XBrushes.Black, new XRect(280, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);

                graph.DrawString(state, font, XBrushes.Black, new XRect(420, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);

                yPoint = yPoint + 40;
            }


            string pdfFilename = "dbtopdf.pdf";
            pdf.Save(pdfFilename);
            Process.Start(pdfFilename);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM