求Excel表导入到sql表中的代码。
求Excel表导入到sql表中的代码。 要完整的。我是新手。想学。前台的也写出来 --------------------编程问答-------------------- 在线等待阿,求大虾阿,急阿 --------------------编程问答-------------------- 第一步:将EXECL表读到datatable中--------------------编程问答-------------------- 第二步:将datatable中的数据插入sql数据库
public DataTable xsldata()
{
try
{
string FileName = "~/Download/" + Path.GetFileName(FileUploadField.FileName);
if (File.Exists(Server.MapPath(FileName)))
{
File.Delete(Server.MapPath(FileName));
}
FileUploadField.PostedFile.SaveAs(Server.MapPath(FileName));
//HDR=Yes,这代表第一行是标题,不做为数据使用 ,如果用HDR=NO,则表示第一行不是标题,做为数据来使用。系统默认的是YES
string connstr2003 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(FileName) + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1;'";
string connstr2007 = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath(FileName) + ";Extended Properties=\"Excel 12.0;HDR=NO\"";
OleDbConnection conn;
if (fileExtenSion.ToLower() == ".xls")
{
conn = new OleDbConnection(connstr2003);
}
else
{
conn = new OleDbConnection(connstr2007);
}
conn.Open();
string sql = "select * from [Sheet1$]";
OleDbCommand cmd = new OleDbCommand(sql, conn);
DataTable dt = new DataTable();
OleDbDataReader sdr = cmd.ExecuteReader();
dt.Load(sdr);
sdr.Close();
conn.Close();
//删除服务器里上传的文件
if (File.Exists(Server.MapPath(FileName)))
{
File.Delete(Server.MapPath(FileName));
}
return dt;
}
catch (Exception e)
{
return null;
}
}
--------------------编程问答-------------------- 在第二步方法中先加入:
public void UpLoad_btnClick(object sender, DirectEventArgs e)
{
if (dt.Rows[i][0].ToString() != "" && dt.Rows[i][1].ToString() != "" && dt.Rows[i][2].ToString() != "" && dt.Rows[i][3].ToString() != "" && dt.Rows[i][4].ToString() != "" && dt.Rows[i][5].ToString() != "" && dt.Rows[i][6].ToString() != "" && dt.Rows[i][7].ToString() != "" && em.Success && t.Success)
{
string Name = dt.Rows[i][0].ToString();
bool Sex = dt.Rows[i][1].ToString() == "男" ? true : false;
int Age = Convert.ToInt32(dt.Rows[i][2].ToString());
string Degree = dt.Rows[i][3].ToString();
string Major = dt.Rows[i][4].ToString();
string Current = dt.Rows[i][5].ToString();
string Email = dt.Rows[i][6].ToString();
string Tel = dt.Rows[i][7].ToString();
DateTime datetime = DateTime.Now;
//给Person表赋值
Person ps = new Person();
ps.PartnerID = parterID;
ps.RegisterTime = datetime;
//给InfoToRecruitment表赋值
InfoToRecruitment info = new InfoToRecruitment();
info.Name = Name;
info.Age = Age;
info.Degree = atp.getDegree(Degree);
info.Gender = Sex;
info.Major = Major;
info.Career = Current;
info.Telephone = Tel;
info.Email = Email;
info.ResumeDeliverTime = datetime;
//给Batch_Person表赋值
Batch_Person bp = new Batch_Person();
bp.BatchID = batchID;
bp.ApplyTime = datetime;
int m = atp.InsertTable(ps, info, bp);
//m=1成功!否则失败
}
}
--------------------编程问答-------------------- 我用的是DateSet阿。 先把Excel数据存储在GridView里面,然后再添加。
if (!FileUploadField.HasFile)
{
X.Msg.Alert("提示", "请选择上传的文件!").Show();
return;
}
//获取客户端FileUploadField文件的扩展名并验证
fileExtenSion = Path.GetExtension(FileUploadField.FileName).ToLower();
if (fileExtenSion.ToLower() != ".xlsx" && fileExtenSion.ToLower() != ".xls")
{
X.Msg.Alert("提示", "上传的文件格式不正确!").Show();
return;
}
DataTable dt = xsldata();
if (dt.Rows[i][0].ToString() != "" && dt.Rows[。。。)
{
。。。
}
但是老提示我Sheet1找不到或者路径错误。是不是数据库里面需要做什么,在敲代码之前。没做过这个。 --------------------编程问答-------------------- fileUpload 控件 --------------------编程问答--------------------
名称不要固定用 Sheet1 --------------------编程问答--------------------
public DataSet GetDataSet()
{
string type2 = FileUpload1.FileName;
var type3 = type2.Substring(type2.LastIndexOf('.'));
if(type3.Equals(".xls") || type3.Equals(".xlsx"))
{
string newName = Server.MapPath("..//App_Data//Uploads//") + DateTime.Now.ToString("hhmmss") + ".xls";
FileUpload1.SaveAs(newName);
string connStr = string.Empty;
if (type3.Equals(".xlsx"))
{
connStr = "Provider=Microsoft.Ace.OleDb.12.0;Data Source=" + newName + ";Extended Properties='Excel 12.0 Xml; HDR=YES; IMEX=1'";
}
else if (type3.Equals(".xls"))
{
connStr = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + newName + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";
}
OleDbConnection conn = new OleDbConnection(connStr);
if (conn.State.ToString() == "Closed")
{
conn.Open();
}
DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
var tableName = dt.Rows[0][2].ToString().Trim();
var str1 = string.Format("Select * from [{0}]", tableName);
OleDbDataAdapter oda = new OleDbDataAdapter(str1, conn);
DataSet ds = new DataSet();
oda.Fill(ds);
conn.Close();
File.Delete( newName );
return ds;
}
else
{
//文件格式错误
}
}
补充:.NET技术 , ASP.NET