使用C#从MS sql导出数据到microsoft excel

时间:2009-06-13 06:37:02

标签: c# excel

如何使用C#从MS-SQL将数据导出到Microsoft Excel?

任何人都可以给我一个编码或相关教程的示例来连接数据库并单击按钮导出到Microsoft Excel吗?

2 个答案:

答案 0 :(得分:0)

您是否知道可以通过右键单击数据库并选择“任务” - >“导出数据”来在SQL Server Management Studio中执行此操作?

答案 1 :(得分:-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;
        }
      }
   }