. NET client export Excel implementation code and Considerations _ Practical Tips

Source: Internet
Author: User
Tags httpcontext
Client Export Excel
Copy Code code as follows:

/*
* Export DataGrid as Excel file
*
* @param strtitle file title
* @param dgdata to be exported datagrid
* @param istartcol starting column serial number
* @param iendcol End column ordinal
*
* Create Person: Calvin
* Date Created: 2005-10-08
* Modified Person:
* Date Modified:
**/
function Datagrid2excel (strtitle, Dgdata, Istartcol, Iendcol)
{
Define Excel Applicaiton Object
var appexcel = null;
Currently active workbook
var currentwork = null;
var currentsheet = null;
   
Try
{
Initialize Application
Appexcel = new ActiveXObject ("Excel.Application");
Appexcel.visible = true;
}
catch (E)
{
Window.alert ("Please Install Excel a");
     
Return
}
   
Get the currently active work Department
Currentwork = AppExcel.Workbooks.Add ();
CurrentSheet = Currentwork.activesheet;
 
Populating Excel Content
Set Title
Currentsheet.cells (1,1). Value = strtitle;
Currentsheet.cells (1,1). Value = Dgdata.innertext;
Window.alert (dgdata.innerhtml);
 
Fill content
for (var irow = 0; IRow < dgdata.rows.length-1; irow++)
{
Display the contents of a specified column
for (var icol = istartcol; icol <= iendcol; icol++)
{
Currentsheet.cells (IRow + 2, Icol + 1). Value =
Dgdata.rows[irow].cells[icol].innertext;
}
}
}

/**************************************************************************/
/**
* Export data from 0-3 columns in Dgdata to Excel files
**/
function Toexcel ()
{
Datagrid2excel ("Example of using JavaScript to export Excel", Document.getelementsbyid ("Dgdata"), 0, 3);
The disadvantage of this approach is that:
(1) It is possible to call Excel.Application on the client, and the security level of IE should be set to low.
(2) in the same way as the method, you can only export the data that is currently displayed in the DataGrid, and you cannot export the paging data.
--------------------------------------------------------------------------------
Ultimate Solution: Export a DataTable to Excel
OK, let's finish this boring post quickly. In general, the DataGrid on the page is a data source of a DataTable based on a query. So in order to import all the data into Excel, we just have to output the DataTable data source as Excel.
Copy Code code as follows:

/**////<summary>
Export DataTable content to Excel and return to client
</summary>
<param name= "Dgdata" > to be exported datatable</param>
Create Person: Chen Wenkei
Date Created: October 08, 2005
Modify Person:
Date Modified:
public static void Datatable2excel (System.Data.DataTable dtdata)
{
System.Web.UI.WebControls.DataGrid dgexport = null;
Current Conversation
System.Web.HttpContext curcontext = System.Web.HttpContext.Current;
IO is used to export and return Excel files
System.IO.StringWriter strwriter = null;
System.Web.UI.HtmlTextWriter htmlwriter = null;
if (dtdata!= null)
{
Set encoding and attachment formats
CurContext.Response.ContentType = "application/vnd.ms-excel";
CurContext.Response.ContentEncoding =system.text.encoding.utf8;
CurContext.Response.Charset = "";
        
Export Excel Files
Strwriter = new System.IO.StringWriter ();
HTMLWriter = new System.Web.UI.HtmlTextWriter (strwriter);
To address possible paging in dgdata, you need to redefine a DataGrid that has no paging
Dgexport = new System.Web.UI.WebControls.DataGrid ();
Dgexport.datasource = Dtdata.defaultview;
Dgexport.allowpaging = false;
Dgexport.databind ();
Back to Client
Dgexport.rendercontrol (HTMLWriter);
CurContext.Response.Write (Strwriter.tostring ());
CurContext.Response.End ();
}
}

Note that before you export Excel, you should change the column name of the DataTable to the text that the customer asks for, OK. Because it is derived from the DataTable, this approach solves the problem of paging data and is the ultimate solution.

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.