<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
protected void 预览Butto1_Click(object sender, EventArgs e)
{
string path = FileUpload1.PostedFile.FileName.ToString();
string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0; Extended Properties=Excel 8.0;data source=" + path;
string sql = "SELECT * FROM [Sheet1$]";// 查询语句
OleDbConnection Conn = new OleDbConnection(ConnStr);
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter(sql, ConnStr);
da.Fill(ds); //填充DataSet
//输出,绑定数据
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
Conn.Close();
}
protected void 写入Button_Click(object sender, EventArgs e)
{
string path = FileUpload1.PostedFile.FileName.ToString();
string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0; Extended Properties=Excel 8.0;data source=" + path;
string sql = "SELECT * FROM [Sheet1$]";// 查询语句
OleDbConnection Conn = new OleDbConnection(ConnStr);
Conn.Open();
OleDbCommand Comm = new OleDbCommand(sql, Conn);
OleDbDataReader sdr = Comm.ExecuteReader(CommandBehavior.CloseConnection);
string str = "";
while (sdr.Read())
{
if (str.IndexOf(sdr["部门"].ToString()) < 0)//获取部门
{
str += sdr["部门"].ToString() + "<br/>";
}
//if (str.IndexOf(sdr["部门"].ToString() + "--" + sdr["岗位"].ToString()) < 0)//获取岗位
//{
// str += sdr["部门"].ToString() + "--" + sdr["岗位"].ToString() + "<br/>";
//}
//Response.Write(string.Format("{0}--{1}--{2}--{3}--{4}--{5}<br />", sdr["工号"].ToString(), sdr["姓名"].ToString(), sdr["性别"].ToString(), sdr["部门"].ToString(), sdr["岗位"].ToString(), sdr["密码"].ToString()));
}
sdr.Close();
Response.Write(str);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body style="font-size:12px;">
<form id="form1" runat="server">
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="预览Butto1" runat="server" Text="预览数据" onclick="预览Butto1_Click" />
<asp:Button ID="写入Button" runat="server" Text="写入数据库"
onclick="写入Button_Click" />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</form>
</body>
</html>