C#匯入Excel表資料

來源:互聯網
上載者:User

protected void btnImport_Click(object sender, EventArgs e)

  {

  int j = 1;

  DateTime str_date;

  DataSet ds;

  string IsXls = System.IO.Path.GetExtension(FileUpload.FileName).ToString().ToLower();//System.IO.Path.GetExtension獲得檔案的副檔名

  if (FileUpload.HasFile == false)//HasFile用來檢查FileUpload是否有指定檔案

  {

  uploadErrorTxt = "請您選擇Excel檔案!<br/>";

  }

  else if (IsXls != ".xls" && IsXls != ".xlsx")

  {

  uploadErrorTxt += "上傳的檔案必須為.xls尾碼!<br/>";

  }

  else

  {

  string filename = DateTime.Now.ToString("yyyyMMddhhMMss") + FileUpload.FileName;

  string savePath = Server.MapPath(("~//upfiles//") + filename);

  FileUpload.SaveAs(savePath);

  try

  {

  ds = ExecleDs(savePath, filename);

  }

  catch

  {

  uploadErrorTxt = "匯入失敗,Excel工作表標籤名錯誤,標籤名必須是Sheet1,請查證後再匯入!";

  gotoError();

  return;

  }

  DataRow[] dr = ds.Tables[0].Select();

  int rowsnum = ds.Tables[0].Rows.Count;

  if (rowsnum == 0)

  {

  uploadErrorTxt += "Excel表為空白表,無資料!<br/>";

  }

  else

  {

  for (int i = 0; i < dr.Length; i++)

  {

  j = j + 1;

  string str_id = dr[i]["ID"].ToString();

  string str_name = dr[i]["名稱"].ToString();

  try

  {

  str_date = DateTime.Parse(dr[i]["時間"].ToString());

  }

  catch

  {

  uploadErrorTxt = "第" + j + "行時間格式有誤,停止匯入!";

  gotoError();

  return;

  }

  bool b = bll.Exists(str_id); //id 不存在

  if (b == false)

  {

  try

  {

  model.id = int.Parse(str_id;);

  }

  catch

  {

  uploadErrorTxt = "第" + j + "行id格式錯誤,停止匯入!";

  gotoError();

  return;

  }

  model.date = str_date;

  model.name=str_name;

  try

  {

  bll.Add(model);

  uploadSucceedTxt = "Excle表匯入成功!";

  }

catch (MembershipCreateUserException ex)

  {

  uploadErrorTxt = "匯入內容:" + ex.Message;

  }

  }

  else

  {

  uploadErrorTxt += "id:" + str_id + "id重複禁止匯入!<br/>";

  }

  }

  }

  }

  if (uploadErrorTxt != "")

  {

  string txtPrint = "";

  txtPrint += "<table width='100%' border='0' align='center' cellpadding='0' cellspacing='1' bgcolor='#BBC7CE' class='tab01'>";

  txtPrint += "<tr>";

  txtPrint += "<td bgcolor='#ffffff'>";

  txtPrint += uploadErrorTxt;

  txtPrint += "</td>";

  txtPrint += "</tr>";

  txtPrint += "</table>";

  //輸出錯誤提示

  this.labErrorTxt.Text = txtPrint;

  }

  else

  {

  string txtPrint = "";

  txtPrint += "<table width='100%' border='0' align='center' cellpadding='0' cellspacing='1' bgcolor='#BBC7CE' class='tab01'>";

  txtPrint += "<tr>";

  txtPrint += "<td bgcolor='#ffffff'>";

  txtPrint += uploadSucceedTxt;

  txtPrint += "</td>";

  txtPrint += "</tr>";

  txtPrint += "</table>";

  //輸出成功提示

  this.labErrorTxt.Text = txtPrint;

  InitDateBind();

  }

  }

  void gotoError()

  {

  if (uploadErrorTxt != "")

  {

  string txtPrint = "";

  txtPrint += "<table width='100%' border='0' align='center' cellpadding='0' cellspacing='1' bgcolor='#BBC7CE' class='tab01'>";

  txtPrint += "<tr>";

  txtPrint += "<td bgcolor='#ffffff'>";

  txtPrint += uploadErrorTxt;

  txtPrint += "</td>";

  txtPrint += "</tr>";

  txtPrint += "</table>";

  //輸出錯誤提示

  this.labErrorTxt.Text = txtPrint;

  InitDateBind();

  }

  }

  public DataSet ExecleDs(string filenameurl, string table)

  {

  DataSet ds = new DataSet();

  string strConn = "Provider=Microsoft.Ace.OleDb.12.0;Data Source=" + filenameurl + ";Extended Properties='Excel 12.0; HDR=YES; IMEX=1'";

  OleDbConnection conn = new OleDbConnection(strConn);

  conn.Open();

  OleDbDataAdapter odda = new OleDbDataAdapter("select * from [Sheet1$]", conn);

  odda.Fill(ds, table);

  return ds;

  }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.