Object Nothing = Missing.Value;
            //WdSaveFormat 为Excel文档的保存格式 
            object format = Excel.XlFileFormat.xlOpenXMLWorkbook;
            Excel.XlFileFormat version = Excel.XlFileFormat.xlExcel8;
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = "Excel文档(*.xlsx)|*.xlsx|Excel97-2003文档(*.xls)|*.xls";
            if (System.Windows.Forms.DialogResult.OK == openFile.ShowDialog())
            {
                string path = openFile.FileName;//文件路径变量 
                if (System.IO.Path.GetExtension(path).ToLower() == ".xlsx")
                {
                    version = Excel.XlFileFormat.xlOpenXMLWorkbook;
                }
                try
                {
                    Excel.Application excelApp = new Excel.Application();               //Excel应用程序变量 
                    Excel.Workbook excelBook = excelApp.Workbooks.Open(path, version, Nothing, Nothing, Nothing, Nothing, Excel.XlSaveAsAccessMode.xlExclusive, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing);
                    Excel.Worksheet excelSheet = (Excel.Worksheet)excelApp.ActiveSheet;
                    Excel.Range test = (Excel.Range)excelSheet.Cells[1, 1];
                    MessageBox.Show(test.Value2.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Excel创建失败!\n原因:" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

解决方案 »

  1.   

    下断点调试看哪一行报错。也可能是dll版本与当前安装office版本不一样引起的异常。
      

  2.   

    但是我写Excel是正常的//由于使用的是COM库,因此有许多变量需要用Nothing 代替 
                    Object Nothing = Missing.Value;
                    //WdSaveFormat 为Excel文档的保存格式 
                    object format = Excel.XlFileFormat.xlOpenXMLWorkbook;
                    Excel.XlFileFormat version = Excel.XlFileFormat.xlExcel8;
                    SaveFileDialog saveexcel = new SaveFileDialog();
                    saveexcel.Filter = "Excel文档(*.xlsx)|*.xlsx|Excel97-2003文档(*.xls)|*.xls";
                    if (saveexcel.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        this.SystemStatus.Text = "数据正在导出中,请稍后...";
                        string path = saveexcel.FileName;//文件路径变量 
                        //保存为2007的格式
                        if (System.IO.Path.GetExtension(saveexcel.FileName).ToLower() == ".xlsx")
                        {
                            version = Excel.XlFileFormat.xlOpenXMLWorkbook;
                        }                    if (System.IO.File.Exists(path))
                        {
                            System.IO.File.Delete(path);
                        }
                        try
                        {
                            Excel.Application excelApp = new Excel.Application();               //Excel应用程序变量 
                            if (null == excelApp)
                            {
                                MessageBox.Show("无法创建Excel对象,可能计算机未安装Excel", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                return;
                            }
                            Excel.Workbooks excelWorkBooks = excelApp.Workbooks;          //Excel文档变量
                            Excel.Workbook excelWorkBook = excelWorkBooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
                            Excel.Worksheet excelSheet = (Excel.Worksheet)excelWorkBook.Worksheets[1];
                            //excelSheet.Name = "test";
                            excelSheet.Cells[1, 1] = "123";
                            //将 excelDoc文档对象的内容保存为XLSX文档   
                            excelWorkBook.SaveAs(path, version, Nothing, Nothing, Nothing, Nothing, Excel.XlSaveAsAccessMode.xlExclusive, Nothing, Nothing, Nothing, Nothing, Nothing);
                            //关闭excelDoc文档对象 
                            excelWorkBook.Close(Nothing, Nothing, Nothing);
                            //关闭excelApp组件对象
                            excelApp.Quit();
                            this.SystemStatus.Text = "";
                            MessageBox.Show("数据导出成功", "", MessageBoxButtons.OK);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Excel创建失败!\n原因:" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
      

  3.   

    但是我写excel是正常,代码附上。
      

  4.   

    excelWorkBook.SaveAs(path, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing);
    这样就可以了,自己搞定了