Excel.Application mydataApp = new Excel.Application();
mydataApp.Visible =false;
mydataApp.DisplayAlerts=false;
Workbooks workbooks = mydataApp.Workbooks;

_Workbook workbook =workbooks.Open(strFile,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);
Open的时候总是听是无法访问xx.xls,我的代码是从另外一个项目中拷贝的,那里可以用,这里不知道怎么回事急死了,文件的属性也不是只读的...

解决方案 »

  1.   

    你的XX.xls物理文件存在吗?把你的调用这个函数的代码贴出来
      

  2.   

    多谢 问题已经解决了 因为此代码前有连接Excel文件的地方 后有没有关闭 所以这里就打不开了 多谢啊 新手学习中 呵呵
      

  3.   

     private bool ExportDataGridview(DataGridView gridView, bool isShowExcle)
            {
                if (gridView.Rows.Count == 0)
                    return false;
                //建立EXCEL对象 
                Excel.Application excel = new Excel.Application();
                excel.Application.Workbooks.Add(true);
                excel.Visible = isShowExcle;
                //生成地址字段名
                for (int i = 0; i < gridView.ColumnCount; i++)
                    {
                        excel.Cells[1, i + 1] = gridView.Columns[i].HeaderText;
                    }
                //填充数据
                    for (int i = 0; i <=gridView.RowCount - 1; i++)
                    {
                        for (int j = 0; j < gridView.ColumnCount; j++)
                        {
                            if (gridView[j, i].ValueType == typeof(string))
                            {
                                excel.Cells[i + 2, j + 1] = "" + gridView[j, i].Value.ToString();
                            }
                        else
                            {
                                excel.Cells[i + 2, j + 1] = gridView[j, i].Value.ToString();
                            }
                         }
                     }
                return true;
            }        private void button3_Click(object sender, EventArgs e)
            {
                this.ExportDataGridview(dataGridView1, true);
            }你看这个可以吗, 我也是刚从书上弄过的, 能导到EXCEL中的 
      

  4.   

    资源管理器中把所有的excel进程全部杀掉!就ok了、
      

  5.   

    可以的应该。 再贴个,前些天刚看到的:
     public void ImportData()
            {
                string strFileName;            Microsoft.Office.Interop.Excel.ApplicationClass MyExcel;
                Microsoft.Office.Interop.Excel.Workbooks wbs;
                Microsoft.Office.Interop.Excel.Workbook wb;
                Microsoft.Office.Interop.Excel.Worksheet ws;            this.ImportFileDialog = new System.Windows.Forms.OpenFileDialog();            ImportFileDialog.Filter = "Excel Files(*.xls)|*.xls";
                if (ImportFileDialog.ShowDialog() == DialogResult.OK)
                    if (ImportFileDialog.CheckFileExists)
                    {
                        strFileName = ImportFileDialog.FileName;                    MyExcel = new Microsoft.Office.Interop.Excel.ApplicationClass();                    wbs = MyExcel.Workbooks;
                        wb = wbs.Open(strFileName,
                            Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                            Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                            Type.Missing, Type.Missing, Type.Missing, Type.Missing);                    ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Sheets[1];                    int Row = EXCEL_START_ROW;                    while (true)
                        {
                            bool BlankRow = true;
                            string CellData;                        string ErrMsg;
                            bool[] Err;                        DataTable.RowInfo NewRow = this.NewRow();                        for (int i = 0; i < ColInfos.Length; i++)
                            {
                                if (((Microsoft.Office.Interop.Excel.Range)ws.Cells[Row, i + 1]).Value2 == null)
                                    CellData = "";
                                else
                                    CellData = ((Microsoft.Office.Interop.Excel.Range)ws.Cells[Row, i + 1]).Value2.ToString();                            if (CellData != "")
                                    BlankRow = false;
                                //caizy2008/12/26改 日期从EXCEL读入的数值转换成日期------------------------------------
                                if (ColInfos[i].DataType == DataTable.CellType.DATETIME && CellData!="")
                                {
                                    //DateTime theDate = new DateTime(long.Parse(CellData));
                                    DateTime theDate =DateTime.FromOADate(double.Parse(CellData));
                                    NewRow.Data[i] = theDate.ToString();
                                    //Convert.ToDateTime("19000100").Date.AddDays(double.Parse(CellData));
                                }
                                else
                                {
                                    NewRow.Data[i] = CellData;
                                }
                                //caizy2008/12/26改------------------------------------                        }                        if (BlankRow)
                                break;                        for (int i = 0; i < ColInfos.Length; i++)
                            {
                                if (ColInfos[i].Level==0 && NewRow.Data[i]=="")
                                {
                                    NewRow.Data[i] = Common.IdControl.GetNewId(dataSource, ColInfos[i].ColId);
                                }
                            }                        ddc = new DataDealCenter.dataDealCenter();
                            ddc.Mode = DataDealCenter.dataDealCenter.OPERATE_MODE.NEW;                        if (ddc._Check(this, null, NewRow, out Err, out ErrMsg, false))
                                ddc._Update(this, null, NewRow, out ErrMsg, false);                        Row++;
                        }                    wb.Saved = true;
                        //MyExcel.Save(strFileName);
                        wb.Close(Type.Missing, Type.Missing, Type.Missing);
                        MyExcel.Quit();                    Common.MessageControl.ShowMsg(10074, MessageControl.MSG_TYPE.INFOMATION);
                    }
            }