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
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
25
protected void Page_Load(object sender, EventArgs e)
26
{
27
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
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
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
65
string 商户编号;
66
string 装机日期;
67
string 手续费率;
68
string 商户名;
69
string 主办行;
70
string 经办人;
71
string 装机方;
72
string 开户行;
73
string 有效性;
74
string 装机台数;
75
string POS类型;
76
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
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
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110