C# 将Sql数据导出到Excel的速度非常慢
我正在将Sql数据导出到Excel。我目前使用的代码是:C# 将Sql数据导出到Excel的速度非常慢,c#,sql-server-2008-r2,export,export-to-excel,C#,Sql Server 2008 R2,Export,Export To Excel,我正在将Sql数据导出到Excel。我目前使用的代码是: DataTable dt = new DataTable(); // Create sql connection string string conString = "Data Source=DELL\\SQLSERVER1;Trusted_Connection=True;DATABASE=Zelen;CONNECTION RESET=FALSE"; Sql
DataTable dt = new DataTable();
// Create sql connection string
string conString = "Data Source=DELL\\SQLSERVER1;Trusted_Connection=True;DATABASE=Zelen;CONNECTION RESET=FALSE";
SqlConnection sqlCon = new SqlConnection(conString);
sqlCon.Open();
SqlDataAdapter da = new SqlDataAdapter("select LocalSKU,ItemName, QOH,Price,Discontinued,CAST(Barcode As varchar(25)) As Barcode,Integer2,Integer3,ISNULL(SalePrice,0.0000)AS SalePrice,SaleOn,ISNULL(Price2,0.0000)AS Price2 from dbo.Inventory", 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);
int i = 1;
int j = 1;
int s = 1;
//header row
foreach (DataColumn col in dtMainSQLData.Columns)
{
ExcelApp.Cells[i, j] = col.ColumnName;
j++;
ExcelApp.Rows.AutoFit();
ExcelApp.Columns.AutoFit();
}
i++;
//data rows
foreach (DataRow row in dtMainSQLData.Rows)
{
for (int k = 1; k < dtMainSQLData.Columns.Count + 1; k++)
{
ExcelApp.Cells[i, k] = "'" + row[k - 1].ToString();
}
i++;
s++;
Console.Write(s);
Console.Write("\n\r");
ExcelApp.Columns.AutoFit();
ExcelApp.Rows.AutoFit();
}
var b = Environment.CurrentDirectory + @"\Sheet1.xlsx";
ExcelApp.ActiveWorkbook.SaveCopyAs(b);
ExcelApp.ActiveWorkbook.Saved = true;
ExcelApp.Quit();
Console.WriteLine(".xlsx file Exported succssessfully.");
DataTable dt=newdatatable();
//创建sql连接字符串
string conString=“数据源=DELL\\SQLSERVER1;受信任的\u连接=True;数据库=Zelen;连接重置=FALSE”;
SqlConnection sqlCon=新的SqlConnection(构造);
sqlCon.Open();
SqlDataAdapter da=新SqlDataAdapter(“从dbo.Inventory中选择LocalSKU、ItemName、QOH、Price、Discontracted、CAST(条形码为varchar(25))作为条形码、Integer2、Integer3、ISNULL(SalePrice,0.0000)作为SalePrice、SaleOn、ISNULL(Price2,0.0000)作为Price2);
System.Data.DataTable dtMainSQLData=新的System.Data.DataTable();
da.Fill(dtMainSQLData);
DataColumnCollection dcCollection=dtMainSQLData.Columns;
//将数据导出到EXCEL工作表中
Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp=新的Microsoft.Office.Interop.Excel.ApplicationClass();
ExcelApp.Application.Workbooks.Add(Type.Missing);
int i=1;
int j=1;
int s=1;
//标题行
foreach(dtMainSQLData.Columns中的DataColumn列)
{
ExcelApp.Cells[i,j]=列名称;
j++;
ExcelApp.Rows.AutoFit();
ExcelApp.Columns.AutoFit();
}
i++;
//数据行
foreach(dtMainSQLData.Rows中的DataRow行)
{
对于(int k=1;k
我的sql数据库中有70000行。我正在控制台应用程序中运行此脚本。
将其导出到excel文件需要一个多小时
如何使用此功能更快地导出
示例将不胜感激。如果您将数据保存为CSV共振峰,您可以将其加载到Excel中,以下是我从代码项目站点修改的一些代码
公共类程序
{
静态void Main(字符串[]参数)
{
秒表秒表=新秒表();
秒表。开始();
DataTable dt=新的DataTable();
//创建连接对象
正在使用(SqlConnection conn=newsqlconnection(@“”)
{
//创建命令对象
conn.Open();
使用(SqlCommand cmd=newsqlcommand(“SELECT*FROM”,conn))
{
使用(SqlDataReader=cmd.ExecuteReader())
{
尝试
{
dt.负载(读卡器);
使用(StreamWriter writer=newstreamwriter(“C:\\Temp\\dump.csv”))
{
ToCSV(dt,writer,false);
}
}
捕获(例外)
{
投
}
}
}
}
//停止计时
秒表;
//写入结果
WriteLine(“经过的时间:{0}”),
秒表(已过);
Console.ReadKey();
}
}
公共静态类数据转换
{
公共静态void ToCSV(DataTable sourceTable、TextWriter writer、bool includeHeaders)
{
如果(包括领导)
{
List headerValues=新列表();
foreach(sourceTable.Columns中的DataColumn列)
{
headerValue.Add(QuoteValue(column.ColumnName));
}
writer.WriteLine(String.Join(“,”,headerValue.ToArray());
}
字符串[]项=null;
foreach(sourceTable.Rows中的DataRow行)
{
items=row.ItemArray.Select(o=>QuoteValue(o.ToString()).ToArray();
writer.WriteLine(String.Join(“,”,items));
}
writer.Flush();
}
私有静态字符串QuoteValue(字符串值)
{
返回字符串.Concat(“\”,value.Replace(“\”,“\”),“\”);
}
}
}
在我的电脑上,这花了30秒来处理100万条记录…如果您将数据保存为CSV共振峰,您可以将其加载到Excel中,以下是我从代码项目站点修改的一些代码
公共类程序
{
静态void Main(字符串[]参数)
{
秒表秒表=新秒表();
秒表。开始();
DataTable dt=新的DataTable();
//创建连接对象
正在使用(SqlConnection conn=newsqlconnection(@“”)
{
//创建命令对象
conn.Open();
使用(SqlCommand cmd=newsqlcommand(“SELECT*FROM”,conn))
{
使用(SqlDataReader=cmd.ExecuteReader())
{
尝试
{
dt.负载(读卡器);
使用(StreamWriter writer=newstreamwriter(“C:\\Temp\\dump.csv”))
{
ToCSV(dt,writer,false);
}
}
捕获(例外)
{
投
}
}
}
}
//停止计时
秒表;
//写入结果
Console.WriteLine(“经过的时间:
public class Program
{
static void Main(string[] args)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
DataTable dt = new DataTable();
// Create Connection object
using (SqlConnection conn = new SqlConnection(@"<Your Connection String>"))
{
// Create Command object
conn.Open();
using (SqlCommand cmd = new SqlCommand("SELECT * FROM <Your Table>", conn))
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
try
{
dt.Load(reader);
using (StreamWriter writer = new StreamWriter("C:\\Temp\\dump.csv"))
{
DataConvert.ToCSV(dt, writer, false);
}
}
catch (Exception)
{
throw;
}
}
}
}
// Stop timing
stopwatch.Stop();
// Write result
Console.WriteLine("Time elapsed: {0}",
stopwatch.Elapsed);
Console.ReadKey();
}
}
public static class DataConvert
{
public static void ToCSV(DataTable sourceTable, TextWriter writer, bool includeHeaders)
{
if (includeHeaders)
{
List<string> headerValues = new List<string>();
foreach (DataColumn column in sourceTable.Columns)
{
headerValues.Add(QuoteValue(column.ColumnName));
}
writer.WriteLine(String.Join(",", headerValues.ToArray()));
}
string[] items = null;
foreach (DataRow row in sourceTable.Rows)
{
items = row.ItemArray.Select(o => QuoteValue(o.ToString())).ToArray();
writer.WriteLine(String.Join(",", items));
}
writer.Flush();
}
private static string QuoteValue(string value)
{
return String.Concat("\"", value.Replace("\"", "\"\""), "\"");
}
}
// Get an excel instance
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
// Get a workbook
Workbook wb = excel.Workbooks.Add();
// Get a worksheet
Worksheet ws = wb.Worksheets.Add();
ws.Name = "Test Export";
// Add column names to the first row
int col = 1;
foreach (DataColumn c in table.Columns) {
ws.Cells[1, col] = c.ColumnName;
col++;
}
// Create a 2D array with the data from the table
int i = 0;
string[,] data = new string[table.Rows.Count, table.Columns.Count];
foreach (DataRow row in table.Rows) {
int j = 0;
foreach (DataColumn c in table.Columns) {
data[i,j] = row[c].ToString();
j++;
}
i++;
}
// Set the range value to the 2D array
ws.Range[ws.Cells[2, 1], ws.Cells[table.Rows.Count + 1, table.Columns.Count]].value = data;
// Auto fit columns and rows, show excel, save.. etc
excel.Columns.AutoFit();
excel.Rows.AutoFit();
excel.Visible = true;
// Start a stopwatch to time the process
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
// Check if there are rows to process
if (table != null && table.Rows.Count > 0) {
// Determine the number of chunks
int chunkSize = 100000;
double chunkCountD = (double)table.Rows.Count / (double)chunkSize;
int chunkCount = table.Rows.Count / chunkSize;
chunkCount = chunkCountD > chunkCount ? chunkCount + 1 : chunkCount;
// Instantiate excel
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
// Get a workbook
Workbook wb = excel.Workbooks.Add();
// Get a worksheet
Worksheet ws = wb.Worksheets.Add();
ws.Name = "Test Export";
// Add column names to excel
int col = 1;
foreach (DataColumn c in table.Columns) {
ws.Cells[1, col] = c.ColumnName;
col++;
}
// Build 2D array
int i = 0;
string[,] data = new string[table.Rows.Count, table.Columns.Count];
foreach (DataRow row in table.Rows) {
int j = 0;
foreach (DataColumn c in table.Columns) {
data[i, j] = row[c].ToString();
j++;
}
i++;
}
int processed = 0;
int data2DLength = data.GetLength(1);
for (int chunk = 1; chunk <= chunkCount; chunk++) {
if (table.Rows.Count - processed < chunkSize) chunkSize = table.Rows.Count - processed;
string[,] chunkData = new string[chunkSize, data2DLength];
int l = 0;
for (int k = processed; k < chunkSize + processed; k++) {
for (int m = 0; m < data2DLength; m++) {
chunkData[l,m] = table.Rows[k][m].ToString();
}
l++;
}
// Set the range value to the chunk 2d array
ws.Range[ws.Cells[2 + processed, 1], ws.Cells[processed + chunkSize + 1, data2DLength]].value = chunkData;
processed += chunkSize;
}
// Auto fit columns and rows, show excel, save.. etc
excel.Columns.AutoFit();
excel.Rows.AutoFit();
excel.Visible = true;
}
// Stop the stopwatch and display the seconds elapsed
sw.Stop();
MessageBox.Show(sw.Elapsed.TotalSeconds.ToString());
Public Shared Sub ExportDataSetToExcel(ByVal ds As DataTable, ByVal filename As String)
Dim response As HttpResponse = HttpContext.Current.Response
response.Clear()
response.Buffer = True
response.Charset = ""
response.ContentType = "application/vnd.ms-excel"
Using sw As New StringWriter()
Using htw As New HtmlTextWriter(sw)
Dim dg As New DataGrid()
dg.DataSource = ds
dg.DataBind()
dg.RenderControl(htw)
response.Charset = "UTF-8"
response.ContentEncoding = System.Text.Encoding.UTF8
response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble())
response.Output.Write(sw.ToString())
response.[End]()
End Using
End Using
End Sub