Page Import and Export Excel

Source: Internet
Author: User

Reference

Using Microsoft. Office. InterOP. Excel;
Using system. reflection; // reflection namespace
Using system. IO;

 

Protected void button#click (Object sender, eventargs E)
{
// Introduce the Microsoft. Office. Excel component to the Project
// Export the second majority
// The first part generates an Excel file

// Object 1 Application Object
Microsoft. Office. InterOP. Excel. Application Excel = new Microsoft. Office. InterOP. Excel. Application ();
// Object 2: thin object
Workbook book = excel. workbooks. Add (missing. value );
// Object 3 worksheet object (the first worksheet in the workbook)
Worksheet worksheet = (worksheet) book. worksheets [1];

//////////////////////////////////////// //////////////////////////////////////// ////////
Worksheet. Name = "N-tier ";
Worksheet. cells [5, 6] = "Welcome to learn N-tier"; // pay attention to defining methods in cells.

Worksheet. get_range ("A2", missing. Value). value2 = "Week ";
Worksheet. get_range ("A2", missing. Value). Font. Name = "";
Worksheet. get_range ("A2", missing. Value). Font. size = 14;
Worksheet. get_range ("A2", missing. Value). rowheight = 28;

Worksheet. get_range ("A1", "Y1"). mergecells = true; // merge Cells
Worksheet. get_range ("A1", "Y1"). value2 = "weekly Usage Table of the classroom Data Center ";
Worksheet. get_range ("A1", "Y1"). horizontalalignment = Microsoft. Office. InterOP. Excel. xlhalign. xlhaligncenter;
Worksheet. get_range ("A1", "Y1"). verticalalignment = Microsoft. Office. InterOP. Excel. xlvalign. xlvalignbottom;
Worksheet. get_range ("A1", "Y1"). Font. Bold = true;
Worksheet. get_range ("A1", "Y1"). Font. Name = "";
Worksheet. get_range ("A1", "Y1"). Font. size = 20;
Worksheet. get_range ("A1", "Y1"). rowheight = 63;
//////////////////////////////////////// ///////////////////////////////////
// Save the Excel part
Excel. Visible = false; // visible to applications
// Excel file name
String excelname = server. mappath ("~ /Temp/") + datetime. Now. tostring (" yyyymmdd-hhmmss ") +". xls ";
Excel. activeworkbook. saveas (excelname, missing. Value, missing. Value
, Missing. Value, Microsoft. Office. InterOP. Excel. xlsaveasaccessmode. xlnochange, missing. Value, missing. Value
, Missing. Value, missing. Value, missing. value );
// Close the work thin
Excel. activeworkbook. Close (true, missing. Value, missing. value );
// Exit the application
Excel. Quit ();
//////////////////////////////////////// //////////////////////////////////////// ///////////////////////////////////////

// Part 2: Download the generated Excel File
Downloadfile (excelname, "student information table .xls", this );


//////////////////////////////////////// /////////////////////////

}


# Region downloadfile
/// <Summary>
/// Download all types of files
/// </Summary>
/// <Param name = "FILENAME"> virtual path of the file </param>
/// <Param name = "downfilename"> New name </param>
/// <Param name = "page"> Download Page Object </param>
Public void downloadfile (string filename, string downfilename, page)
{
// Clear all HTML output in the buffer
Page. response. Clear ();
Page. response. Buffer = true;
// Set the HTTP Character Set of the output stream
Page. response. charset = "UTF-8 ";
// The attachment parameter indicates downloading as an attachment
String attachment = "attachment; filename =" + httputility. urlencode (downfilename, system. Text. encoding. utf8 );
Page. response. addheader ("content-disposition", attachment );
// Set the httpmime type of the output stream (output as a stream)
Page. response. contenttype = "application/octent-stream ";
Page. response. writefile (filename );
Page. response. End ();
}
# Endregion
Protected void button2_click (Object sender, eventargs E)
{
// Introduce the Microsoft. Office. Excel component to the Project
// Export the second majority
// The first part generates an Excel file

 

// Object 1 Application Object
Microsoft. Office. InterOP. Excel. Application Excel = new Microsoft. Office. InterOP. Excel. Application ();
// Object 2: thin object
Workbook book = excel. workbooks. Add (missing. value );
// Object 3 worksheet object (the first worksheet in the workbook)
Worksheet worksheet = (worksheet) book. worksheets [1];

//////////////////////////////////////// //////////////////////////////////////// ////////
Dataclassesdatacontext datacontext = new dataclassesdatacontext ();
VaR stuslist = datacontext. Student. tolist ();

For (INT I = 0; I <stuslist. Count; I ++)
{
Worksheet. cells [I + 1, 1] = stuslist [I]. Name;
Worksheet. cells [I + 1, 2] = stuslist [I]. sex;
}

//////////////////////////////////////// ///////////////////////////////////
// Save the Excel part
Excel. Visible = false; // visible to applications
// Excel file name
String excelname = server. mappath ("~ /Temp/") + datetime. Now. tostring (" yyyymmdd-hhmmss ") +". xls ";
Excel. activeworkbook. saveas (excelname, missing. Value, missing. Value
, Missing. Value, Microsoft. Office. InterOP. Excel. xlsaveasaccessmode. xlnochange, missing. Value, missing. Value
, Missing. Value, missing. Value, missing. value );
// Close the work thin
Excel. activeworkbook. Close (true, missing. Value, missing. value );
// Exit the application
Excel. Quit ();
//////////////////////////////////////// //////////////////////////////////////// ///////////////////////////////////////

// Part 2: Download the generated Excel File
Downloadfile (excelname, "ssstudent information table .xls", this );
}

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.