2017-01-17 79 views
0

我运行了一个存储过程,我想在Excel模板上进行转储。直接将数据表导入Excel C#

它目前的作品,但只是太长时间。在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]; 
      } 
     } 

    } 
+1

writedata_from_proc方法做什么?它的源代码是什么? –

+0

您没有包含来自**'writedata_from_proc' **的主代码。 – ManishChristian

+1

定义'但只是太长了'。 –

回答

1

Excel中已经有一个内置的工具,正是这一点,它需要不再比运行查询和获取结果要好。它被称为MS Query。简而言之,你会:

  1. 转到Excel的功能区中的“数据”选项卡
  2. 选择“获取外部数据”(功能区组) - >“自其他来源” - >“从SQL服务器。”我假设这是SQL Server。从你的语法来看,它可能是Sybase,在这种情况下,你仍然可以通过ODBC(SQL Server下面的几个选项)来做到这一点。
  3. 绕过所有的设计者,你将登陆MS查询窗口。从这里,您可以直接编辑SQL并输入您的SQL - 当您关闭MS Query时,它会询问您要放置数据的位置。选择一个单元格。

最重要的是,当它的时间来刷新,你在桌子上单击鼠标右键,选择“刷新”,它会重新执行你的程序(或查询)。根据我的经验,Excel实际上比大多数数据库浏览器更快地提供数据更快

这里有更多的细节微软链接:

https://support.office.com/en-us/article/Use-Microsoft-Query-to-retrieve-external-data-42a2ea18-44d9-40b3-9c38-4c62f252da2e?ui=en-US&rs=en-US&ad=US&fromAR=1

所以,你不需要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();