导出到Excel呼叫被拒绝

时间:2016-11-14 22:07:47

标签: c# winforms datagridview export-to-excel

我在winform上有一个按钮,我将内容从数据gridview导出到excel,它在我的本地机器上工作正常但是当我将.exe复制到客户端机器并让他运行excel功能时无效。

这是导出到excel的代码

private void ExportToExcel()
 {
    // Creating a Excel object. 
Microsoft.Office.Interop.Excel._Application excel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook workbook = excel.Workbooks.Add(Type.Missing);
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;

try
{
worksheet = workbook.ActiveSheet;
worksheet.Name = "ExportedFromDatGrid";
int cellRowIndex = 1;
int cellColumnIndex = 1;
//Loop through each row and read value from each column. 
  for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
   {
     for (int j = 0; j < dataGridView1.Columns.Count; j++)
     {
    // Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check. 
     if (cellRowIndex == 1)
      {
    worksheet.Cells[cellRowIndex, cellColumnIndex] = dataGridView1.Columns[j].HeaderText;
    statuspanel.Text = "Getting header text";
      }
    else
    {
  worksheet.Cells[cellRowIndex, cellColumnIndex] = dataGridView1.Rows[i].Cells[j].Value.ToString();
  statuspanel.Text = "Getting row text" + '-' + i.ToString() + '-' + j.ToString();
    }
  cellColumnIndex++;
   }
cellColumnIndex = 1;
   cellRowIndex++;
  }
// Getting the location and file name of the excel to save from user.

   SaveFileDialog saveDialog = new SaveFileDialog();
  saveDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*";
   saveDialog.FilterIndex = 2;
 if (saveDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
     {
   workbook.SaveAs(saveDialog.FileName);
   MessageBox.Show("Export Successful");
  }
 }
 catch (System.Exception ex)
  {
  MessageBox.Show(ex.Message);
}
finally
   {
  excel.Quit();
   workbook = null;
    excel = null;
 } 
 }

客户端计算机上的错误

************** Exception Text **************
System.Runtime.InteropServices.COMException (0x80010001): Creating an instance of the COM component with CLSID {00024500-0000-0000-C000-000000000046} from the IClassFactory failed due to the following error: 80010001 Call was rejected by callee. (Exception from HRESULT: 0x80010001 (RPC_E_CALL_REJECTED)).
   at System.Runtime.Remoting.RemotingServices.AllocateUninitializedObject(RuntimeType objectType)
   at System.Runtime.Remoting.Activation.ActivationServices.CreateInstance(RuntimeType serverType)
   at System.Runtime.Remoting.Activation.ActivationServices.IsCurrentContextOK(RuntimeType serverType, Object[] props, Boolean bNewObj)
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.Activator.CreateInstance(Type type)
   at TotalReporting.GetNetWeightTrackingQuery.ExportToExcel() in c:\Users\israa\Documents\Visual Studio 2013\Projects\Learning Windows Forms\TotalReporting\TotalReporting\GetNetWeightTrackingQuery.cs:line 529
   at TotalReporting.GetNetWeightTrackingQuery.button3_Click(Object sender, EventArgs e) in c:\Users\israa\Documents\Visual Studio 2013\Projects\Learning Windows Forms\TotalReporting\TotalReporting\GetNetWeightTrackingQuery.cs:line 498
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************

请帮助

1 个答案:

答案 0 :(得分:0)

代码本身是正确的。

似乎错误是由Excel在您调用时忙或未准备好引起的。客户端计算机上的Excel未激活,或某些进程正在运行(请参阅任务管理器)。您可以在this question

中找到更多信息

顺便说一下,dataGridView1中的第一行被跳过,不会导出到excel。你可能想解决这个问题。