Export to Excel Call Rejected

0

I have a button on my winform where I export the contents from a data gridview to excel it works fine on my local machinebut when I copied the .exe over to the client machine and let him run the excel functionality is not working.

this is the code for exporting to 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;
 } 
 }

The error on client machine

************** 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 **************

PLEASE HELP

c#
winforms
datagridview
export-to-excel
asked on Stack Overflow Nov 14, 2016 by CodeMan

1 Answer

0

The code itself is correct.

It seems that the error is caused by Excel being busy or not ready at the time you invoke it. Either the Excel on client machine is unactivated, or some process is running(see task manager). You can find more information in this question

By the way, the first row in dataGridView1 is skipped and will not be exported to excel. You might want to fix that.

answered on Stack Overflow Nov 15, 2016 by Jonathan Tsai • edited May 23, 2017 by Community

User contributions licensed under CC BY-SA 3.0