Export Asp.net to Excel

Source: Internet
Author: User

The problem of exporting data from Asp.net to Excel is already a commonplace. I suddenly used it today. I felt a bit messy, so I encapsulated it and pasted the code for reference only.

 

Using System;
Using System. Collections. Generic;
Using System. IO;
Using System. Text;
Using System. Web;
Using System. Data;

Namespace AK. T. FileDeal
{
Public class Excel
{

/// <Summary>
/// Export to the Excel worksheet with the tititile header. The datatable field must correspond to the title
/// </Summary>
/// <Param name = "ds"> </param>
/// <Param name = "title"> </param>
Public void CreateExcel (DataTable ds, string [] title, System. Web. UI. Page page)
{
HttpResponse resp;
Resp = page. Response;
Resp. ContentEncoding = System. Text. Encoding. GetEncoding ("GB2312 ");
Resp. AppendHeader ("Content-Disposition", "attachment; filename = Cards _" + DateTime. Now. to1_datestring () + ". xls ");

String ls_item = "";
String colHeaders = "";
DataTable dt = ds;
DataRow [] myRow = dt. Select ("");

For (int I = 0; I <title. Length; I ++)
{
If (I = title. Length-1)
{
ColHeaders + = title [I] + "\ n ";
Continue;
}
ColHeaders + = title [I] + "\ t ";
}

Int count = title. Length;

Foreach (DataRow row in myRow)
{
For (int I = 0; I <count; I ++)
{
If (I = count-1)
{
Ls_item + = row [I]. ToString () + "\ n ";
Continue;
}
Ls_item + = row [I]. ToString () + "\ t ";
}
}
Resp. Write (colHeaders + ls_item );
Resp. End ();
}

/// <Summary>
/// Export to Excel. titile is the excel header, and DtRow is the name of the field in ds.
/// </Summary>
/// <Param name = "ds"> </param>
/// <Param name = "title"> </param>
/// <Param name = "DtRow"> </param>
Public void CreateExcel (DataTable ds, string [] title, string [] DtRow, System. Web. UI. Page page)
{
If (DtRow. Length! = Ds. Rows. Count)
{
Return;
}
HttpResponse resp;
Resp = page. Response;
Resp. ContentEncoding = System. Text. Encoding. GetEncoding ("GB2312 ");
Resp. AppendHeader ("Content-Disposition", "attachment; filename = Cards _" + DateTime. Now. to1_datestring () + ". xls ");

String ls_item = "";
String colHeaders = "";
DataTable dt = ds;
DataRow [] myRow = dt. Select ("");

For (int I = 0; I <title. Length; I ++)
{
If (I = title. Length-1)
{
ColHeaders + = title [I] + "\ n ";
Continue;
}
ColHeaders + = title [I] + "\ t ";
}
Foreach (DataRow row in myRow)
{
For (int I = 0; I <DtRow. Length; I ++)
{
If (I = DtRow. Length-1)
{
Ls_item + = row [DtRow [I]. ToString () + "\ n ";
Continue;
}
Ls_item + = row [DtRow [I]. ToString () + "\ t ";
}
}
Resp. Write (colHeaders + ls_item );
Resp. End ();
}

/// <Summary>
/// Export a single row of data
/// </Summary>
/// <Param name = "title"> </param>
/// <Param name = "Content"> </param>
Public void CreateOnceExcel (string [] title, string [] Content, System. Web. UI. Page)
{
HttpResponse resp;
Resp = page. Response;
Resp. ContentEncoding = System. Text. Encoding. GetEncoding ("GB2312 ");
Resp. AppendHeader ("Content-Disposition", "attachment; filename = Cards _" + DateTime. Now. to1_datestring () + ". xls ");

String ls_item = "";
String colHeaders = "";

For (int I = 0; I <title. Length; I ++)
{
If (I = title. Length-1)
{
ColHeaders + = title [I] + "\ n ";
Continue;
}
ColHeaders + = title [I] + "\ t ";
}
For (int I = 0; I <Content. Length; I ++)
{
If (I = Content. Length-1)
{
Ls_item + = Content [I]. ToString () + "\ n ";
Continue;
}
Ls_item + = Content [I]. ToString () + "\ t ";
}

Resp. Write (colHeaders + ls_item );
Resp. End ();
}

}
}

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.