通過OleDbConnection的方法GetOleDbSchemaTable獲取獲取Excel的Sheet名
/// <summary> /// 獲取Excel的Sheet名 /// </summary> /// <param name="strExcelPath"></param> /// <returns></returns> public static List<string> GetExcelSheetNames(string strExcelPath) { List<string> lstSheetNames = new List<string>(); try { string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strExcelPath + ";" + "Extended Properties=Excel 8.0;"; string strSheetName = ""; using (OleDbConnection con = new OleDbConnection(strCon)) { con.Open(); DataTable excelDataTableDescribe = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);//dtbl1 rows.Count Sheet個數 第3列為 Sheet名 if (excelDataTableDescribe == null || excelDataTableDescribe.Columns.Count < 3 || excelDataTableDescribe.Rows.Count == 0) return lstSheetNames; foreach (DataRow row in excelDataTableDescribe.Rows) { strSheetName = row[2].ToString().Trim();//row["TABLE_NAME"].ToString(); lstSheetNames.Add(strSheetName); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } return lstSheetNames; }
通過OleDbDataAdapter的Fill方法將數據填充到DataTable
public static DataTable GetData(string strPath, string strSheetName) { DataTable dtbl = new DataTable(); try { string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strPath + ";" + "Extended Properties=Excel 8.0;"; String strCmd = "select * from [" + strSheetName + "]"; OleDbDataAdapter cmd = new OleDbDataAdapter(strCmd, strCon); cmd.Fill(dtbl); } catch (Exception ex) { MessageBox.Show(ex.Message); } return dtbl; }
OracleBulkCopy
/// <summary> /// 批量插入數據 /// </summary> /// <param name="dt">數據表</param> /// <param name="targetTable">模板表名</param> /// <param name="dicStrFields">數據表字段與目標表字段映射</param> /// <param name="db">數據庫連接</param> /// <returns></returns> public static bool BulkToDB(DataTable dt, string targetTable, Dictionary<string, string> dicStrFields, DBUtility.IDBConnection db) { if (dt == null || dt.Rows.Count == 0) return false; Application.DoEvents(); int index = Regex.Matches(db.ConnectionString, ";").OfType<Match>().Select(t => t.Index).Max(); string connstr = db.ConnectionString.Substring(0, index + 1); OracleBulkCopy bulkCopy = new OracleBulkCopy(connstr, OracleBulkCopyOptions.UseInternalTransaction); //用其它源的數據有效批量加載Oracle表中 bulkCopy.BatchSize = 100000; bulkCopy.BulkCopyTimeout = 260; bulkCopy.DestinationTableName = targetTable; //服務器上目標表的名稱 bulkCopy.BatchSize = dt.Rows.Count; //每一批次中的行數 try { //foreach (DataColumn column in dt.Columns) foreach (KeyValuePair<string, string> kv in dicStrFields) { bulkCopy.ColumnMappings.Add(kv.Value, kv.Key);//源列名->目標列名 //bulkCopy.ColumnMappings.Add(column.ColumnName, column.ColumnName);//源列名->目標列名 } bulkCopy.WriteToServer(dt); //將提供的數據源中的所有行復制到目標表中 return true; } catch (Exception ex) { throw ex; } finally { if (bulkCopy != null) bulkCopy.Close(); } }
//不讀取Excel第一行 string strConHDRYes = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strPath + ";" + "Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'"; //讀取第一行 string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strPath + ";" + "Extended Properties='Excel 8.0;HDR=No;IMEX=1'"; OleDbDataAdapter cmd = new OleDbDataAdapter(strCmd, strCon);//列名 cmd.Fill(ds,0,1 ,"res");
HDR=Yes 不讀取第一行
不同的Excel版本使用的鏈接字符串不一樣 注意:
"Extended Properties='Excel 8.0;HDR=No;IMEX=1'";這里有個單引號哦
case ".xls":
//string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strPath + ";" + "Extended Properties=Excel 8.0;";
//第一行默認為列名 HDR=Yes時不讀取第一行,DataTable的ColumnName可得到列名
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strExcelPath + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
break;
case ".xlsx":
strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strExcelPath + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1;'";
break;
default:
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strExcelPath + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
break;
