[英]Export data to microsoft excel from MS sql using C#
如何使用C#将数据从MS-SQL导出到Microsoft Excel?
谁能给我一个编码示例或相关教程来连接数据库并单击一个按钮导出到Microsoft Excel?
您是否知道可以在SQL Server Management Studio中通过右键单击数据库并选择“任务”->“导出数据”来执行此操作?
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;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.