问下各位朋友
1.当程序读写Excel文件时,如果用户此时打开Excel文件,程序会崩溃。应该怎么样防止这样的问题啊?
2.C#读取Excel速度方面的优化主要考虑哪些方面,用数据库的方式读取Excel文件会快很多,这种方式和一行行遍历Excel读取有什么不同啊?

解决方案 »

  1.   

    1. 读取数据的代码写在try块里,自定义错误.2. 没试过,不作回答..
      

  2.   

    Excel写入的时候让其Visible = false 写入完成再由程序打开!
      

  3.   

    http://www.worktool.cn/c-JiShu/2008-11/345.htm
      

  4.   

    建议如果数据量很大的话,可以通过OleDb连接,把excel文件作为数据源来读取
      

  5.   

    修改文件名判断是否在使用
    注意EXCEL多sheet,大数据量,分多sheet读取
    sqlbuckcopy导入
      

  6.   


    正好写了个小程序,能解决你的问题,请参考:
    源码:using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Data; 
    using System.Data.OleDb; 
    using System.Drawing; 
    using System.Text; 
    using System.Windows.Forms; namespace btnnum 

        public partial class Form1 : Form 
        { 
            public Form1() 
            { 
                InitializeComponent(); 
            }         //获得的excel文件的名称 
            private string xlsname = string.Empty; 
            //定义DataSet,xls文件将填充到DataSet中 
            DataSet ds = new DataSet(); 
            private void Form1_Load(object sender, EventArgs e) 
            {         }         private void toolStripLabel1_Click(object sender, EventArgs e) 
            { 
                OpenFileDialog ofd = new OpenFileDialog(); 
                ofd.Filter = "Excel文件(*.xls;*.xlsx)|*.xls;*.xlsx"; 
                ofd.Multiselect = false; 
                if (ofd.ShowDialog() == DialogResult.OK) 
                { 
                    xlsname = ofd.FileName; 
                    toolStripComboBox1.Enabled = true; 
                    toolStripStatusLabel1.Text = "已经选择Excel文件: "+xlsname; 
                    toolStripComboBox1.Items.Clear(); 
                    using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + xlsname + ";Extended Properties='Excel 12.0;HDR=NO;IMEX=1';")) 
                    { 
                        conn.Open(); 
                        DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" }); 
                        foreach (DataRow dr in dt.Rows) 
                        { 
                            toolStripComboBox1.Items.Add(dr["TABLE_NAME"]); 
                        } 
                    } 
                } 
                else 
                { 
                    if (xlsname.Length > 0) 
                        toolStripLabel2.Enabled = true; 
                    else 
                        toolStripLabel2.Enabled = false; 
                } 
            }         private void toolStripLabel2_Click(object sender, EventArgs e) 
            { 
                try 
                { 
                    ds.Clear(); 
                    using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + xlsname + ";Extended Properties='Excel 12.0;HDR=NO;IMEX=1'")) 
                    { 
                        using (OleDbDataAdapter da = new OleDbDataAdapter("Select * from ["+toolStripComboBox1.SelectedText+"]", conn)) 
                        { 
                            da.Fill(ds, "xlssheet"); 
                        } 
                    } 
                    dataGridView1.DataSource = ds.Tables["xlssheet"]; 
                    dataGridView1.AutoGenerateColumns = true; 
                    toolStripLabel3.Enabled = true; 
                } 
                catch 
                { 
                    MessageBox.Show("打开EXCEL文件遇到错误,请检查是否合法的excel文件"); 
                } 
            }         private void toolStripLabel3_Click(object sender, EventArgs e) 
            { 
                richTextBox1.Text = "该EXCEL工作表的行列数信息:\n"+"共有: "+dataGridView1.Rows.Count+"行\n共有: "+dataGridView1.Columns.Count+"列"; 
            }         private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e) 
            { 
                if (toolStripComboBox1.SelectedIndex > -1) 
                { 
                    toolStripLabel2.Enabled = true; 
                } 
                else 
                { 
                    toolStripLabel2.Enabled = false; 
                } 
            } 
        } 

    源文件下载:
    http://download.csdn.net/source/3034170
    个人感觉这个小程序稳定性和效率还不错。其中读取EXCEL文件就是分Sheet读的,效率较高。请参考。如有不足请一定提出来,学习+完善,谢谢啊呵呵。