Using the Aspose.cells component to generate an Excel file instance _ practical Tips

Source: Internet
Author: User

Generates an Excel file with a header, formatted as shown below.

Of course, some of the more complicated things can also be implemented by merging cells by using the method public void merge (int firstrow, int firstcolumn, int totalrows, int totalcolumns).

Implementation mode:

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

2. The implementation code is as follows:

Copy Code code as follows:

Create a new workbook
Workbook workbook = new Workbook (); Work book
Worksheet sheet = workbook. Worksheets[0]; Work table
Cells Cells = sheet. cells;//cell


Style style = workbook. Styles[workbook. Styles.add ()];//new style

#region Table Header
Title
Style. HorizontalAlignment = textalignmenttype.center;//text centered
Style. Font.Name = "Song body";//Text font
Style. Font.Size = 18;//Text Size
Style. Font.isbold = true;//Bold

            cells. Merge (0, 0, 1,);              //merged cells
            cells[0, 0]. Putvalue ("Standardized Work advice summary form");  //Fill in content
             cells[0, 0]. SetStyle (Style);           //To Cell Association style  
             cells. Setrowheight (0);             //Set row height  


           //Publish 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 ("Publish from: {0} to {1}", DateTime.Now.AddDays (-1). ToString ("YYYY year mm month DD Day"), DateTime.Now.ToString ("yyyy mm month DD day"));
            cells[1, 0]. SetStyle (style);
            cells. Setrowheight (1);

           //Statistics 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 ("statistic time: {0}", DateTime.Now.ToString ("YYYY year mm month DD Day"));
            cells[1, 7]. SetStyle (style);
            cells. Setrowheight (1, 20);
            #endregion

#region Table

#region Table header 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);

Recommended time
Cells[2, 1]. Putvalue ("Suggested Time");
Cells[2, 1]. SetStyle (style);
Cells. Setcolumnwidthpixel (1, 77);

Suggested department
Cells[2, 2]. Putvalue ("proposed department");
Cells[2, 2]. SetStyle (style);
Cells. Setcolumnwidthpixel (2, 107);

Advise people
Cells[2, 3]. Putvalue ("advising person");
Cells[2, 3]. SetStyle (style);
Cells. Setcolumnwidthpixel (3, 69);

Category
Cells[2, 4]. Putvalue ("category");
Cells[2, 4]. SetStyle (style);
Cells. Setcolumnwidthpixel (4, 71);

Type of Business
Cells[2, 5]. Putvalue ("Type of Business");
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, article 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 ("Opinion proposal");
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);

Process Progress
Cells[2, 10]. Putvalue ("Process progress");
Cells[2, 10]. SetStyle (style);
Cells. Setcolumnwidthpixel (10, 72);

Note
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");//saved to hard disk
}

3. The generated Excel can be saved to disk, or it can be downloaded in a streaming way on a Web page.

Copy Code code as follows:

Download
System.IO.MemoryStream ms = Workbook. Savetostream ()//Generate data stream
byte[] bt = Ms. ToArray ();

string filename = "Standardized Work advice summary table" + DateTime.Now.ToString ("YYYYMMDDHHMMSS") + ". xls";//client saved file name
Downloading files as streams of characters

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

Notifies 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.