C#讀取Excel資料兩種方式效能對比
阿新 • • 發佈:2019-02-04
方式一:程式讀取Excel資料,程式碼如下:
- Excel.Application m_ExcelFile = new Excel.Application();
- Excel._Workbook m_Workbook;
- Excel._Worksheet m_Worksheet;
- object missing = System.Reflection.Missing.Value;
- Console.WriteLine("excelFilePath:" + excelFilePath);
- m_ExcelFile.Workbooks.Open(excelFilePath, missing, missing, missing, missing, missing, missing, missing, missing, missing
- , missing, missing, missing, missing, missing);
- m_ExcelFile.Visible = false;
- m_Workbook = m_ExcelFile.Workbooks[1];
- m_Worksheet = (Excel.Worksheet)m_Workbook.ActiveSheet;
- int clomn_Count = m_Worksheet.UsedRange.Columns.Count;
- int row_Count = m_Worksheet.UsedRange.Rows.Count;
- for (int i = 2; i < row_Count + 1; i++)//
- {
- string lv_strSql;
- string lv_strSqlOne = "insert into user (";
- string lv_strSqlTwo = " value(";
- for (int j = 1; j < clomn_Count + 1; j++)
- {
- if (((Excel.Range)m_Worksheet.UsedRange.Cells[1, j]).Text.ToString() == "會員姓名" && ((Excel.Range)m_Worksheet.UsedRange.Cells[i, j]).Text.ToString().Trim() !=
- {
- lv_strSqlOne += "name,";
- lv_strSqlTwo += "'" + ((Excel.Range)m_Worksheet.UsedRange.Cells[i, j]).Text.ToString() + "',";
- }
- .........//表格可能有好多列
- elseif (((Excel.Range)m_Worksheet.UsedRange.Cells[1, j]).Text.ToString() == "累計積分" && ((Excel.Range)m_Worksheet.UsedRange.Cells[i, j]).Text.ToString().Trim() != "")
- {
- lv_strSqlOne += "score,";
- lv_strSqlTwo += "'" + ((Excel.Range)m_Worksheet.UsedRange.Cells[i, j]).Text.ToString() + "',";
- }
- }
- lv_strSqlOne += "create_date,sync_flag)";
- lv_strSqlTwo += "'" + DateTime.Now + "',0)";
- lv_strSql = lv_strSqlOne + lv_strSqlTwo;
- Console.WriteLine("lv_strSql:" + lv_strSql);
- try
- {
- int lv_ret = m_db.RunNoQuery(lv_strSql);//執行資料庫插入操作。
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- //關閉Excel相關物件
- m_Worksheet = null;
- m_Workbook = null;
- m_ExcelFile.Quit();
- m_ExcelFile = null;
第二種方式:直接把Excel當作資料庫,查詢Excel的資料,程式碼如下:
- String source = null;
- OdbcConnection conn = null;
- string sql = "select * from [Sheet1$]";
- try
- {
- source = "Driver={Microsoft Excel Driver (*.xls)};DBQ=" + tbExcelFilePath.Text;
- conn = new OdbcConnection(source);
- conn.Open();
- }
- catch (OdbcException e)
- {
- try
- {
- source = "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" + tbExcelFilePath.Text;
- conn = new OdbcConnection(source);
- conn.Open();
- }
- catch (OdbcException e1)
- {
- MessageBox.Show("請確認此檔案沒有被其它程式開啟!");
- }
- }
- try
- {
- OdbcCommand cmd = new OdbcCommand(sql, conn);
- OdbcCommand cmd1 = new OdbcCommand("select count(*) from [Sheet1$]", conn);
- OdbcDataReader read = cmd.ExecuteReader();
- int count = int.Parse(cmd1.ExecuteScalar().ToString());
- int rn = 1;
- while (read.Read())
- {
- try
- {
- if (m_stop) break;
- rn++;
- string lv_strSql;
- string lv_strSqlOne = "insert into user (";
- string lv_strSqlTwo = " value(";
- String[] row = new String[read.FieldCount];
- for (int i = 0; i < read.FieldCount; i++)
- {
- row[i] = read.GetValue(i).ToString();
- if (read.GetName(i) == "會員姓名" && read.GetValue(i).ToString().Trim() != "")
- {
- lv_strSqlOne += "name,";
- lv_strSqlTwo += "'" + read.GetValue(i).ToString() + "',";
- }
- ............//Excel可能有多列
- elseif (read.GetName(i) == "累計積分" && read.GetValue(i).ToString().Trim() != "")
- {
- lv_strSqlOne += "score,";
- lv_strSqlTwo += "'" + read.GetValue(i).ToString() + "',";
- }
- }
- lv_strSqlOne += "create_date,sync_flag)";
- lv_strSqlTwo += "'" + DateTime.Now + "',0)";
- lv_strSql = lv_strSqlOne + lv_strSqlTwo;
- Console.WriteLine("lv_strSql:" + lv_strSql);
- int lv_ret = m_db.RunNoQuery(lv_strSql);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- }
- read.Close();
- conn.Close();
- }
- catch (Exception e)
- {
- MessageBox.Show(e.Message);
- }
在效率上第一種方式比第二種慢很多,推薦使用第二種。