上传Excel进数据库,实例。
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Web;
5
using System.Web.Security;
6
using System.Web.UI;
7
using System.Web.UI.WebControls;
8
using System.Web.UI.WebControls.WebParts;
9
using System.Web.UI.HtmlControls;
10
using System.Data.OleDb;
11
using System.Data.SqlClient;
12![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
public partial class InserPosAll : System.Web.UI.Page
14
{
15
public DataSet ExecleDs(string filenameurl, string table)
16
{
17
string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + filenameurl + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";
18
OleDbConnection conn = new OleDbConnection(strConn);
19
OleDbDataAdapter odda = new OleDbDataAdapter("select * from [sheet1$]", conn);
20
DataSet ds = new DataSet();
21
odda.Fill(ds, table);
22
return ds;
23
}
24![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
protected void Page_Load(object sender, EventArgs e)
26
{
27![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
}
29
protected void Button1_Click(object sender, EventArgs e)
30
{
31
if (FileUpload1.HasFile == false)
32
{
33
Response.Write("<script>alert('请您选择Excel文件')</script> ");
34
return;//当无文件时,返回
35
}
36
//string IsXls = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();
37
//if (IsXls != ".xls")
38
//{
39
// Response.Write("<script>alert('只可以选择Excel文件')</script>");
40
// return;//当选择的不是Excel文件时,返回
41
//}
42
String serverPath = Server.MapPath("~/") + FileUpload1.FileName;
43
FileUpload1.SaveAs(serverPath);
44
string error = null;
45![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
SqlConnection conn = new SqlConnection("data source=10.47.0.7;database=pos_inquire;user=sa;password=19730524");
47
conn.Open();
48
string Sqlstrl = " Truncate table pos_all";
49
SqlCommand comd = new SqlCommand(Sqlstrl, conn);
50
comd.ExecuteNonQuery();
51![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
//string strpath = FileUpload1.PostedFile.FileName.ToString(); //获取Execle文件路径
53
string filename = FileUpload1.FileName; //获取Execle文件名
54
//DataSet ds = conn.ExecleDs(strpath,filename);
55
DataSet ds = ExecleDs(serverPath, filename);
56
DataRow[] dr = ds.Tables[0].Select(); //定义一个DataRow数组
57
int rowsnum = ds.Tables[0].Rows.Count;
58
if (rowsnum == 0)
59
{
60
Response.Write("<script>alert('Excel表为空表,无数据!')</script>"); //当Excel表为空时,对用户进行提示
61
}
62
else
63
{
64![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
65
string 商户编号;
66
string 装机日期;
67
string 手续费率;
68
string 商户名;
69
string 主办行;
70
string 经办人;
71
string 装机方;
72
string 开户行;
73
string 有效性;
74
string 装机台数;
75
string POS类型;
76![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
77
for (int i = 0; i < dr.Length; i++)
78
{
79
商户编号 = dr[i][0].ToString(); //string dh = dr[i]["YongHuMiMa"].ToString();
80
装机日期 = dr[i][1].ToString();
81
手续费率 = dr[i][2].ToString();
82
商户名 = dr[i][3].ToString();
83
主办行 = dr[i][4].ToString();
84
经办人 = dr[i][5].ToString();
85
装机方 = dr[i][6].ToString();
86
开户行 = dr[i][7].ToString();
87
有效性 = dr[i][8].ToString();
88
装机台数 = dr[i][9].ToString();
89
POS类型 = dr[i][10].ToString();
90![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
91
string insertstr = "insert into pos_all(商户编号,装机日期,手续费率,商户名,主办行,经办人,装机方,开户行,有效性,装机台数,POS类型) values('" + 商户编号 + "','" + 装机日期 + "','" + 手续费率 + "','" + 商户名 + "','" + 主办行 + "','" + 经办人 + "','" + 装机方 + "','" + 开户行 + "','" + 有效性 + "','" + 装机台数 + "','" + POS类型 + "')";
92
SqlCommand cmd = new SqlCommand(insertstr, conn);
93
try
94
{
95
cmd.ExecuteNonQuery();
96
}
97
catch (MembershipCreateUserException ex) //捕捉异常
98
{
99
Response.Write("<script>alert('创建用户:" + ex.Message + "')</script>");
100
}
101
}
102
Response.Write("<script>alert('Excle表导入成功!')</script>");
103
System.IO.File.Delete(serverPath);
104
Response.Redirect("List_Posall.aspx");
105
106
}
107
conn.Close();
108
}
109
110
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
59
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
60
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
62
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
63
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
64
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
65
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
66
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
67
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
68
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
69
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
70
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
72
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
73
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
74
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
75
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
76
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
77
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
78
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
79
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
80
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
81
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
82
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
83
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
84
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
85
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
86
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
87
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
88
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
89
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
90
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
91
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
92
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
93
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
94
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
95
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
96
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
97
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
98
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
99
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
100
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
101
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
102
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
103
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
104
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
105
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
106
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
107
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
108
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
109
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
110
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)