2009-06-13 15 views
-1

C#を使用してMS-SQLからMicrosoft Excelにデータをエクスポートするにはどうすればよいですか?C#を使用してMS SQLからExcelにデータをエクスポートする

データベースに接続してボタンをクリックしてMicrosoft Excelにエクスポートするには、誰かにコーディングや関連チュートリアルのサンプルを提供できますか?

+2

私はグーグルにあなたの正確な質問のタイトルを入力し、多くの例を見つけた... –

+0

質問を尋ねるIIの時間あなたが必要な正確に何を尋ねていない限り文句を言わないあなたの答えを得ます。 – Shoban

答えて

0

SQL Server Management Studioでデータベースを右クリックし、[タスク] - > [データのエクスポート]を選択すると、これを行うことができますか?

-1
using System; 
using System.IO; 
using System.Data.SqlClient; 
using System.Data; 

namespace ExporExcel 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      SqlConnection con = new SqlConnection("Data Source=WINCTRL-KJ8RKFO;Initial Catalog=excel;Integrated Security=True"); 

      DataTable dt = new DataTable(); 
      SqlDataAdapter sda = new SqlDataAdapter("select * from Employee",con); 
      sda.Fill(dt); 
      ExportToExcel(dt); 
     } 

     public static void ExportToExcel(DataTable dtReport, string ExcelFilePath = null) 
     { 

       int ColumnsCount; 

       if (dtReport == null || (ColumnsCount = dtReport.Columns.Count) == 0) 

        throw new Exception("ExportToExcel: Null or empty input table!\n"); 


       Microsoft.Office.Interop.Excel.Application Excel = new Microsoft.Office.Interop.Excel.Application(); 
       Excel.Workbooks.Add(); 


       Microsoft.Office.Interop.Excel._Worksheet Worksheet = Excel.ActiveSheet; 

       object[] Header = new object[ColumnsCount]; 

        for (int i = 0; i < ColumnsCount; i++) 
        Header[i] = dtReport.Columns[i].ColumnName; 

    Microsoft.Office.Interop.Excel.Range HeaderRange = Worksheet.get_Range((Microsoft.Office.Interop.Excel.Range)(Worksheet.Cells[1, 1]), 
    (Microsoft.Office.Interop.Excel.Range)(Worksheet.Cells[1, ColumnsCount])); 

       int RowsCount = dtReport.Rows.Count; 
       object[,] Cells = new object[RowsCount, ColumnsCount]; 

       for (int j = 0; j < RowsCount; j++) 
        for (int i = 0; i < ColumnsCount; i++) 
           Cells[j, i] = dtReport.Rows[j][i]; 

       Worksheet.get_Range((Microsoft.Office.Interop.Excel.Range)(Worksheet.Cells[2, 1]), (Microsoft.Office.Interop.Excel.Range)(Worksheet.Cells[RowsCount + 1, ColumnsCount])).Value = Cells; 
       Excel.Visible = true; 
     } 
     } 
    } 
+0

これは、たくさんのセルを書いている場合、Excelに書き込むにはあまり効果的な方法ではありません。 –

関連する問題