Basic knowledge of aspose. Cells

Source: Internet
Author: User

From http://www.cnblogs.com/kenblove/archive/2009/01/07/1371104.html

In the past two days, I have used aspose. Cells to build an Excel report. I feel that this component is quite useful. I will record the common usage knowledge:

1. Create a workbook and a worksheet

 


Workbook WB= NewWorkbook ();
WB. worksheets. Clear ();
WB. worksheets. Add ("New worksheet1"); // New worksheet1 is the name of Worksheet
Worksheet WS=WB. worksheets [0];

 

If the following two statements are used directly, the default first worksheet is used:

 


Workbook WB= NewWorkbook ();
Worksheet WS=WB. worksheets [0];

 

2. assign a value to the cell to set the background color and add the background color:

 


Cell cell = WS. cells [ 0 , 0 ];
Cell. putvalue ( " Fill " ); // Values must be assigned using the putvalue method.
Cell. style. foregroundcolor = Color. Yellow;
Cell. style. Pattern = Backgroundtype. Solid;
Cell. style. Font. Size = 10 ;
Cell. style. Font. Color = Color. blue;

 

Custom format:

 


Cell. style. Custom= "DDD, DD mmmm'yy";

 

Rotation Font:

 


Cell. style. Rotation= 90;

 

 

3. Set range and assign a value plus Style

 

Range1
Int Styleindex = WB. Styles. Add ();
Style = WB. styles [styleindex];
Style. foregroundcolor = Color. Yellow;
Style. Pattern = Backgroundtype. Solid;
Style. Font. Size = 10 ;

// Create a range of 2 rows and 3 columns from cells []
Range = WS. cells. createRange ( 0 , 0 , 2 , 3 );
Cell cell = Range [ 0 , 0 ];
Cell. style. Font = 9 ;
Range. Style = Style;
Range. Merge ();

 

Note that the style cannot be directly set for range. you must first define the style and then assign it to the style. other settings are basically the same as cell settings. the style of range will overwrite the style defined by cell. in addition, you must assign a value before passing the style. otherwise, it may not take effect.

 

4. Use formula:

 

Formula1
WS. cells [ 0 , 0 ]. Putvalue ( 1 );
WS. cells [ 1 , 0 ]. Putvalue ( 20 );
WS. cells [ 2 , 0 ]. Formula = " Sum (A1: B1) " ;
WB. calculateformula ( True );

 

When saving an Excel file, you must call the calculateformula method to calculate the result.

 

5. Insert an image:

 

Pictures1
StringImageurl=System. Web. httpcontext. Current. server. mappath ("~ /Images/log_topleft.gif");
WS. Pictures. Add (10,10, Imageurl );

 

6. Use validations:

 

Validations1
Cells = WS. cells;

cells [ 12 , 0 ]. putvalue ( " Please enter a number other than 0 to 10 in B1 to activate data validation: " );
cells [ 12 , 0 ]. style. istextwrapped = true ;

Cells [12,1]. Putvalue (5);
Validations=Totalsheet. validations;

Validation = Validations [validations. Add ()];
// Set the data validation type
Validation. Type = Validationtype. wholenumber;
// Set the operator for the data validation
Validation. Operator = Operatortype.;
// Set the value or expression associated with the data validation
Validation. formula1 = " 0 " ;
// The value or expression associated with the second part of the data validation
Validation. formula2 = " 10 " ;

Validation. showerror = True ;
// Set the validation alert style
Validation. alertstyle = Validationalerttype. Information;
// Set the title of the data-validation error dialog box
Validation. errortitle = " Error " ;
// Set the data validation error message
Validation. errormessage = " Enter value between 0 to 10 " ;
// Set the data validation input message
Validation. inputmessage = " Data validation using condition for numbers " ;
Validation. ignoreblank = True ;
Validation. showinput = True ;
Validation. showerror = True ;

//Set the validations region, because the current location of validations is 12, 1, so the following settings correspond to 12, 1
Cellarea;
Cellarea. startrow= 12;
Cellarea. endrow= 12;
Cellarea. startcolumn= 1;
Cellarea. endcolumn= 1;
Validation. arealist. Add (cellarea );

/*
It should be noted that validations is also the same as the style of range, to be added, otherwise it will not take effect
*/

 

The above is just the foundation of the Foundation. To learn more, you must constantly try it. Below are the help documents and DLL:

Click here to download!

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.