Asp.net中把Excel數(shù)據(jù)存儲(chǔ)至SQL Server中的具體實(shí)現(xiàn)方法
ExcelWrapper
/// <summary>
/// 查詢EXCEL電子表格添加到DATASET
/// </summary>
/// <param name="filenameurl">文件路徑</param>
/// <param name="table">dataset中的表名(并不是要和數(shù)據(jù)庫(kù)中的表一樣)</param>
/// <returns></returns>
public static DataSet ExecleDs(string filenameurl, string table)
{
string strConn = "Provider=Microsoft.Jet.OleDb.4.0;"
+ "data source=" + filenameurl + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
DataSet ds = new DataSet();
OleDbDataAdapter odda = new OleDbDataAdapter("select * from [Sheet1$]", conn);
odda.Fill(ds, table);
return ds;
}
.cs
// 提交按鈕
protected void imgbtnSubmit_Click(object sender, ImageClickEventArgs e)
{
try
{
if (!FileUpload1.HasFile)
{
JsHelper.Alert("請(qǐng)您選擇Excel文件", this);
return;
}
// 取得文件后綴名
string extension = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();
if (extension != ".xls" && extension != ".xlsx")
{
JsHelper.Alert("只可以選擇Excel文件", this);
return;
}
// 構(gòu)造Exel存在服務(wù)器相對(duì)路徑的文件名,并SaveAs 將上傳的文件內(nèi)容保存在服務(wù)器上
string filename = DateTime.Now.ToString("yyyymmddhhMMss") + FileUpload1.FileName;
string savePath = Server.MapPath(("~\\upfiles\\") + filename);
FileUpload1.SaveAs(savePath);
DataSet ds = ExcelWrapper.ExecleDs(savePath, filename);
DataRow[] dr = ds.Tables[0].Select();
int rowsnum = ds.Tables[0].Rows.Count;
List<String> lstMsg = new List<string>();
if (rowsnum == 0)
{
JsHelper.Alert("Excel表為空表,無(wú)數(shù)據(jù)", this);
}
else
{
for (int i = 0; i < dr.Length; i++)
{
String error = "";
// excel列名不能變
string num = dr[i]["學(xué)號(hào)"].ToString();
string name = dr[i]["姓名"].ToString();
string pwd = dr[i]["密碼"].ToString();
string collegeNum = dr[i]["學(xué)院編號(hào)"].ToString();
string birth = dr[i]["生日"].ToString();
if (!BLL.M_CollegeBLL.GetAllCollegeNum().Contains(collegeNum))
{
error += "所屬學(xué)院不存 ";
}
if (String.IsNullOrEmpty(collegeNum))
{
error += "請(qǐng)選擇該學(xué)生所在院系 ";
}
if (String.IsNullOrEmpty(num))
{
error += "學(xué)號(hào)不能為空 ";
}
else if (!Utility.IsLetterThanSomeLength(num, 25))
{
error += "學(xué)號(hào)的長(zhǎng)度過(guò)長(zhǎng) ";
}
if (String.IsNullOrEmpty(name))
{
error += "姓名不能為空 ";
}
else if (!Utility.IsLetterThanSomeLength(name, 25))
{
error += "姓名的長(zhǎng)度過(guò)長(zhǎng) ";
}
if (String.IsNullOrEmpty(birth))
{
error += "出生日期不能為空 ";
}
else if (!Utility.IsDateTime(birth))
{
error += "出生日期格式不正確 ";
}
if (String.IsNullOrEmpty(sex))
{
error += "性別不能為空 ";
}
if (String.IsNullOrEmpty(error))
{
M_Student stu = new M_Student();
stu.Num = num;
stu.Name = name;
stu.Pwd = pwd;
stu.CollegeNum = collegeNum;
stu.Birthday = Convert.ToDateTime(birth);
// 該學(xué)號(hào)不存在
if (!BLL.M_StudentBLL.GetAllStuNum().Contains(num))
{
BLL.M_StudentBLL.Add(stu);
}
else
{
BLL.M_StudentBLL.Modify(stu);
}
}
else
{
lstMsg.Add("學(xué)號(hào)為" + num + "未導(dǎo)入成功," + "原因:" + error + "。");
}
}
}
this.lblHint.Text = "導(dǎo)入完成。";
if (null != lstMsg)
{
this.lblHint.Text += "共有" + lstMsg.Count() + "條記錄未成功。<br /><br />";
foreach (string s in lstMsg)
{
this.lblHint.Text += s;
}
}
}
catch
{
this.lblHint.Text = "程序出錯(cuò),請(qǐng)您檢查需要導(dǎo)入的表!";
}
}
效果圖
相關(guān)文章
淺談如何在ASP.NET Core中實(shí)現(xiàn)一個(gè)基礎(chǔ)的身份認(rèn)證
這篇文章主要介紹了淺談如何在ASP.NET Core中實(shí)現(xiàn)一個(gè)基礎(chǔ)的身份認(rèn)證,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-12-12ASP.NET數(shù)據(jù)庫(kù)存取圖片的方法
這篇文章主要為大家詳細(xì)介紹了ASP.NET數(shù)據(jù)庫(kù)如何存取圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01.Net?Core使用Logger實(shí)現(xiàn)log寫(xiě)入本地文件系統(tǒng)
這篇文章介紹了.Net?Core使用Logger實(shí)現(xiàn)log寫(xiě)入本地文件系統(tǒng)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06不可或缺的ASP.NET內(nèi)置對(duì)象小結(jié)
這篇文章主要介紹了不可或缺的ASP.NET內(nèi)置對(duì)象小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04asp.net 從客戶端中檢測(cè)到有潛在危險(xiǎn)的 Request.Form 值錯(cuò)誤解
asp.net程序運(yùn)行時(shí)出現(xiàn)以下錯(cuò)誤: “/news”應(yīng)用程序中的服務(wù)器錯(cuò)誤。2009-05-05在Asp.Net Core中使用ModelConvention實(shí)現(xiàn)全局過(guò)濾器隔離
這篇文章主要介紹了在Asp.Net Core中使用ModelConvention實(shí)現(xiàn)全局過(guò)濾器隔離,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01