Asp.net export standard Excel

Source: Internet
Author: User

References: http://www.cnblogs.com/jeffhsu/archive/2011/08/24/2152013.html

I have read the jeffhsu method, but I need to make an Excel template. I have improved it by referring to the online materials.

// Asp.net export EXCEL
Private void ExportExcel ()
{
// Temporary file
String tempFile = string. Format ("{0}/1_12.16.xls", System. Environment. GetEnvironmentVariable ("TEMP"), Guid. NewGuid ());

// Use OleDb to connect
OleDbConnection con = new OleDbConnection ("Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" + tempFile + "; Extended Properties = Excel 8.0 ");
Using (con)
{
Con. Open ();
// Create a Sheet
OleDbCommand created CREATE = new OleDbCommand ("create table Sheet1 ([ID] VarChar, [name] VarChar, [time] VarChar)", con );
Using create. ExecuteNonQuery ();

// Insert data
OleDbCommand cmd = new OleDbCommand (@ "insert into [Sheet1 $] VALUES (@ ID, @ name, @ time)", con );
Cmd. Parameters. AddWithValue ("@ ID", "1 ");
Cmd. Parameters. AddWithValue ("@ name", "Haha ");
Cmd. Parameters. AddWithValue ("@ time", DateTime. Now );
Cmd. ExecuteNonQuery ();
}

Response. ContentType = "application/ms-excel ";
Response. AppendHeader ("Content-Disposition", "attachment?filename=info.xls ");
Response. BinaryWrite (File. ReadAllBytes (tempFile ));

File. Delete (tempFile );
}

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.