使用c#将表从Sql Server导出到PDF文件
Posted
技术标签:
【中文标题】使用c#将表从Sql Server导出到PDF文件【英文标题】:Export Table from Sql Server to PDF file using c# 【发布时间】:2014-05-26 20:18:43 【问题描述】:我在 sql 中有几个表,我想在单击表单按钮后将它们导出为 PDF 文件 有谁知道我该怎么做?
当我将表格从 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();
【问题讨论】:
【参考方案1】:“导出为 PDF”是什么意思?使用上面的excel导出然后使用PDF打印机发送文件的打印命令不是最简单的方法吗? 如果您想以本机方式创建 PDF,您很可能会花一些精力来布局文档。
//编辑:只要对 SO 进行一点研究,也会提出这个问题:Best C# API to create PDF
【讨论】:
我想选择以 PDF 格式保存文件 那么就这样吧!? ***.com/questions/13233359/… 他的问题是另一笔交易。我只是说函数 openWorkBook.ExportAsFixedFormat(ExcelApp.XlFixedFormatType.xlTypePDF, pdfPathName); 我看不懂怎么写,你能解释一下吗?谢谢 你有什么不明白的?而不是 .SaveCopyAs() 您使用 .ExportAsFixedFormat()【参考方案2】:参考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());
【讨论】:
以上是关于使用c#将表从Sql Server导出到PDF文件的主要内容,如果未能解决你的问题,请参考以下文章
使用 Golang API 将表从 Amazon RDS 导出到 csv 文件
将表从 Oracle 转移到 SQL Server 的方法都有哪些
将表从 SQL Server 2008 复制到 MS Access 2007