在部署应用程序后,无法使用C#中的interop open库将数据导出到excel文件

时间:2014-12-10 19:03:07

标签: c# excel excel-interop

我正在使用Visual Studio 2013中的C#windows应用程序,该应用程序具有使用interop excel开放库将数据导出到excel文件的功能。 它在我的开发机器上使用时工作正常,但是当我在具有Office 2013的用户机器上部署它时失败我得到以下错误

enter image description here

不知道为什么它让我误解了这些错误,详情如下

我尝试了很多方法但未能解决这个问题

另外,我导出数据的代码如下

 // 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();

我可以知道解决方法吗?

1 个答案:

答案 0 :(得分:0)

  

您使用的是第三方软件,如AddinExpress和OffiSync   可能与Office 2013不兼容或过时。 (Source

让用户从Excel中删除不兼容的插件。