关于 c# 操作 Excel文件中遇到的保存出错问题

做一个程序,里面要用到 Excel 保存, 但是 ,试了好长时间没有成共!郁闷ing

呵呵,现在天快亮了 ,终于出来了,

我的代码:

/// <summary>  
        /// 将DataGridView控件中数据导出到Excel  
        /// </summary>  
        /// <param name="gridView">DataGridView对象</param>  
        /// <param name="isShowExcle">是否显示Excel界面</param>  
        /// <returns></returns>  
        public bool ExportDataGridview(DataGridView gridView, bool isShowExcle, string paths)
        {
            //object temp =Missing.Value;
            if (gridView.Rows.Count == 0)
                return false;
            else
            {
                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)) //判断DataGirdView中数据的类型  
                        {
                            excel.Cells[i + 2, j + 1] = "'" + gridView[j, i].Value.ToString();
                        }
                        else
                        {
                            excel.Cells[i + 2, j + 1] = gridView[j, i].Value.ToString();
                        }
                    }

                    try
                    {
                        paths = paths + "fCData.xls";
                        excel.ActiveWorkbook.SaveCopyAs(paths + "CData.xls");
                        return true;
                    }
                    catch (Exception EX)
                    {
                        throw EX;
                       
                    }
                  

                }
                return true;
            }
        }
        private void buttonExport_Click(object sender, EventArgs e)
        {
             
            string ConfigFile =System.Windows.Forms.Application.StartupPath + @"/ExcelPathSettings.ini";
            if (!File.Exists(ConfigFile))
            {
                MessageBox .Show("导出Excel文件丢失或者你还没有配置路径","警告",MessageBoxButtons.RetryCancel,MessageBoxIcon.Warning);return ;
            }
            else
            {

            FileStream fs = new FileStream(ConfigFile, FileMode.Open, FileAccess.Read);
            StreamReader sr=new StreamReader(fs);
            sr.BaseStream.Seek(0,SeekOrigin.Begin);
                string Filepath=sr.ReadLine();
      
                if(Filepath!=string.Empty)
                {
                      ExportDataGridview(this.dataGridView1, true, Filepath);
                      MessageBox.Show("文件一已经被保存到"+ Filepath+"里面", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return;
                }
                else
                {
                     MessageBox .Show("配置路径不正确","警告",MessageBoxButtons.RetryCancel,MessageBoxIcon.Warning);return ;
                }       

        }
        }

//保存方式一:保存WorkBook
xBook.SaveAs(@"D:/CData.xls",
Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,
Excel.XlSaveAsAccessMode.xlNoChange,Missing.Value,Missing.Value,Missing.Value,
Missing.Value,Missing.Value);

//保存方式二:保存WorkSheet
xSheet.SaveAs(@"D:/CData2.xls",
Missing.Value,Missing.Value,Missing.Value,Missing.Value,
Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);

//保存方式三
xBook.Save();
xSheet=null;
xBook=null;
xApp.Quit(); //这一句是非常重要的,否则Excel对象不能从内存中退出
xApp=null;
}
}
}

方式四:excel.ActiveWorkbook.SaveAs(strExcelFileName, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange,
Excel.XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

参考资料:http://topic.csdn.net/u/20071012/11/9aff9740-a4a4-4b2e-a2d1-14784ad5e1af.html

http://hi.baidu.com/zhangcheng1/blog/item/b296c2438247eb1273f05d74.html

http://bbs.zdnet.com.cn/thread-600288-1-1.html

http://www.bitscn.com/dotnet/c/200806/142691.html

http://blog.csdn.net/hanghwp/archive/2007/04/27/1587724.aspx

http://www.cnblogs.com/9who/archive/2008/07/28/1254413.html

http://topic.csdn.net/t/20020831/10/984982.html

展开阅读全文

没有更多推荐了,返回首页