[英]Datatable directly to Excel C#
我运行了要在Excel模板上转储的存储过程。
目前可以使用,但是花费的时间太长。 在SQL Server Management Studio中,查询运行良好,但是当我只写模板时,它确实很慢。
谁能建议一种更有效的方法来达到相同的结果?
这是我的代码的一部分:
sdate = StartDate.Value.ToString();
edate = EndDate.Value.ToString();
Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet aSheet;
try
{
//Start Excel and get Application object.
oXL = new Excel.Application();
oXL.Visible = true;
//open the excel template
oWB = oXL.Workbooks.Open("C:\\TEMP\\template.xlsm");
//oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
//Call to service
//aSheet = (Excel._Worksheet)oWB.Worksheets.get_Item(1);
aSheet = (Excel._Worksheet)oWB.ActiveSheet;
//backgroundWorker1.ReportProgress(i++);
writedata_from_proc(aSheet, "dbo.CODE_RED_2017");
//backgroundWorker1.ReportProgress(i++);
//Make sure Excel is visible and give the user control
//of Microsoft Excel's lifetime.
//backgroundWorker1.ReportProgress(i++);
MessageBox.Show("Data extraction complete");
oXL.Visible = true;
oXL.UserControl = true;
//SaveExcel(oWB);
//clean up the COM objects to remove them from the memory
Marshal.FinalReleaseComObject(aSheet);
Marshal.FinalReleaseComObject(oWB);
Marshal.FinalReleaseComObject(oXL);
}
catch (Exception theException)
{
String errorMessage;
errorMessage = "Error: ";
errorMessage = String.Concat(errorMessage, theException.Message);
errorMessage = String.Concat(errorMessage, " Line: ");
errorMessage = String.Concat(errorMessage, theException.Source);
MessageBox.Show(errorMessage, "Error");
}
}
这是我最初错过的代码:
public void writedata_from_proc(Excel._Worksheet oWS,string sentproc)
{
int rowCount = 0;
SqlCommand cmd = new SqlCommand(sentproc.ToString());
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@start_date", SqlDbType.DateTime).Value = getsdate();
cmd.Parameters.Add("@end_date",SqlDbType.DateTime).Value=getedate();
cmd.CommandTimeout = 0;
DataTable dt = GetData(cmd);
oWS.UsedRange.Rows.Count.ToString();
if (sentproc.Contains("CODE_RED"))
{
rowCount = 1;
}
else
{
rowCount = oWS.UsedRange.Rows.Count;
}
foreach (DataRow dr in dt.Rows)
{
rowCount += 1;
for (int i = 1; i < dt.Columns.Count + 1; i++)
{
// Add the header the first time through
if (rowCount == 2)
{
oWS.Cells[1, i] = dt.Columns[i - 1].ColumnName;
}
oWS.Cells[rowCount, i] = dr[i - 1];
}
}
}
Excel已经有一个内置工具可以执行此操作,并且它所花的时间比运行查询和获取结果所需的时间短。 它称为MS查询。 简而言之,您将:
exec dbo.CODE_RED_2017
最重要的是,当需要刷新时,右键单击表并选择“刷新”,它将重新执行您的过程(或查询)。 以我的经验,Excel实际上比大多数数据库浏览器更快地呈现数据。
这是带有更多详细信息的Microsoft链接:
因此,您根本不需要C#。 就是说,如果您要通过C#以某种方式自动执行此操作,则也可以通过这种方式完成。 这是一个例子:
string sql = "exec dbo.CODE_RED_2017";
string source = "your connection string here";
Excel.Range r = activeSheet.Range["A1"];
Excel.ListObject lo = sheet.ListObjects.AddEx(Excel.XlListObjectSourceType.xlSrcQuery,
source, true, Excel.XlYesNoGuess.xlGuess, r);
lo.QueryTable.CommandText = sql;
lo.Refresh();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.