
excel
以下是一个C#代码示例,用于将
excel文件中的数据导入到SQL数据库中:csharpusing System;using System.Data.OleDb;using System.IO;using System.Linq;using System.Data.Table elm;public static void Import
excelTo
Database(string filePath, string connectionString){ if (!File.Exists(filePath)) { throw new FileNotFoundException("File not found", filePath); } if (connectionString == string.Empty) { connectionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " + filePath + "; Extended Properties=
excel 8.0;HDR=Yes;IMEX=1"; } DataTable table = GetDataTable(filePath); if (table == null || table.Rows.Count == 0) { return; } string[] tableNames = GetTableNameFrom
excelTable(table); foreach (string tableName in tableNames) { using (OleDbConnection connection = new OleDbConnection(connectionString)) { connection.Open(); using (OleDbCommand command = new OleDbCommand()) { command.Connection = connection; command.CommandText = "select * from " + tableName; DataTable dt = FillDataTable(command); string tableNameInDB = "dbo." + tableName; if (dbtableNames.Cont
AIns(tableNameInDB) == false) { dbtableNames.Add(tableNameInDB); } connection.Execute("CREATE TABLE " + tableNameInDB + " ("); foreach (DataColumn column in dt.Columns) { connection.Execute("COLUMN " + column.ColumnName + " " + column-buttons[0]); } for (int i = 0; i < dt.Rows.Count; i++) { connection.Execute("ROW " + (i+1)); foreach (DataRow row in dt.Rows[i]) { foreach (object o in row.ItemArray) { connection.Execute(o.ToString()); } } } connection.Execute(")"); } } }}public static DataTable GetDataTable(string filePath){ DataTable table = null; if (filePath.EndsWith(".xls")) { table = (DataTable)System.IO.File.OpenText(filePath); return table; } else if (filePath.EndsWith(".xlsx")) { %>