使用Aspose.Cells的基础知识整理

简介: 转自 http://www.cnblogs.com/kenblove/archive/2009/01/07/1371104.html这两天用Aspose.Cells构建一个Excel报表,感觉这个组件还比较好用.

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

这两天用Aspose.Cells构建一个Excel报表,感觉这个组件还比较好用.记录一下常用的使用知识:

1.创建Workbook和Worksheet

 

img_1c53668bcee393edac0d7b3b3daff1ae.gif img_405b18b4b6584ae338e0f6ecaf736533.gif
Workbook wb = new Workbook();
wb.Worksheets.Clear();
wb.Worksheets.Add(
"New Worksheet1");//New Worksheet1是Worksheet的name
Worksheet ws 
= wb.Worksheets[0];

 

如果直接用下边两句则直接使用默认的第一个Worksheet:

 

img_1c53668bcee393edac0d7b3b3daff1ae.gif img_405b18b4b6584ae338e0f6ecaf736533.gif
Workbook wb = new Workbook();
Worksheet ws 
= wb.Worksheets[0];

 

2.给Cell赋值设置背景颜色并加背景色:

 

img_1c53668bcee393edac0d7b3b3daff1ae.gif img_405b18b4b6584ae338e0f6ecaf736533.gif
Cell cell = ws.Cells[00];
cell.PutValue(
"填充"); //必须用PutValue方法赋值
cell.Style.ForegroundColor 
= Color.Yellow;
cell.Style.Pattern 
= BackgroundType.Solid;
cell.Style.Font.Size 
= 10;
cell.Style.Font.Color 
= Color.Blue;

 

自定义格式:

 

img_1c53668bcee393edac0d7b3b3daff1ae.gif img_405b18b4b6584ae338e0f6ecaf736533.gif
cell.Style.Custom = "ddd, dd mmmm 'yy";

 

旋转字体:

 

img_1c53668bcee393edac0d7b3b3daff1ae.gif img_405b18b4b6584ae338e0f6ecaf736533.gif
cell.Style.Rotation = 90;

 

 

3.设置Range并赋值加Style

 

img_1c53668bcee393edac0d7b3b3daff1ae.gif img_405b18b4b6584ae338e0f6ecaf736533.gif range1
int styleIndex = wb.Styles.Add();
Style style 
= wb.Styles[styleIndex];
style.ForegroundColor 
= Color.Yellow;
style.Pattern 
= BackgroundType.Solid;
style.Font.Size 
= 10;

//从Cells[0,0]开始创建一个2行3列的Range
Range range = ws.Cells.CreateRange(0023);
Cell cell 
= range[00];
cell.Style.Font 
= 9;
range.Style 
= style;
range.Merge();

 

注意Range不能直接设置Style.必须先定义style再将style赋给Style.其他设置和Cell基本一致.Range的Style会覆盖Cell定义的Style.另外必须先赋值再传Style.否则可能不生效.

 

4.使用Formula:

 

img_1c53668bcee393edac0d7b3b3daff1ae.gif img_405b18b4b6584ae338e0f6ecaf736533.gif formula1
ws.Cells[0,0].PutValue(1);
ws.Cells[
1,0].PutValue(20);
ws.Cells[
2,0].Formula="SUM(A1:B1)";
wb.CalculateFormula(
true);

 

Save Excel文件的时候必须调用CalculateFormula方法计算结果.

 

5.插入图片:

 

img_1c53668bcee393edac0d7b3b3daff1ae.gif img_405b18b4b6584ae338e0f6ecaf736533.gif pictures1
string imageUrl = System.Web.HttpContext.Current.Server.MapPath("~/images/log_topleft.gif");
ws.Pictures.Add(
1010, imageUrl);

 

6.使用Validations:

 

img_1c53668bcee393edac0d7b3b3daff1ae.gif img_405b18b4b6584ae338e0f6ecaf736533.gif validations1
Cells cells = ws.Cells;

cells[
120].PutValue("Please enter a number other than 0 to 10 in B1 to activate data validation:");
cells[
120].Style.IsTextWrapped = true;

cells[
121].PutValue(5);
Validations validations 
= totalSheet.Validations;

Validation validation 
= validations[validations.Add()];
//Set the data validation type
validation.Type = ValidationType.WholeNumber;
//Set the operator for the data validation
validation.Operator = OperatorType.Between;
//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;

//设置Validations的区域,因为现在要Validations的位置是12,1,所以下面设置对应的也要是12,1
CellArea cellArea;
cellArea.StartRow 
= 12;
cellArea.EndRow 
= 12;
cellArea.StartColumn 
= 1;
cellArea.EndColumn 
= 1;
validation.AreaList.Add(cellArea);

/*
要注意 的地方Validations 也是和Range的Style一样,要新增的,否则不生效
*/

 

上边不过是基础中的基础,要了解更多就要自己不断的尝试了.下边奉上帮助文档和DLL:

点击这里下载!

 

附送Excel支持的56种颜色: Excel报表支持的56种颜色(Excel2003及以下版本)

 
目录
相关文章
|
1月前
|
区块链
Excel常识整理
Excel常识整理
13 0
LaTeX高效写作系列:word表格转LaTeX
Fancy版本见九天学者的个人博客,关注文集博士干点啥或者微信公众号九天学者及时获取连载更新。 如何将word表格转为格式 迫于无奈从刚开始学习计算机就上了某软这条贼船,不少情况下,将表格写为了word文件。
3062 0
WORD必学技巧:使用项目编号
WORD必学技巧:使用项目编号
169 0
WORD必学技巧:使用项目编号