asp.net读取excel中的数据并绑定在gridview
程序员文章站
2024-02-27 12:02:09
前台label,dropdownlist,gridview控件 aspx.cs核心代码: 复制代码 代码如下: using system.data.oledb;//需要引入...
前台label,dropdownlist,gridview控件
aspx.cs核心代码:
using system.data.oledb;//需要引入命名
public void excel_click(object sender, eventargs e)
{
if (this.attachmentfile.value == "" && this.label1.text == "" && dropdownlist2.selectedvalue == "")
{
response.write("<script>window.alert('请选择要导入的文件')</script>");
}
if (this.attachmentfile.value != "" && this.dropdownlist2.selectedvalue == "")
{
httpfilecollection files = httpcontext.current.request.files;
httppostedfile postedfile = files[0];
filename = system.io.path.getfilename(postedfile.filename);
if (filename != "")
{
postedfile.saveas("\\\\localhost\\文件夹\\" + filename);
}
string strconn;
strconn = "provider=microsoft.jet.oledb.4.0;data source=" + "\\\\localhost\\文件夹\\" + filename + ";extended properties=excel 8.0;";//this.attachmentfile.value.tostring()
oledbconnection conn = new oledbconnection(strconn);
conn.open();
datatable sheetnames = conn.getoledbschematable(system.data.oledb.oledbschemaguid.tables, new object[] { null, null, null, "table" });
foreach (datarow dr in sheetnames.rows)
{
dropdownlist2.items.add(dr[2].tostring());
}
this.label1.text = "\\\\localhost\\文件夹\\" + filename;//this.attachmentfile.value.tostring();
conn.close();
}
if (this.label1.text.tostring() != "" && this.dropdownlist2.selectedvalue != "")// && this.dropdownlist1.selectedvalue.tostring() != "全部"
{
//绑定到gridview
gridview1.datasource = createdatasource(dropdownlist2.selectedvalue.tostring(), this.label1.text.tostring());//, this.dropdownlist1.selectedvalue.tostring()
gridview1.databind();
}
}
//以excel为数据源获取数据集
private dataset createdatasource(string select, string lable)
{
string strcon = "provider=microsoft.jet.oledb.4.0;data source=" + lable + ";extended properties=excel 8.0;";
string strsql = "select 登记号码,姓名,日期,签到时间,签退时间,部门 from [" + select + "] order by 部门,日期,姓名";//excel表格的字段
oledbconnection conn = new oledbconnection(strcon);
oledbdataadapter da = new oledbdataadapter(strsql, conn);
try
{
conn.open();
dataset ds = new dataset();
da.fill(ds);
conn.close();
return ds;
}
catch (exception e)
{
response.write("<script>window.alert('没有数据,或者" + e.message + "')</script>");
return null;
}
}
以上是插入07以前版本excel
如果07版本以后只需要做小小修改
string strcon = "provider=microsoft.ace.oledb.12.0;data source=" + lable + ";extended properties=excel 12.0;";
aspx.cs核心代码:
复制代码 代码如下:
using system.data.oledb;//需要引入命名
public void excel_click(object sender, eventargs e)
{
if (this.attachmentfile.value == "" && this.label1.text == "" && dropdownlist2.selectedvalue == "")
{
response.write("<script>window.alert('请选择要导入的文件')</script>");
}
if (this.attachmentfile.value != "" && this.dropdownlist2.selectedvalue == "")
{
httpfilecollection files = httpcontext.current.request.files;
httppostedfile postedfile = files[0];
filename = system.io.path.getfilename(postedfile.filename);
if (filename != "")
{
postedfile.saveas("\\\\localhost\\文件夹\\" + filename);
}
string strconn;
strconn = "provider=microsoft.jet.oledb.4.0;data source=" + "\\\\localhost\\文件夹\\" + filename + ";extended properties=excel 8.0;";//this.attachmentfile.value.tostring()
oledbconnection conn = new oledbconnection(strconn);
conn.open();
datatable sheetnames = conn.getoledbschematable(system.data.oledb.oledbschemaguid.tables, new object[] { null, null, null, "table" });
foreach (datarow dr in sheetnames.rows)
{
dropdownlist2.items.add(dr[2].tostring());
}
this.label1.text = "\\\\localhost\\文件夹\\" + filename;//this.attachmentfile.value.tostring();
conn.close();
}
if (this.label1.text.tostring() != "" && this.dropdownlist2.selectedvalue != "")// && this.dropdownlist1.selectedvalue.tostring() != "全部"
{
//绑定到gridview
gridview1.datasource = createdatasource(dropdownlist2.selectedvalue.tostring(), this.label1.text.tostring());//, this.dropdownlist1.selectedvalue.tostring()
gridview1.databind();
}
}
//以excel为数据源获取数据集
private dataset createdatasource(string select, string lable)
{
string strcon = "provider=microsoft.jet.oledb.4.0;data source=" + lable + ";extended properties=excel 8.0;";
string strsql = "select 登记号码,姓名,日期,签到时间,签退时间,部门 from [" + select + "] order by 部门,日期,姓名";//excel表格的字段
oledbconnection conn = new oledbconnection(strcon);
oledbdataadapter da = new oledbdataadapter(strsql, conn);
try
{
conn.open();
dataset ds = new dataset();
da.fill(ds);
conn.close();
return ds;
}
catch (exception e)
{
response.write("<script>window.alert('没有数据,或者" + e.message + "')</script>");
return null;
}
}
以上是插入07以前版本excel
如果07版本以后只需要做小小修改
复制代码 代码如下:
string strcon = "provider=microsoft.ace.oledb.12.0;data source=" + lable + ";extended properties=excel 12.0;";
上一篇: 浅谈导入JavaWeb 项目出现的问题
下一篇: Python 文件处理注意事项总结