DataGrid export Word and Excel

Source: Internet
Author: User

<% @ Page Language = "C #" codebehind = "webform3.aspx. cs" autoeventwireup = "false" inherits = "web_zwt.webform3" %>
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> webform3 </title>
<Meta content = "Microsoft Visual Studio. NET 7.1" name = "generator">
<Meta content = "C #" name = "code_language">
<Meta content = "JavaScript" name = "vs_defaultclientscript">
<Meta content = "http://schemas.microsoft.com/intellisense/ie5" name = "vs_targetschema">
</Head>
<Body ms_positioning = "gridlayout">
<Form ID = "form1" method = "Post" runat = "server">
<Font face = "">
<Asp: DataGrid id = "datagrid1" style = "Z-INDEX: 101; left: 24px; position: absolute; top: 128px"
Runat = "server"> </ASP: DataGrid> <asp: button id = "button1" style = "Z-INDEX: 102; left: 24px; position: absolute; top: 24px "runat =" server"
TEXT = "Export word"> </ASP: button> <asp: button id = "button2" style = "Z-INDEX: 103; left: 120px; position: absolute; top: 24px "runat =" server"
TEXT = "Export excel"> </ASP: button> <asp: Label id = "label1" style = "Z-INDEX: 104; left: 24px; position: absolute; top: 88px "runat =" server "> This is useful for me. in "Reference", enter reference-> com-> Microsoft Excel 10.0 Object Library. </ASP: Label> </font> </form>
</Body>
</Html>

Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Using system. IO;
Using system. Windows. forms;

Namespace web_zwt
{
/// <Summary>
/// Summary of webform3.
/// </Summary>
Public class webform3: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. DataGrid datagrid1;
Protected system. Web. UI. webcontrols. Button button1;
Protected system. Web. UI. webcontrols. Button button2;
Protected system. Web. UI. webcontrols. Label label1;
Config con = new config ();
Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
// Add reference in "Reference"-> com-> Microsoft Excel 10.0 Object Library.

Dataset DS = con. DS ("select * From tab1 ");
Datagrid1.datasource = Ds;
Datagrid1.databind ();
}

/// <Summary>
/// Excel
/// </Summary>
/// <Param name = "ds"> dataset </param>
/// <Param name = "typeid"> // when typeid = "1", the exported file is in Excel format; XML format file exported when typeid = "2" </param>
/// <Param name = "FILENAME"> exported name </param>
Public void createexcel (Dataset ds, string typeid, string filename)
{
Httpresponse resp;
Resp = page. response;
Resp. Clear ();
Resp. Buffer = true;
Resp. charset = "gb2312"; // set the type to Chinese to prevent garbled characters.
Resp. contentencoding = system. Text. encoding. getencoding ("gb2312 ");
Resp. appendheader ("content-disposition", "attachment; filename =" + filename );
Resp. contentencoding = system. Text. encoding. getencoding ("gb2312"); // set the output stream to simplified Chinese
Resp. contenttype = "application/MS-excel"; // set the output file type to an Excel file. // Image/JPEG; text/html; image/GIF; VND. MS-Excel/MSWord

String colheaders = "", ls_item = "";
// Int I = 0;

// Define the table object and row object, and use dataset to initialize its value
Datatable dt = Ds. Tables [0];
Datarow [] myrow = DT. Select ("");
// When typeid = "1", the exported file is in Excel format; When typeid = "2", the exported file is in XML format.
If (typeid = "1 ")
{
For (INT I = 0; I <DT. Columns. Count-1; I ++)
{
// Obtain the titles of each column in the data table. Separate the headers with a carriage return character.
Colheaders + = DT. Columns [I]. Caption. tostring () + "/t ";
}
Colheaders + = DT. Columns [DT. Columns. Count-1]. Caption. tostring () + "/N ";
// Write the obtained data to the HTTP output stream
Resp. Write (colheaders );
// Process data row by row
Foreach (datarow row in myrow)
{
For (INT I = 0; I <DT. Columns. Count-1; I ++)
{
// In the current row, data is obtained one by one, separated by/t, and carriage return/N is added at the end
Ls_item + = row [I]. tostring () + "/t ";
}
Ls_item + = row [DT. Columns. Count-1]. tostring () + "/N ";
// Write the data in the current row to the HTTP output stream, and leave ls_item empty for downstream data
Resp. Write (ls_item );
Ls_item = "";
}
}
Else
{
If (typeid = "2 ")
{
// Export XML data directly from dataset and write it to the HTTP output stream
Resp. Write (Ds. getxml ());
}
}
// Write the data in the buffer to the HTTP header file
Resp. End ();

}
/// <Summary>
/// Word
/// </Summary>
/// <Param name = "ds"> dataset </param>
/// <Param name = "typeid"> // when typeid = "1", the exported file is in the Word format; typeid = "2" No </param>
/// <Param name = "FILENAME"> exported name </param>
Public void createword (Dataset ds, string typeid, string filename)
{
Httpresponse resp;
Resp = page. response;
Resp. Clear ();
Resp. Buffer = true;
Resp. charset = "gb2312"; // set the type to Chinese to prevent garbled characters.
Resp. contentencoding = system. Text. encoding. getencoding ("gb2312 ");
Resp. appendheader ("content-disposition", "attachment; filename =" + filename );
Resp. contentencoding = system. Text. encoding. getencoding ("gb2312"); // set the output stream to simplified Chinese
Resp. contenttype = "application/MS-excel ";

Datatable dt = Ds. Tables [0];
Datarow [] myrow = DT. Select ("");
String STR = string. empty;
STR + = "<Table> ";
If (typeid = "1 ")
{
STR + = "<tr> ";
For (INT I = 0; I <DT. Columns. Count; I ++)
{
STR + = "<TD>" + dt. Columns [I]. Caption. tostring () + "</TD> ";
}
STR + = "</tr> ";
Foreach (datarow row in myrow)
{
STR + = "<tr> ";
For (INT I = 0; I <DT. Columns. Count; I ++)
{
STR + = "<TD>" + row [I]. tostring () + "</TD> ";
}
STR + = "</tr> ";

}
STR + = "</table> ";
Resp. Write (STR );
}
Else
{
If (typeid = "2 ")
{
}
}
// Write the data in the buffer to the HTTP header file
Resp. End ();

 

}

# Code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. button1.click + = new system. eventhandler (this. button#click );
This. button2.click + = new system. eventhandler (this. button2_click );
This. Load + = new system. eventhandler (this. page_load );

}
# Endregion

Private void button2_click (Object sender, system. eventargs E)
{
Dataset DS = con. DS ("select * From tab1 ");
Createexcel (DS, "1", "aaa.xls ");
}

Private void button#click (Object sender, system. eventargs E)
{
Dataset DS = con. DS ("select * From tab1 ");
Createword (DS, "1", "aaa.doc ");
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.