日期:2014-05-17 浏览次数:20838 次
DataSet ds = new DataSet();
try {
//获取全部数据
string strConn = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'", excelFile);
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
strExcel = string.Format("select * from [{0}$]",sheetName);
myCommand = new OleDbDataAdapter(strExcel, strConn);
myCommand.Fill(ds,sheetName);
//*一下为不明白的地方,请哪位懂的帮我家一下注释(注释越多越好)
//如果目标表不存在则创建,excel文件的第一行为列标题,从第二行开始全部都是数据记录
string strSql = string.Format("if object('{0}')is null create table {0}(",sheetName);
foreach(System.Data.DataColumn c in ds.Tables[0].Columns)
{
strSql += string.Format("[{0}]varchar(255),",c.ColumnName);
}
strSql = strSql.Trim(',') + ")";
using (System.Data.SqlClient.SqlConnection sqlconn = new System.Data.SqlClient.SqlConnection(connectionString))
{
sqlconn.Open();
System.Data.SqlClient.SqlCommand command = sqlconn.CreateCommand();
command.CommandText = strSql;