. NET excel export method,. net excel Export

Source: Internet
Author: User

. NET excel export method,. net excel Export

// Export
Private string outFileName = "";

Private string fullFilename = "";
Private Workbook book = null;
Private Worksheet sheet = null;

Private void AddHeader (string [] dt)
{
Cell cell = null;

Int col = 0;
Foreach (string item in dt)
{
Cell = sheet. Cells [0, col];
Cell. PutValue (item );
Col ++;
}

}

 


Private void AddBody (DataTable dt, params int [] cl)
{
Int cls = 0;
Foreach (int item in cl)
{
For (int r = 0; r <dt. Rows. Count; r ++)
{
Sheet. Cells [r + 1, cls]. PutValue (dt. Rows [r] [item]. ToString ());
}
Cls ++;
}
}

 

Public string Export (DataTable dt)
{
Try
{
FullFilename = "Excel ";
Book = new Workbook ();
// Book. Open (tempfilename );
Sheet = book. Worksheets [0];
Sheet. Name = "data ";

// Sheet. Name = sheetName;
// AddTitle (title, dt. Columns. Count );
AddHeader (new string [] {"column 1", "column 2 ",...});
AddBody (dt, number of columns );
Sheet. AutoFitColumns ();
// Sheet. AutoFitRows ();
FileName = System. DateTime. Now. ToString ("yyyymmddhhmmss") + ". xls ";
String SaveFilePath = @ "\ DownLoadFile \" + fileName;
Book. Save (System. AppDomain. CurrentDomain. BaseDirectory + SaveFilePath );
This. fullFilename = System. AppDomain. CurrentDomain. BaseDirectory + SaveFilePath;
// Byte [] array = File. ReadAllBytes (fullFilename );
FileStream fs = new FileStream (fullFilename, FileMode. Open );
Byte [] data = new byte [fs. Length];
Fs. Read (data, 0, data. Length );

BinaryWriter w = new BinaryWriter (fs );
MemoryStream MS = new MemoryStream (data );
W. Write (ms. ToArray ());
W. Close ();
W. Dispose ();

Fs. Close ();
Fs. Dispose ();
// HttpContext. Current. Response. AppendHeader ("Content-Disposition", "attachment; filename =" + fileName );
// HttpContext. Current. Response. BinaryWrite (ms. ToArray ());
// HttpContext. Current. Response. End ();
Ms. Close ();
Ms. Dispose ();
Return fullFilename;
}
Catch (Exception e)
{
Return string. Empty;
}
}

 

Only the Export (DataTable dt) method is required for calling.

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.