简体   繁体   English

使用C#将表从Sql Server导出到PDF文件

[英]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? 我在sql中有几张表,我想在单击表单按钮后将它们导出到PDF文件,有人知道我该怎么做吗?

i have this code when i export table from SQL to Excel: 当我将表格从SQL导出到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"? “导出为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? 仅使用上面的excel导出,然后使用PDF打印机发送文件的打印命令,这不是最简单的方法吗? If you want to create the PDF natively you will most likely have some effort to layout the document. 如果要本地创建PDF,则很可能会花一些精力来布局文档。

//edit: just a little research on SO would have brought this up as well: Best C# API to create PDF // edit:关于SO的一些研究也可以提出来:创建PDF的最佳C#API

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());
        }
    }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用C#将表从SQL Server导出到Excel 2007 - Export Table from SQL Server to Excel 2007 using C# 使用C#以编程方式导出具有数据的SQL Server表结构 - Export sql server table structure with data programmatically using C# 如何使用 C# 将批量数据从雪花导出到 SQL 服务器表 - How to export a bulk data from snowflake to SQL Server table using C# 使用 c# 将表从 SQL Server 2008 数据库导出到 excel - Export a table from SQL Server 2008 Database to excel using c# 如何使用C#代码导出表的&#39;Sql文件&#39; - How to export 'Sql File' of a Table using C# code 如何使用 Vue 和 C# 控制器从 SSRS(Sql Server 报告服务)下载 pdf 文件? - How to download a pdf file from SSRS (Sql Server Reporting Service) using Vue and C# controllers? 使用 C# 显示 SQL Server 数据库中的 PDF - Show PDF from SQL Server database using C# C#从SQL Server导出到Excel - C# export to excel from sql server 如何从.Net C#和SQL服务器windows应用导出PDF - How to export PDF from .Net C# and SQL server windows application 从远程SQL Server导出Blob并保存到SQL Server所在磁盘上的文件[C#] - Export blob from remote sql server and save to file on disk where sql server is located [C#]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM