62,047
社区成员
发帖
与我相关
我的任务
分享
string excelConnectionStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";
OleDbConnection olconnection = new OleDbConnection(excelConnectionStr);
olconnection.Open();
OleDbCommand cmd = new OleDbCommand("select * from [" + DropDownList2.SelectedItem.Text.ToString().Trim() + "]", olconnection);
OleDbDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
SqlConnection conn = BuildConnection.Buildsqlconnection();
SqlBulkCopy bulkcopy = new SqlBulkCopy(conn);
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (CheckBoxList1.Items[i].Selected == true)
{
bulkcopy.ColumnMappings.Add(CheckBoxList1.Items[i].Text.ToString().Trim(), CheckBoxList1.Items[i].Text.ToString().Trim());
}
}
cmd.Connection.Close();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
bulkcopy.DestinationTableName = tableName;
bulkcopy.WriteToServer(dt);
DataTable Excel_UserInfo = new DataTable();
string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + fileInfo.FullName + ";" + "Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1;\"";
string strExcel = "select * from [sheet1$]";
using (OleDbDataAdapter adaptor = new OleDbDataAdapter(strExcel, strConn))
{
DataSet ds = new DataSet();
adaptor.Fill(ds);
Excel_UserInfo = ds.Tables[0];
}