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