GridviewData Import Excel/Excel data read into the gridview
:
Solution:
Add a button to the page and click Add event as follows:
Protected void button#click (Object sender, eventargs E)
{
Export ("application/MS-excel", "Student renewal report .xls ");
}
Private void Export (string filetype, string filename)
{
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;
Stringwriter Tw = new stringwriter ();
Htmltextwriter hW = new htmltextwriter (TW );
Gridview1.rendercontrol (HW );
Response. Write (TW. tostring ());
Response. End ();
}
// If no method is available, an error is returned. The control gridview1 of the type "gridview" must be placed in the form tag with runat = Server
Public override void verifyrenderinginserverform (Control)
{
}
The introduction of namespace Io and text is also required for file operations.
Code :
using system;
using system. data;
using system. configuration;
using system. web;
using system. web. security;
using system. web. ui;
using system. web. UI. webcontrols;
using system. web. UI. webcontrols. webparts;
using system. web. UI. htmlcontrols;
using system. data. sqlclient;
using system. drawing;
using system. io;
using system. text;
Public partial class default7: system. web. UI. page
{< br> sqlconnection sqlcon;
sqlcommand sqlcom;
string strcon = "Data Source = (local); database = beifeng trade; uid = sa; pwd = sa ";
protected void page_load (Object sender, eventargs e)
{< br> If (! Ispostback)
{< br> BIND ();
}< BR >}< br>
Public void BIND ()
{< br> string sqlstr = "select top 5 * From feihu Studio ";
sqlcon = new sqlconnection (strcon);
sqldataadapter myda = new sqldataadapter (sqlstr, sqlcon);
dataset myds = new dataset ();
sqlcon. open ();
myda. fill (myds, "");
gridview1.datasource = myds;
gridview1.datakeynames = new string [] {"ID card number"};
gridview1.databind ();
sqlcon. close ();
}
Protected void button#click (Object sender, eventargs E)
{
Export ("application/MS-excel", "Student renewal report .xls ");
}
Private void Export (string filetype, string filename)
{
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;
Stringwriter Tw = new stringwriter ();
Htmltextwriter hW = new htmltextwriter (TW );
Gridview1.rendercontrol (HW );
Response. Write (TW. tostring ());
Response. End ();
}
Public override void verifyrenderinginserverform (Control)
{
}
}
Front-end :
<Asp: gridview id = "gridview1" runat = "server" autogeneratecolumns = "false" cellpadding = "3"
Backcolor = "white" bordercolor = "# cccccc" borderstyle = "NONE" borderwidth = "1px" font-size = "12px">
<Footerstyle backcolor = "white" forecolor = "#000066"/>
<Columns>
<Asp: boundfield datafield = "ID card number" headertext = "no." readonly = "true"/>
<Asp: boundfield datafield = "name" headertext = "name"/>
<Asp: boundfield datafield = "Date of Birth" headertext = "zip code"/>
<Asp: boundfield datafield = "Home Address" headertext = "Home Address"/>
<Asp: boundfield datafield = "zip code" headertext = "zip code"/>
<Asp: boundfield datafield = "starting salary" headertext = "starting salary"/>
</Columns>
<Rowstyle forecolor = "#000066" type = "regxph" text = "yourobjectname"/>
<Selectedrowstyle backcolor = "#669999" font-bold = "true" forecolor = "white"/>
<Pagerstyle backcolor = "white" forecolor = "#000066" horizontalalign = "Left" cssclass = "MS-formlabel datagridfixedheader"/>
<Headerstyle backcolor = "#006699" font-bold = "true" forecolor = "white"/>
</ASP: gridview>
<Asp: button id = "button1" runat = "server" onclick = "button#click" text = "Export"/>
Code for reading Excel Data : This is very simple.
Private dataset createdatasource ()
{
String strcon;
Strcon = "provider = Microsoft. Jet. oledb.4.0; Data Source =" + server. mappath ("excel.xls") + "; extended properties = Excel 8.0 ;";
Oledbconnection olecon = new oledbconnection (strcon );
Oledbdataadapter myda = new oledbdataadapter ("select * from [sheet1 $]", strcon );
Dataset myds = new dataset ();
Myda. Fill (myds );
Return myds;
}
Protected void button#click (Object sender, eventargs E)
{
Gridview1.datasource = createdatasource ();
Gridview1.databind ();
}
Due to the time relationship, thisArticleFirst come here. There are still a lot of skills to write after you have time, but I think if you canIntegration should be able to give a clear picture.