2016-11-14 312 views
0

我有一個按鈕,在我的winform中,我從數據gridview中導出內容到excel,它可以在我的本地機器上正常工作,但是當我將.exe複製到客戶機並讓他運行excel功能不起作用。導出到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 ************** 

請幫助

回答

0

代碼本身是正確的代碼。

看來,這個錯誤是由於您在調用Excel時很忙或未準備好導致的。客戶端計算機上的Excel未激活,或者某個進程正在運行(請參閱任務管理器)。您可以在this question中找到更多信息

順便說一下,dataGridView1中的第一行會被跳過,不會導出到excel。你可能想解決這個問題。