我正在使用Microsoft office 2013
办公室专业人员。因此,当我尝试将数据从c#
导出到Excel
时,会显示以下错误。
The message filter indicated that the application is busy. (Exception from HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER))
但是当我使用office 2007
时,我的代码没有问题。仅在使用office 2013
时发生错误。任何人都可以为此提供合适的解决方案。
以下代码正在使用
Microsoft.Office.Interop.Excel.Worksheet objWorkSheet1 = null;
Microsoft.Office.Interop.Excel.Application objExcel = new Microsoft.Office.Interop.Excel.Application
{
Visible = false,
DisplayAlerts = false,
ScreenUpdating = false
};
Microsoft.Office.Interop.Excel.Workbooks objWorkbooks = objExcel.Workbooks;
Microsoft.Office.Interop.Excel.Workbook objWorkbook = objWorkbooks.Add(Missing.Value);
Microsoft.Office.Interop.Excel.Sheets objSheets = objWorkbook.Worksheets;
Microsoft.Office.Interop.Excel.Range objCells;
Microsoft.Office.Interop.Excel.Range myCell;
var iCurrentRow = 10;
var dt = ds;
int columnsCount = dt.Columns.Count;
objWorkSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)(objSheets[1]);
objCells = objWorkSheet1.Cells;
objWorkSheet1.Cells[2, columnsCount - 1] = ReportCaption;
objWorkSheet1.Cells[2, columnsCount - 1].Font.Size = 15;
objWorkSheet1.Range[objWorkSheet1.Cells[5, 2], objWorkSheet1.Cells[5, columnsCount - 1]].Merge();
objWorkSheet1.Cells[5, 2] = "Printed By: " + Common.Utilities.UserName; ;
objWorkSheet1.Range[objWorkSheet1.Cells[6, 2], objWorkSheet1.Cells[6, columnsCount - 1]].Merge();
objWorkSheet1.Cells[6, 2] = "Date: " + DateTime.Now;
objWorkSheet1.Range[objWorkSheet1.Cells[2, 2], objWorkSheet1.Cells[4, columnsCount - 1]].Merge();
if (name != null)
if (name.Length > 0)
objWorkSheet1.Cells[8, 2] = "NAME: " + name;
objWorkSheet1.get_Range((Microsoft.Office.Interop.Excel.Range)objWorkSheet1.Cells[2, 2], (Microsoft.Office.Interop.Excel.Range)objWorkSheet1.Cells[4, columnsCount - 1]).Cells.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
objWorkSheet1.get_Range((Microsoft.Office.Interop.Excel.Range)objWorkSheet1.Cells[2, 2], (Microsoft.Office.Interop.Excel.Range)objWorkSheet1.Cells[4, columnsCount - 1]).Cells.VerticalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
for (var h = 0; h < dt.Columns.Count; h++)
{
myCell = (Microsoft.Office.Interop.Excel.Range)objCells[iCurrentRow, h + 1];
myCell.Value2 = dt.Columns[h].ColumnName;
}
iCurrentRow++;
for (var r = 0; r < dt.Rows.Count; r++)
{
for (var c = 0; c < dt.Columns.Count; c++)
{
if (dt.Columns[c].DataType.Name == "String" || dt.Columns[c].DataType.Name == "DateTime")
{
myCell = (Microsoft.Office.Interop.Excel.Range)objCells[r + iCurrentRow, c + 1];
myCell.Value2 = "'" + dt.Rows[r][c].ToString().Trim();
}
else
{
myCell = (Microsoft.Office.Interop.Excel.Range)objCells[r + iCurrentRow, c + 1];
myCell.Value2 = dt.Rows[r][c];
}
}
}
objWorkSheet1.Cells.EntireRow.AutoFit();
objWorkSheet1.Cells.EntireColumn.AutoFit();
objWorkbook.SaveAs(savingFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value);
objWorkbook.Close(Missing.Value, Missing.Value, Missing.Value);
objExcel.Quit();