ASP.NET Excel導(dǎo)入SQL Server的調(diào)用操作
作者:佚名
ASP.NET Excel導(dǎo)入SQL Server的調(diào)用操作是如何進(jìn)行的呢?那么這篇ASP.NET Excel導(dǎo)入SQL Server的調(diào)用操作希望對(duì)你有所幫助。
ASP.NET Excel導(dǎo)入SQL Server的調(diào)用操作之.aspx.cs文件調(diào)用代碼:
- protected void btnExport_Click(object sender, EventArgs e)
- {
- string filepath = this.fileUpload.PostedFile.FileName;
- //該部分獲取的是文件名,沒有路徑,所以很有可能找不到該excel文件,所以如果有問題便可以這樣寫:
- //string fileName = fileUpload.PostedFile.FileName;
- // fileUpload.PostedFile.SaveAs(Server.MapPath("upfile") + "\\" + fileName);
- //string filepath = Server.MapPath("upfile") + "\\" + fileName;
- if (filepath != "")
- {
- if (this.txtTableName.Text != "")
- {
- if (filepath.Contains("xls"))
- {
- int result = ets.ImportSql(filepath, this.txtTableName.Text);
- if (result == (int)ExcelToSQL.ImportState.tableNameError)
- ets.Alert("此表名在數(shù)據(jù)中不存在!");
- else if(result==(int)ExcelToSQL.ImportState.excelFormatError)
- ets.Alert("Excel格式不能正確讀??!");
- else if (result == (int)ExcelToSQL.ImportState.fieldMatchError)
- ets.Alert("Excel里的字段和Sql Server里的字段不匹配!");
- else if(result==(int)ExcelToSQL.ImportState.dataTypeError)
- ets.Alert("轉(zhuǎn)換數(shù)據(jù)類型時(shí)發(fā)生錯(cuò)誤!");
- else if (result == (int)ExcelToSQL.ImportState.right)
- {
- ets.Alert("導(dǎo)入成功");
- }
- }
- else ets.Alert("上傳的文件類型必須為excel文件!");
- }
- else ets.Alert("表名不能為空!");
- }
- else ets.Alert("沒有選擇要上傳的文件!");
- }
ASP.NET Excel導(dǎo)入SQL Server的調(diào)用操作之.aspx前臺(tái)代碼
- ﹤form id="form1" runat="server"﹥
- ﹤div﹥
- ﹤asp:Label ID="Label1" runat="server" Text="路徑"﹥﹤/asp:Label﹥
- ﹤asp:FileUpload ID="fileUpload" runat="server" Width="443px" /﹥﹤br /﹥
- ﹤asp:Label ID="Label2" runat="server" Text="數(shù)據(jù)庫表名稱"﹥﹤/asp:Label﹥
- ﹤asp:TextBox ID="txtTableName" runat="server"﹥﹤/asp:TextBox﹥﹤br /﹥
- ﹤asp:Button ID="btnExport" runat="server" Text="導(dǎo)入到SQL" OnClick="btnExport_Click" /﹥
- ﹤/div﹥
- ﹤/form﹥
ASP.NET Excel導(dǎo)入SQL Server的調(diào)用操作就向你介紹到這里,希望對(duì)你了解ASP.NET Excel導(dǎo)入SQL Server的調(diào)用操作有所幫助。
【編輯推薦】
責(zé)任編輯:仲衡
來源:
IT168