<!-- 前端aspx代码-->
<form id="form2" runat="server">
<div class="modal-body">
<div class="input-group">
<span class="input-group-addon">选择小区:</span>
<select name="_examplesingle" οnchange="gaibian()" id="_examplesingle">
</select>
<asp:TextBox style="display:none" ID="TextBox1" runat="server"></asp:TextBox>
</div>
<div class="h10px"></div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon">选择文件:</span>
<asp:FileUpload ID="fuload" runat="server" />
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
<asp:Button ID="btnExport" runat="server" CssClass="btn btn-primary" OnClick="btnExport_Click" Text="导入" />
</div>
</form>
<!-- 后端端cs-->
public DataSet xsldata()
{
if (fuload.FileName == "")
{
Response.Write("<script>alert('请选择文件')</script>");
return null;
}
string fileExtenSion;
fileExtenSion = Path.GetExtension(fuload.FileName);
if (fileExtenSion.ToLower() != ".xls" && fileExtenSion.ToLower() != ".xlsx")
{
Response.Write("<script>alert('文件格式不正确')</script>");
return null;
}
try
{
string FileName = "/UploadImages/" + Path.GetFileName(fuload.FileName);
if (File.Exists(Server.MapPath(FileName)))
{
File.Delete(Server.MapPath(FileName));
}
fuload.SaveAs(Server.MapPath(FileName));
//HDR=Yes,这代表第一行是标题,不做为数据使用 ,如果用HDR=NO,则表示第一行不是标题,做为数据来使用。系统默认的是YES
string connstr2003 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(FileName) + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
string connstr2007 = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath(FileName) + ";Extended Properties=\"Excel 12.0;HDR=YES\"";
OleDbConnection conn;
if (fileExtenSion.ToLower() == ".xls")
{
conn = new OleDbConnection(connstr2003);
}
else
{
conn = new OleDbConnection(connstr2007);
}
string sql = "select * from [Sheet1$]";
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
OleDbDataAdapter myCommand = new OleDbDataAdapter(sql, conn);
DataSet ds = new DataSet();
myCommand.Fill(ds, "[Sheet1$]");
conn.Close();
//删除服务器里上传的文件
if (File.Exists(Server.MapPath(FileName)))
{
File.Delete(Server.MapPath(FileName));
}
return ds;
}
catch (Exception e)
{
return null;
}
}
protected void btnExport_Click(object sender, EventArgs e)
{
try
{
string XQBM = TextBox1.Text;
DataSet ds = xsldata();
//dataGridView2.DataSource = ds.Tables[0];
int errorcount = 0;//记录错误信息条数
int insertcount = 0;//记录插入成功条数
int updatecount = 0;//记录更新信息条数
string connStr1 = ConfigurationManager.AppSettings["datahost"].ToString();//获取webconfig 配置
string connStr2 = ConfigurationManager.AppSettings["database"].ToString();//获取webconfig 配置
string connStr3 = ConfigurationManager.AppSettings["uid"].ToString();//获取webconfig 配置
string connStr4 = ConfigurationManager.AppSettings["pwd"].ToString();//获取webconfig 配置
string strcon = "server="+ connStr1 + ";database=" + connStr2 + ";USER ID=" + connStr3 + ";PWD=" + connStr4 + "";
SqlConnection conn = new SqlConnection(strcon);//链接数据库
conn.Open();
int rowsnum = dt.Rows.Count;
for (int i = 0; i < rowsnum; i++)
{
string DYJ = dt.Rows[i]["栋"].ToString();
}
Response.Write("<script>alert('导入成功')</script>");
}
catch (Exception ex)
{
}
}
<!--webconfig 配置-->
<appSettings>
<!--公共通用配置 -->
<!--当前站点的weburl地址,地址中的最后不能省掉 "/" -->
<add key="datahost" value="116.62.134.102" />
<add key="database" value="LEQI" />
<add key="uid" value="sa" />
<add key="pwd" value="1234qwer!" />
<add key="AppKey" value="dcbf665e31681a6b3ec1a097" />
<add key="MasterSecret" value="c1134dc4718350530bffa2a8" />
</appSettings>
<!--注意 如果服务器没有Excel 要安装Excel 还要安装 AccessDatabaseEngine.exe-->
AccessDatabaseEngine.exe下载地址:https://pan.baidu.com/s/19F37aTHxBmTwYG4FWFDwhQ