.net-Excel導入-OleDB方式(備註:表頭在第二行/不在第一行 的解決方法) ...
優點:將Excel直接當做數據源處理,通過SQL直接讀取內容,讀取速度較快。
缺點:讀取數據方式不夠靈活,無法直接讀取某一個單元格,只有將整個Sheet頁讀取出來後(結果為Datatable)再在Datatable中根據行列數來獲取指定的值。
當Excel數據量很大時。會非常占用記憶體,當記憶體不夠時會拋出記憶體溢出的異常。
aspx代碼:
<asp:FileUpload ID="FileUpload2" runat="server" Width="265px" Style=" float:left; height:25px;" />
<asp:Button ID="Button2" runat="server" Text="第2階段:建議答覆及評比推薦表-導入" CssClass="pl"
Style="margin: 10px 10px; width:250px; float:left " onclick="Button2_Click" />
cs代碼:
protected void Button2_Click(object sender, EventArgs e)
{
if (!FileUpload2.PostedFile.FileName.Trim().ToLower().EndsWith(".xls"))
{
this.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('只能上傳Excel文件(.xls).');", true);
return;
}
string strFullName = this.FileUpload2.PostedFile.FileName.ToString();
//先上傳文件到伺服器固定路徑
string FileName = FileUpload2.PostedFile.FileName;
string FilePathName = "";
string strFileName = "";
if (FileName != "")
{
string dateStr = DateTime.Now.ToString("yyyyMMddhhmmssms");
string filename = Path.GetFileNameWithoutExtension(FileName);
string oldfilekz = Path.GetExtension(FileName);
FilePathName = Server.MapPath("~") + "File\\" + filename + dateStr + oldfilekz;
FileUpload2.PostedFile.SaveAs(FilePathName);
strFileName = Server.MapPath("~") + "File\\" + filename + dateStr + oldfilekz;
}
//以Excel做為資料庫導入數據
string strCon = "Provider=Microsoft.Jet.OleDb.4.0;"
+ "data source=" + strFileName + ";"
+ "Extended Properties='Excel 8.0;HDR=NO; IMEX=1'";
ArrayList list = new ArrayList();
int updatecount = 0;
OleDbConnection con = new OleDbConnection(strCon);
OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet0$]", con);
DataSet ds = new DataSet();
da.Fill(ds, "excel");
DataTable dt = new DataTable();
dt = GetExcelTable(ds.Tables["excel"]);
//string aa = ds.Tables["excel"].Rows[3][1].ToString();
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
updatecount += 1;
StringBuilder Sql = new StringBuilder();
string TOPICID = dt.Rows[i]["序列號"].ToString();
string STATE = (dt.Rows[i]["是否採納"].ToString() == "是") ? "5" : "5-1";
Sql.Append(" update bbs_topic set ZN_DEPT='" + ds.Tables["excel"].Rows[i]["答覆部門"].ToString() + "',DFR='" + dt.Rows[i]["答覆人"].ToString() + "',ZXJY='" + dt.Rows[i]["是否屬於專項建議"].ToString() + "',STATE='" + STATE + "',DFYJ='" + dt.Rows[i]["答覆意見"].ToString() + "',TA_YX='" + dt.Rows[i]["是否推薦為優秀建議"].ToString() + "',TJJX='" + dt.Rows[i]["推薦獎項"].ToString() + "' where TOPICID='" + TOPICID + "'");
list.Add(Sql);
}
if (list.Count != 0)
{
if (DbCommon.SqlTranList(list)) {
this.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('信息導入成功!');", true);
}
}
}
else
{
this.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('導入的文件沒有記錄!');", true);
return;
}
}
/// <summary>
/// 去除不必要的標題行
/// </summary>
/// <param name="table"></param>
/// <returns></returns>
public DataTable GetExcelTable(DataTable table)
{
foreach (DataRow row in table.Rows)
{
if (!(row[0].ToString()).Equals("序列號"))
{
row.Delete();
}
else
{
break;
}
}
table.AcceptChanges();
for (int i = 0; i < table.Columns.Count; i++)
{
table.Columns[i].ColumnName = table.Rows[0][i].ToString();
}
table.Rows[0].Delete();
table.AcceptChanges();
//table = table.GetChanges(DataRowState.Unchanged);
return table;
}