I am working on a C# windows application in Visual Studio 2013 which has a feature to export data to an excel file using interop excel open library. It works fine when used on my development machine but fails when I deploy it on a user machine which has Office 2013 I get the following error
Not sure why it threw me that error and details are as follows
I tried many ways but failed to solve this
Also, my code to export data is as follows
// creating Excel Application
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
// creating new WorkBook within Excel application
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
// creating new Excelsheet in workbook
Microsoft.Office.Interop.Excel._Worksheet indexWorkSheet = null;
indexWorkSheet = workbook.Sheets[1];
// changing the name of active sheet
indexWorkSheet.Name = "Index Summary";
indexWorkSheet.get_Range("A1").Style.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
indexWorkSheet.get_Range("A10", "A30").Style.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
string filelocation;
SaveFileDialog SaveFile = new SaveFileDialog();
if (SaveFile.ShowDialog() == DialogResult.OK)
{
filelocation = SaveFile.FileName;
// storing header part in Excel
indexWorkSheet.Shapes.AddPicture(@"C:\Atlas Applications\AtlasPetroleumIndex\CLPetroleumIndex\Images\logo.jpg", MsoTriState.msoFalse, MsoTriState.msoCTrue, 155, 20, 180, 65).LockAspectRatio = MsoTriState.msoCTrue;
// indexWorkSheet
for (int i = 1; i < dataGridViewIndex.Columns.Count + 1; i++)
{
indexWorkSheet.Cells[9, i + 3] = dataGridViewIndex.Columns[i - 1].HeaderCell.Value;
}
for (int i = 1; i < dataGridViewIndex.Rows.Count + 1; i++)
{
indexWorkSheet.Cells[i + 9, 3] = dataGridViewIndex.Rows[i - 1].HeaderCell.Value;
//worksheet2.Cells[i + 1, 1] = dataGridViewPaymentsReceived.Rows[i - 1].HeaderCell.Value;
}
for (int i = 0; i < dataGridViewIndex.Rows.Count; i++)
{
for (int j = 0; j < dataGridViewIndex.Columns.Count; j++)
{
indexWorkSheet.Cells[i + 10, j + 4] = dataGridViewIndex.Rows[i].Cells[j].Value.ToString();
}
}
indexWorkSheet.Cells[7, 2] = lblIndexSummary.Text;
indexWorkSheet.Range[indexWorkSheet.Cells[6, 2], indexWorkSheet.Cells[1, 9]].Merge();
indexWorkSheet.Range[indexWorkSheet.Cells[7, 2], indexWorkSheet.Cells[7, 9]].Merge();
indexWorkSheet.Cells[9, 3].EntireRow.Font.Bold = true;
indexWorkSheet.Cells[7, 3].EntireRow.Font.Bold = true;
for (int i = 1; i < dataGridViewIndex.Rows.Count + 9; i++)
{
indexWorkSheet.Cells[i + 9, 3].Font.Bold = true;
}
string colIndex = "I" + (dataGridViewIndex.Rows.Count + 9).ToString();
Microsoft.Office.Interop.Excel.Range formatRange;
formatRange = indexWorkSheet.get_Range("B8", colIndex);
formatRange.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous,
Microsoft.Office.Interop.Excel.XlBorderWeight.xlMedium, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic,
Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic);
string volumeIndex = "H" + (dataGridViewIndex.Rows.Count + 9).ToString();
Microsoft.Office.Interop.Excel.Range numberFormatRange;
numberFormatRange = indexWorkSheet.get_Range("H10", volumeIndex);
numberFormatRange.NumberFormat="N3";
string alignIndex = "H" + (dataGridViewIndex.Rows.Count + 9).ToString();
Microsoft.Office.Interop.Excel.Range alignRange;
alignRange = indexWorkSheet.get_Range("E9", alignIndex);
alignRange.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignRight;
workbook.SaveAs(filelocation, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
workbook.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, filelocation);
workbook.Close(true, Type.Missing, Type.Missing);
app.Quit();
May I know a way to fix it?
You are using 3rd party software like AddinExpress and OffiSync which are probably incompatible with Office 2013 or out of date. ( Source )
Have the user remove the incompatible addin from Excel.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.