日期:2014-05-18  浏览次数:21001 次

c#遍历Excel中的数据什么方式最快?
我做一个将Excel中的某列汉字转换成拼音后,放到同一Excel中指定的某列,这就需要先遍历数据,但是EXCEL有60000多行,整个的遍历要花相当长的时间.我是这么做的:
  Microsoft.Office.Interop.Excel.Application appExcel = null;//创建EXCEL对像
  Microsoft.Office.Interop.Excel.Workbook WorkBook;//创建工作薄
  Microsoft.Office.Interop.Excel.Worksheet ws = null;//创建工作表
  //遍历数据:
  for(int i=1;i<=ws.rows.count;i++)
  {
  //如果有值就读出来
  txt=ws.cell[i,"指定列"];
  }

但是速度很慢,怎么解决呢?



------解决方案--------------------
C# code
string filename = "";
            if (DialogResult.OK == saveFileDlg.ShowDialog())
            {
                filename = saveFileDlg.FileName;
                //execl 导出
                String source = null;
                OdbcConnection conn = null;

                try
                {
                    source = "Driver={Microsoft Excel Driver (*.xls)};FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\""+filename +"\";DBQ=" + filename ;
                    conn = new OdbcConnection(source);
                    conn.Open();
                }
                catch
                {
                    try
                    {
                        source = "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\""+filename +"\";DBQ=" + filename ;
                        conn = new OdbcConnection(source);
                        conn.Open();
                    }
                    catch
                    {
                        MessageBox.Show("请确认此文件没有被其它程序打开!");
                        return;
                    }
                }

                if (dataSet1.Tables.Count <= 0) return;
                try
                {
                    string s = "";
                    string f = "";
                    string[] numlx = new string[] { "System.Int32", "System.Int16", "System.Int64", "System.Decimal", "System.Single", "System.Double" };

                    for (int i = 0; i < dataSet1.Tables[0].Columns.Count; i++)
                    {
                        if (numlx.Contains(dataSet1.Tables[0].Columns[i].DataType.ToString()))
                        {
                            s = s + ",[" + dataSet1.Tables[0].Columns[i].ColumnName + "] NUMBER";
                        }
                        else
                        {
                            s = s +",[" +dataSet1.Tables[0].Columns[i].ColumnName + "] TEXT";
                        }
                        f = f + ",[" + dataSet1.Tables[0].Columns[i].ColumnName+"]";
                    }
                    s = "CREATE TABLE " + dataSet1.Tables[0].TableName + "(" + s.Substring(1) + ")";
                    f = "insert into " + dataSet1.Tables[0].TableName + "(" + f.Substring(1) + ") values(";


                    OdbcCommand cmd1 = new OdbcCommand(s, conn);
                    cmd1.ExecuteNonQuery();
                    foreach (DataRow dr in dataSet1.Tables[0].Rows)
                    {
                        string sz = "";
                        for (int i = 0; i < dataSet1.Tables[0].Columns.Count; i++)
                        {
                            if (dr[i] != DBNull.Value)
                            {
                                sz = sz + ",'" + dr[i].ToString() + "'";
                            }
                            else
                            {
                                sz = sz + ",null";
                            }
                        }