2014-04-12 41 views
0

我有幾個表中的sql,我想將它們導出爲PDF文件後,我單擊表格按鈕 沒有人知道我該怎麼做?從Sql Server導出到PDF文件使用c#

我有這樣的代碼,當我從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(); 
} 

回答

0

你是什麼意思的「導出爲PDF」嗎?不是使用上面的excel導出,然後使用PDF打印機發送文件的打印命令最簡單的方法嗎? 如果您想本機創建PDF,您很可能會花費一些努力來佈局文檔。

//編輯:只是那麼一點點研究會提出這也:Best C# API to create PDF

+0

我想有救選項PDF格式的文件 – user3082812

+0

那麼就這樣吧!? http://stackoverflow.com/questions/13233359/saving-file-as-pdf-by-using-microsoft-office-interop-excel – silent

+0

它不工作,所以... – user3082812

0

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

}

相關問題