Use the Aspose. Cells component to generate an Excel file instance

Source: Internet
Author: User

Generate an Excel file with a table header in the following format.

Of course, more complex operations can also be achieved by merging cells using the public void Merge (int firstRow, int firstColumn, int totalRows, int totalColumns) method.

Implementation Method:

1. First, add a reference to "Aspose. Cells. dll.

2. The implementation code is as follows:

Copy codeThe Code is as follows:
// Create a workbook
Workbook workbook = new Workbook (); // Workbook
Worksheet sheet = workbook. Worksheets [0]; // Worksheet
Cells cells = sheet. Cells; // Cell


Style style = workbook. Styles [workbook. Styles. Add ()]; // Add a Style

# Region Header
// Title
Style. HorizontalAlignment = TextAlignmentType. Center; // text Center
Style. Font. Name = ""; // text Font
Style. Font. Size = 18; // text Size
Style. Font. IsBold = true; // bold

Cells. Merge (0, 0, 1, 12); // Merge cells
Cells [0, 0]. PutValue ("Standardization Work opinion suggestion summary table"); // enter the content
Cells [0, 0]. SetStyle (style); // associate a style with a cell.
Cells. SetRowHeight (0, 28); // you can specify the Row Height.


// Release time
Style. HorizontalAlignment = TextAlignmentType. Left;
Style. Font. Size = 11;
Style. Font. IsBold = false;
Cells. Merge (1, 0, 1, 7 );
Cells [1, 0]. putValue (String. format ("Release start and end time: {0} to {1}", DateTime. now. addDays (-1 ). toString ("MM dd, yyyy"), DateTime. now. toString ("MM dd, yyyy ")));
Cells [1, 0]. SetStyle (style );
Cells. SetRowHeight (1, 20 );

// Statistical time
Style. HorizontalAlignment = TextAlignmentType. Right;
Style. Font. Size = 11;
Style. Font. IsBold = false;
Cells. Merge (1, 7, 1, 5 );
Cells [1, 7]. PutValue (String. Format ("statistical time: {0}", DateTime. Now. ToString ("MM dd, yyyy ")));
Cells [1, 7]. SetStyle (style );
Cells. SetRowHeight (1, 20 );
# Endregion

# Region table

# Region table title row
// Serial number
Style. HorizontalAlignment = TextAlignmentType. Center;
Cells [2, 0]. PutValue ("Serial Number ");
Cells [2, 0]. SetStyle (style );
Cells. SetRowHeight (2, 20 );
Cells. SetColumnWidthPixel (0, 38 );

// Suggested time
Cells [2, 1]. PutValue ("recommended time ");
Cells [2, 1]. SetStyle (style );
Cells. SetColumnWidthPixel (1, 77 );

// Recommended team
Cells [2, 2]. PutValue ("Recommended Department ");
Cells [2, 2]. SetStyle (style );
Cells. SetColumnWidthPixel (2,107 );

// Author
Cells [2, 3]. PutValue ("author ");
Cells [2, 3]. SetStyle (style );
Cells. SetColumnWidthPixel (3, 69 );

// Category
Cells [2, 4]. PutValue ("class ");
Cells [2, 4]. SetStyle (style );
Cells. SetColumnWidthPixel (4, 71 );

// Business type
Cells [2, 5]. PutValue ("business type ");
Cells [2, 5]. SetStyle (style );
Cells. SetColumnWidthPixel (5, 71 );

// Standard name
Cells [2, 6]. PutValue ("standard name ");
Cells [2, 6]. SetStyle (style );
Cells. SetColumnWidthPixel (6,114 );

// Standard chapter and entry number
Cells [2, 7]. PutValue ("Standard chapter, article number ");
Cells [2, 7]. SetStyle (style );
Cells. SetColumnWidthPixel (7,104 );

// Comments and Suggestions
Cells [2, 8]. PutValue ("Suggestions ");
Cells [2, 8]. SetStyle (style );
Cells. SetColumnWidthPixel (8,255 );

// Processing Department
Cells [2, 9]. PutValue ("Processing Department ");
Cells [2, 9]. SetStyle (style );
Cells. SetColumnWidthPixel (9, 72 );

// Processing progress
Cells [2, 10]. PutValue ("processing progress ");
Cells [2, 10]. SetStyle (style );
Cells. SetColumnWidthPixel (10, 72 );

// Remarks
Cells [2, 11]. PutValue ("Remarks ");
Cells [2, 11]. SetStyle (style );
Cells. SetColumnWidthPixel (11,255 );

# Endregion

# Endregion


System. IO. MemoryStream MS = workbook. SaveToStream (); // generate data stream
Byte [] bt = ms. ToArray ();

Workbook. Save (@ "E: \ test.xls"); // Save it to the hard disk
}

3. The generated Excel file can be saved to the disk or downloaded on the web page through an over-current method.

Copy codeThe Code is as follows:
// Download
System. IO. MemoryStream MS = workbook. SaveToStream (); // generate data stream
Byte [] bt = ms. ToArray ();

String fileName = "Standardization Work suggestion summary table" + DateTime. Now. ToString ("yyyyMMddHHmmss") + ". xls"; // file name saved by the client
// Download an object in the form of a streaming

Response. ContentType = "application/vnd. ms-excel ";

// Notify the browser to download the file instead of opening it
Response. AddHeader ("Content-Disposition", "attachment; filename =" + HttpUtility. UrlEncode (fileName, System. Text. Encoding. UTF8 ));
Response. BinaryWrite (bt );

Response. Flush ();
Response. End ();

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.