Asp.net中把Excel資料存放區至SQL Server中的具體實現方法

來源:互聯網
上載者:User

ExcelWrapper

複製代碼 代碼如下: /// <summary>
/// 查詢EXCEL試算表添加到DATASET
/// </summary>
/// <param name="filenameurl">檔案路徑</param>
/// <param name="table">dataset中的表名(並不是要和資料庫中的表一樣)</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("請您選擇Excel檔案", this);
return;
}

// 取得檔案尾碼名
string extension = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();
if (extension != ".xls" && extension != ".xlsx")
{
JsHelper.Alert("只可以選擇Excel檔案", this);
return;
}

// 構造Exel存在伺服器相對路徑的檔案名稱,並SaveAs 將上傳的檔案內容儲存在伺服器上
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表為空白表,無資料", this);
}
else
{
for (int i = 0; i < dr.Length; i++)
{
String error = "";

// excel列名不能變
string num = dr[i]["學號"].ToString();
string name = dr[i]["姓名"].ToString();
string pwd = dr[i]["密碼"].ToString();
string collegeNum = dr[i]["學院編號"].ToString();
string birth = dr[i]["生日"].ToString();

if (!BLL.M_CollegeBLL.GetAllCollegeNum().Contains(collegeNum))
{
error += "所屬學院不存 ";
}

if (String.IsNullOrEmpty(collegeNum))
{
error += "請選擇該學生所在院系 ";
}

if (String.IsNullOrEmpty(num))
{
error += "學號不可為空 ";
}
else if (!Utility.IsLetterThanSomeLength(num, 25))
{
error += "學號的長度過長 ";
}

if (String.IsNullOrEmpty(name))
{
error += "姓名不可為空 ";
}
else if (!Utility.IsLetterThanSomeLength(name, 25))
{
error += "姓名的長度過長 ";
}

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);

// 該學號不存在
if (!BLL.M_StudentBLL.GetAllStuNum().Contains(num))
{
BLL.M_StudentBLL.Add(stu);
}
else
{
BLL.M_StudentBLL.Modify(stu);
}
}
else
{
lstMsg.Add("學號為" + num + "未匯入成功," + "原因:" + error + "。");
}
}
}
this.lblHint.Text = "匯入完成。";
if (null != lstMsg)
{
this.lblHint.Text += "共有" + lstMsg.Count() + "條記錄未成功。<br /><br />";
foreach (string s in lstMsg)
{
this.lblHint.Text += s;
}
}
}
catch
{
this.lblHint.Text = "程式出錯,請您檢查需要匯入的表!";
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.