|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
以前学了大概半年时间的asp(没有机会做大系统,最多是自己对公司系统做些调整和修改还有一些小程序)。应该说开始接触asp.net是今年元月5号的事。现在很想把公司的系统重新用.net来架构,却不知道如何下手。将excel数据导进到GridView与将GridView数据导出到excel,这个功效应当是OA或ERP体系中常常会用到的功效吧,固然详细到项目中,利用的控件纷歧定是GridView,不外我想,不管甚么样的富控件,应当都撑持以DataTable为数据源吧,如许,我们办理列表控件与Excel数据互换,就有良多配合的地方。
1、从Execl中导进数据到GridView.
这个对照oledb来完成相称的复杂:
privateDataSetCreateDataSource()
{
stringstrCon;
strCon="Provider=Microsoft.Jet.OLEDB.4.0;DataSource="+Server.MapPath("excel.xls")+";ExtendedProperties=Excel8.0;";
OleDbConnectionolecon=newOleDbConnection(strCon);
OleDbDataAdaptermyda=newOleDbDataAdapter("SELECT*FROM[Sheet1$]",strCon);
DataSetmyds=newDataSet();
myda.Fill(myds);
returnmyds;
}
protectedvoidButton1_Click(objectsender,EventArgse)
{
GridView1.DataSource=CreateDataSource();
GridView1.DataBind();
}
利用Oledb毗连对Excel的全体操纵,请参考本站以下文章:
利用OleDb,让Excel操纵与数据库一样复杂
2、然后是将GridView数据导出到Excel中。
在页面增添一个按钮,在单击事务中增加以下办法:
protectedvoidButton1_Click(objectsender,EventArgse)
{
Export("application/ms-excel","excel地点");
}
privatevoidExport(stringFileType,stringFileName)
{
Response.Charset="GB2312";
Response.ContentEncoding=System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode(FileName,Encoding.UTF8).ToString());
Response.ContentType=FileType;
this.EnableViewState=false;
StringWritertw=newStringWriter();
HtmlTextWriterhw=newHtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
//假如没有上面办法会报错范例“GridView”的控件“GridView1”必需放在具有runat=server的窗体标志内
publicoverridevoidVerifyRenderingInServerForm(Controlcontrol)
{
}
另有由于对Excel操纵,属于文件操纵,以是要引进称号空间IO和Text
全体背景代码以下:
usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
usingSystem.Data.SqlClient;
usingSystem.Drawing;
usingSystem.IO;
usingSystem.Text;
publicpartialclassDefault1:System.Web.UI.Page
{
SqlConnectionsqlcon;
SqlCommandsqlcom;
stringstrCon="DataSource=(local);Database=数据库;Uid=sa;Pwd=sa";
protectedvoidPage_Load(objectsender,EventArgse)
{
if(!IsPostBack)
{
bind();
}
}
publicvoidbind()
{
stringsqlstr="select*from数据库";
sqlcon=newSqlConnection(strCon);
SqlDataAdaptermyda=newSqlDataAdapter(sqlstr,sqlcon);
DataSetmyds=newDataSet();
sqlcon.Open();
myda.Fill(myds,"数据库");
GridView1.DataSource=myds;
GridView1.DataKeyNames=newstring[]{"姓名"};
GridView1.DataBind();
sqlcon.Close();
}
protectedvoidButton1_Click(objectsender,EventArgse)
{
Export("application/ms-excel","要导出的excel地点");
}
privatevoidExport(stringFileType,stringFileName)
{
Response.Charset="GB2312";
Response.ContentEncoding=System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode(FileName,Encoding.UTF8).ToString());
Response.ContentType=FileType;
this.EnableViewState=false;
StringWritertw=newStringWriter();
HtmlTextWriterhw=newHtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
publicoverridevoidVerifyRenderingInServerForm(Controlcontrol)
{
}
}
前台次要代码以下:
<asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False">
<Columns>
<asp:BoundFieldDataField="姓名"HeaderText="姓名"/>
<asp:BoundFieldDataField="家庭住址"HeaderText="家庭住址"/>
<asp:BoundFieldDataField="邮政编码"HeaderText="邮政编码"/>
<asp:BoundFieldDataField="出身日期"HeaderText="邮政编码"/>
</Columns>
</asp:GridView>
<asp:ButtonID="Button1"runat="server"Text="导出"/>
以上代码来自互联网,并没有经由自己测试,不包管源码的准确性,但我想从以上的源码中,足以看出此类功效的解题思绪,假如供应的源码有忽略,人人略微修改便可。我之所以想学。NET,是因为一直觉的BILLGATES好厉害,希望有一天能去微软,虽然现在还距离遥远,呵呵:) |
|