ASP. NET (C #) export data to Word or Excel

Source: Internet
Author: User

The simplest way is to export everything on the page.

Called during loading. Note that no other controls, including buttons, are available on the page.

Void converttoexcel ()

{

Response. Clear ();

Response. Buffer = true;

Response. Charset = "GB2312 ";

Response. AppendHeader ("Content-Disposition", "attachment; filename =" + DateTime. Now. ToString ("yyyyMMddhhmmss") + ". xls ");

Response. ContentEncoding = System. Text. Encoding. GetEncoding ("GB2312 ");

Response. ContentType = "application/ms-excel ";

This. Page. EnableViewState = false;

System. IO. StringWriter oStringWriter = new System. IO. StringWriter ();

System. Web. UI. HtmlTextWriter oHtmlTextWriter = new System. Web. UI. HtmlTextWriter (oStringWriter );

This. Page. RenderControl (oHtmlTextWriter );

Response. Write (oStringWriter. ToString ());

Response. End ();

}

ASP. NET (C #) export data to Word or Excel

Namespace:

Using System. IO;

Using System. Text;

Export data from the DataGrid to Excel

String excelname = "excel file name ";

HttpContext. Current. Response. Charset = "GB2312 ";

HttpContext. Current. Response. ContentEncoding = Encoding. UTF8;

HttpContext. Current. Response. ContentType = "application/ms-excel ";

HttpContext. Current. Response. AppendHeader ("Content-disposition", "attachment; filename =" + excelname + ". xls ");

Dr1.Page. EnableViewState = false;

StringWriter sw = new StringWriter ();

HtmlTextWriter tw = new HtmlTextWriter (sw );

Dr1.RenderControl (tw );

HttpContext. Current. Response. Write (sw. ToString ());

HttpContext. Current. Response. End ();

Export data from the DataGrid to the Word

String excelname = "word file name ";

HttpContext. Current. Response. Charset = "GB2312 ";

HttpContext. Current. Response. ContentEncoding = Encoding. UTF8;

HttpContext. Current. Response. ContentType = "application/ms-winword ";

HttpContext. Current. Response. AppendHeader ("Content-disposition", "attachment; filename =" + excelname + ". doc ");

Dr1.Page. EnableViewState = false;

StringWriter sw = new StringWriter ();

HtmlTextWriter tw = new HtmlTextWriter (sw );

Dr1.RenderControl (tw );

HttpContext. Current. Response. Write (sw. ToString ());

HttpContext. Current. Response. End ();

ASP. NET 2.0, C # ---- use the GridView control to export other files (export Excel files and export Word files)

// Note: if you use the GridView to export files on the Visual Studio2005 platform,

// The VerifyRenderingInServerForm method must be reloaded.

Public override void VerifyRenderingInServerForm (Control control Control)

{

}

///

/// Method for exporting data to a file,

///

/// Model = 1: Export to Execl, Model = 2: Export to Word

Private void toFiles (int Model)

{

String strFileName = DateTime. Now. ToString ("yyyyMMdd-hhmmss ");

System. Web. HttpContext HC = System. Web. HttpContext. Current;

HC. Response. Clear ();

HC. Response. Buffer = true;

HC. Response. ContentEncoding = System. Text. Encoding. UTF8; // set the output stream to simplified Chinese

If (Model = 1)

{

// --- Export as an Excel file

HC. Response. AddHeader ("Content-Disposition", "attachment; filename =" + HttpUtility. UrlEncode (strFileName, System. Text. Encoding. UTF8) + ". xls ");

HC. Response. ContentType = "application/ms-excel"; // set the output file type to an excel file.

}

Else

{

// --- Export as a Word file

HC. Response. AddHeader ("Content-Disposition", "attachment; filename =" + HttpUtility. UrlEncode (strFileName, System. Text. Encoding. UTF8) + ". doc ");

HC. Response. ContentType = "application/ms-word"; // set the output file type to Word.

}

System. IO. StringWriter sw = new System. IO. StringWriter ();

System. Web. UI. HtmlTextWriter htw = new System. Web. UI. HtmlTextWriter (sw );

This. GridView1.RenderControl (htw );

HC. Response. Write (sw. ToString ());

HC. Response. End ();

}

//-Export as an Excel file

Protected void ToExecl_Click (object sender, EventArgs e)

{

ToFiles (1 );

}

//-Export as a Word file

Protected void button#click (object sender, EventArgs e)

{

ToFiles (2 );

}

FROM: http://hi.baidu.com/jg_%B3%C2/blog/item/4f0edf188851c50135fa41ce.html

C # operate on Word [to]

Import COM Library: Microsoft word 11.0 Object Library.

Related Article

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.