Aspose.Cells for .NET下载地址 https://soft51.cc/software/175799451582733643
在企业级应用中,Excel 文件不仅仅是数据的载体,更是信息展示的重要工具。良好的样式和格式化能够:
在 Aspose.Cells 中,样式系统主要由以下核心对象组成:
Style 对象:表示单元格的完整样式信息,包括字体、对齐、边框、背景等所有格式属性。
StyleFlag 对象:用于控制应用样式时的具体属性范围,提供精细化控制。
Font 对象:专门用于字体相关设置,如字体名称、大小、颜色、样式等。
Border 对象:控制单元格边框的样式、颜色和粗细。
样式应用通常遵循以下步骤:
// 方式1:创建全新样式
Style newStyle = workbook.CreateStyle();
// 方式2:复制现有单元格样式
Style copyStyle = cell.GetStyle();
// 方式3:从样式集合中获取
Style builtinStyle = workbook.Styles[0];
在 Excel 中,样式具有继承性,单元格样式会受到以下因素影响:
理解样式继承有助于更好地控制格式化效果。
字体是影响文档可读性的最重要因素之一。Aspose.Cells 提供了全面的字体控制能力。
字体族(Font.Name):
字体大小(Font.Size):
字体颜色(Font.Color):
Color.FromArgb(r, g, b)
Color.Black
、Color.Red
等Font.ThemeColor
属性粗体(Font.IsBold):
斜体(Font.IsItalic):
下划线(Font.Underline):
FontUnderlineType.Single
:单下划线FontUnderlineType.Double
:双下划线FontUnderlineType.SingleAccounting
:会计格式单下划线FontUnderlineType.DoubleAccounting
:会计格式双下划线删除线(Font.IsStrikeout):
上标下标:
Font.IsSuperscript
:上标,如数学公式Font.IsSubscript
:下标,如化学公式字符间距(Font.CharacterSpacing): 调整字符间的距离,可用于标题设计或特殊效果。
字体轮廓(Font.IsOutline): 创建空心字效果,常用于大标题。
字体阴影(Font.IsShadow): 为文字添加阴影效果,增强视觉层次。
对齐方式直接影响数据的展示效果和专业性。
TextAlignmentType.Left(左对齐):
TextAlignmentType.Center(居中对齐):
TextAlignmentType.Right(右对齐):
TextAlignmentType.Justify(两端对齐):
TextAlignmentType.Distributed(分散对齐):
TextAlignmentType.Top(顶端对齐):
TextAlignmentType.Center(垂直居中):
TextAlignmentType.Bottom(底端对齐):
自动换行(IsTextWrapped):
缩进(Indent):
文本旋转(RotationAngle):
文本方向(TextDirection):
数据类型导向:
功能导向:
美观导向:
背景设置是创建专业表格外观的重要手段,能够有效区分不同类型的数据和区域。
BackgroundType.Solid(纯色背景):
BackgroundColor
属性设置颜色BackgroundType.Gradient(渐变背景):
BackgroundType.Pattern(图案背景):
RGB 颜色:
Color.FromArgb(255, 0, 0) // 红色
Color.FromArgb(0, 255, 0) // 绿色
Color.FromArgb(0, 0, 255) // 蓝色
系统预定义颜色:
Color.Red, Color.Green, Color.Blue
Color.LightBlue, Color.LightGray
Color.Yellow, Color.Orange
主题颜色: Excel 主题颜色系统确保文档的颜色协调性,支持主题切换时自动调整颜色。
边框是表格结构的重要组成部分,能够清晰地定义数据边界和分组关系。
BorderType 枚举:
TopBorder
:上边框BottomBorder
:下边框LeftBorder
:左边框RightBorder
:右边框DiagonalDown
:从左上到右下的对角线DiagonalUp
:从左下到右上的对角线CellBorderType 枚举:
None
:无边框Thin
:细边框(最常用)Medium
:中等边框Thick
:粗边框Double
:双线边框Dotted
:点线边框Dashed
:虚线边框DashDot
:点划线边框DashDotDot
:双点划线边框外边框策略:
内边框策略:
分组边框策略:
条件边框策略:
表格头部通常需要与数据区域有明显区分:
交替行背景(斑马纹)能够提高表格可读性:
汇总行需要突出显示其重要性:
条件格式化是 Excel 中最强大的功能之一,能够根据单元格值自动应用格式。在企业数据分析中,条件格式化能够:
FormatConditionType.CellValue: 这是最基础的条件格式化类型,根据单元格的具体值进行判断。
比较操作符(OperatorType):
Equal
:等于指定值NotEqual
:不等于指定值Greater
:大于指定值GreaterOrEqual
:大于等于指定值Less
:小于指定值LessOrEqual
:小于等于指定值Between
:介于两个值之间NotBetween
:不在两个值之间应用场景:
FormatConditionType.Expression: 使用自定义公式进行复杂的条件判断,提供最大的灵活性。
公式特点:
典型应用:
=COUNTIF($A:$A,$A1)>1
=WEEKDAY(A1,2)<=5
=A1<TODAY()
=B1>A1
数据条在单元格内显示条形图,直观展示数值大小关系。
特点:
应用场景:
色阶通过颜色渐变显示数值的相对大小。
类型:
设计原则:
图标集通过不同图标显示数据的状态或等级。
内置图标集:
在复杂的业务场景中,往往需要同时应用多个条件:
使用命名区域或公式创建动态的条件阈值:
=A1>AVERAGE($A$1:$A$100)
=A1>PERCENTILE($A$1:$A$100,0.8)
=ABS(A1-AVERAGE($A$1:$A$100))>2*STDEV($A$1:$A$100)
条件格式化可以引用其他工作表的数据,实现更复杂的业务逻辑:
数据验证是保证 Excel 数据质量的关键机制,通过预设规则限制用户输入,从源头确保数据的准确性和一致性。
数值验证用于限制单元格只能输入指定范围内的数字。
验证条件:
应用场景:
专门用于整数输入的验证,确保输入值为整数。
特点:
应用场景:
确保输入的是有效的日期格式。
验证选项:
业务应用:
验证时间格式的输入。
验证规则:
控制文本输入的长度。
长度控制:
提供下拉选项列表,是最常用的数据验证类型。
列表来源:
"选项1,选项2,选项3"
=$A$1:$A$10
=CategoryList
=Sheet2!$A$1:$A$10
使用自定义公式进行复杂验证。
公式要求:
高级应用:
=COUNTIF($A:$A,A1)=1
=ISNUMBER(SEARCH("@",A1))
=IF(B1="是",A1<>"",TRUE)
级联下拉列表是企业应用中的常见需求,实现多级联动选择。
第二级下拉列表的选项根据第一级的选择动态变化。
实现方法:
公式示例:
=INDIRECT(SUBSTITUTE(A1," ","_"))
对于三级或更多级的级联,需要:
提示标题和内容:
提示时机:
警告级别:
错误消息:
将 Excel 作为数据收集表单使用:
建立企业主数据的标准字典:
通过数据验证实施业务规则:
下面通过一个完整的企业销售报表系统来展示样式与格式化的综合应用:
using Aspose.Cells;
using System;
using System.Drawing;
public class EnterpriseReportFormatter
{
private Workbook workbook;
private Worksheet reportSheet;
private Worksheet configSheet;
public void CreateFormattedSalesReport()
{
Console.WriteLine("创建企业销售报表格式化系统...");
// 初始化工作簿
workbook = new Workbook();
reportSheet = workbook.Worksheets[0];
reportSheet.Name = "销售报表";
configSheet = workbook.Worksheets.Add("配置表");
// 1. 创建报表标题区域
CreateReportHeader();
// 2. 设置数据表格
CreateDataTable();
// 3. 应用条件格式化
ApplyConditionalFormatting();
// 4. 设置数据验证
SetupDataValidation();
// 5. 创建汇总区域
CreateSummarySection();
// 6. 设置打印格式
SetupPrintFormatting();
// 保存文件
workbook.Save("EnterpriseFormattedSalesReport.xlsx");
Console.WriteLine("企业销售报表格式化系统创建完成!");
}
private void CreateReportHeader()
{
Console.WriteLine("创建报表标题区域...");
// 公司标题
reportSheet.Cells["A1"].Value = "ABC集团有限公司";
reportSheet.Cells.Merge(0, 0, 1, 8);
Style companyStyle = workbook.CreateStyle();
companyStyle.Font.Name = "微软雅黑";
companyStyle.Font.Size = 20;
companyStyle.Font.IsBold = true;
companyStyle.Font.Color = Color.DarkBlue;
companyStyle.HorizontalAlignment = TextAlignmentType.Center;
companyStyle.VerticalAlignment = TextAlignmentType.Center;
companyStyle.BackgroundColor = Color.FromArgb(240, 248, 255);
companyStyle.Pattern = BackgroundType.Solid;
reportSheet.Cells["A1"].SetStyle(companyStyle);
reportSheet.Cells.SetRowHeight(0, 35);
// 报表标题
reportSheet.Cells["A2"].Value = "2024年度销售业绩报表";
reportSheet.Cells.Merge(1, 0, 1, 8);
Style titleStyle = workbook.CreateStyle();
titleStyle.Font.Name = "微软雅黑";
titleStyle.Font.Size = 16;
titleStyle.Font.IsBold = true;
titleStyle.Font.Color = Color.White;
titleStyle.HorizontalAlignment = TextAlignmentType.Center;
titleStyle.VerticalAlignment = TextAlignmentType.Center;
titleStyle.BackgroundColor = Color.DarkBlue;
titleStyle.Pattern = BackgroundType.Solid;
reportSheet.Cells["A2"].SetStyle(titleStyle);
reportSheet.Cells.SetRowHeight(1, 30);
// 报表生成信息
reportSheet.Cells["A3"].Value = $"生成时间:{DateTime.Now:yyyy-MM-dd HH:mm}";
reportSheet.Cells["F3"].Value = "单位:万元";
Style infoStyle = workbook.CreateStyle();
infoStyle.Font.Size = 10;
infoStyle.Font.Color = Color.Gray;
reportSheet.Cells["A3"].SetStyle(infoStyle);
reportSheet.Cells["F3"].SetStyle(infoStyle);
}
private void CreateDataTable()
{
Console.WriteLine("创建数据表格...");
// 设置表头
string[] headers = {
"销售区域", "销售经理", "Q1销售额", "Q2销售额",
"Q3销售额", "Q4销售额", "年度总计", "完成率", "等级"
};
for (int i = 0; i < headers.Length; i++)
{
Cell headerCell = reportSheet.Cells[4, i];
headerCell.Value = headers[i];
}
// 设置表头样式
Range headerRange = reportSheet.Cells.CreateRange("A5:I5");
Style headerStyle = workbook.CreateStyle();
headerStyle.Font.Name = "微软雅黑";
headerStyle.Font.Size = 11;
headerStyle.Font.IsBold = true;
headerStyle.Font.Color = Color.White;
headerStyle.HorizontalAlignment = TextAlignmentType.Center;
headerStyle.VerticalAlignment = TextAlignmentType.Center;
headerStyle.BackgroundColor = Color.DarkSlateGray;
headerStyle.Pattern = BackgroundType.Solid;
// 设置边框
headerStyle.SetBorder(BorderType.TopBorder, CellBorderType.Medium, Color.Black);
headerStyle.SetBorder(BorderType.BottomBorder, CellBorderType.Medium, Color.Black);
headerStyle.SetBorder(BorderType.LeftBorder, CellBorderType.Thin, Color.White);
headerStyle.SetBorder(BorderType.RightBorder, CellBorderType.Thin, Color.White);
StyleFlag headerFlag = new StyleFlag();
headerFlag.All = true;
headerRange.ApplyStyle(headerStyle, headerFlag);
reportSheet.Cells.SetRowHeight(4, 25);
// 创建示例数据
object[,] salesData = {
{"华北区", "张经理", 145.6, 178.2, 165.8, 192.4, "", "", ""},
{"华东区", "李经理", 289.3, 312.7, 298.5, 345.1, "", "", ""},
{"华南区", "王经理", 198.7, 215.9, 234.2, 267.8, "", "", ""},
{"华西区", "赵经理", 167.4, 189.6, 178.3, 201.7, "", "", ""},
{"华中区", "钱经理", 223.8, 245.2, 256.9, 284.5, "", "", ""},
{"东北区", "孙经理", 134.2, 156.8, 143.5, 165.9, "", "", ""},
{"西北区", "周经理", 98.5, 112.3, 108.7, 125.4, "", "", ""},
{"西南区", "吴经理", 176.9, 198.4, 187.6, 218.3, "", "", ""}
};
// 写入基础数据(前6列)
for (int row = 0; row < salesData.GetLength(0); row++)
{
for (int col = 0; col < 6; col++)
{
reportSheet.Cells[row + 5, col].Value = salesData[row, col];
}
}
// 设置年度总计公式
for (int row = 5; row < 13; row++)
{
reportSheet.Cells[row, 6].Formula = $"=SUM(C{row + 1}:F{row + 1})";
}
// 设置完成率公式(假设年度目标为800万)
for (int row = 5; row < 13; row++)
{
reportSheet.Cells[row, 7].Formula = $"=G{row + 1}/800";
}
// 设置等级公式
for (int row = 5; row < 13; row++)
{
reportSheet.Cells[row, 8].Formula =
$"=IF(H{row + 1}>=1.2,\"优秀\",IF(H{row + 1}>=1,\"达标\",IF(H{row + 1}>=0.8,\"良好\",\"待改进\")))";
}
// 计算公式
workbook.CalculateFormula();
// 设置数据区域样式
Range dataRange = reportSheet.Cells.CreateRange("A6:I13");
Style dataStyle = workbook.CreateStyle();
dataStyle.SetBorder(BorderType.TopBorder, CellBorderType.Thin, Color.LightGray);
dataStyle.SetBorder(BorderType.BottomBorder, CellBorderType.Thin, Color.LightGray);
dataStyle.SetBorder(BorderType.LeftBorder, CellBorderType.Thin, Color.LightGray);
dataStyle.SetBorder(BorderType.RightBorder, CellBorderType.Thin, Color.LightGray);
dataStyle.HorizontalAlignment = TextAlignmentType.Center;
dataStyle.VerticalAlignment = TextAlignmentType.Center;
StyleFlag dataFlag = new StyleFlag();
dataFlag.Borders = true;
dataFlag.HorizontalAlignment = true;
dataFlag.VerticalAlignment = true;
dataRange.ApplyStyle(dataStyle, dataFlag);
// 设置交替行背景
for (int row = 5; row < 13; row += 2)
{
Range alternateRow = reportSheet.Cells.CreateRange(row, 0, 1, 9);
Style alternateStyle = workbook.CreateStyle();
alternateStyle.BackgroundColor = Color.FromArgb(248, 248, 248);
alternateStyle.Pattern = BackgroundType.Solid;
alternateRow.ApplyStyle(alternateStyle, new StyleFlag { BackgroundColor = true, Pattern = true });
}
// 设置数字格式
Range numberRange = reportSheet.Cells.CreateRange("C6:G13");
Style numberStyle = workbook.CreateStyle();
numberStyle.Custom = "#,##0.0";
numberRange.ApplyStyle(numberStyle, new StyleFlag { NumberFormat = true });
// 设置百分比格式
Range percentRange = reportSheet.Cells.CreateRange("H6:H13");
Style percentStyle = workbook.CreateStyle();
percentStyle.Custom = "0.0%";
percentRange.ApplyStyle(percentStyle, new StyleFlag { NumberFormat = true });
// 自动调整列宽
reportSheet.AutoFitColumns();
}
private void ApplyConditionalFormatting()
{
Console.WriteLine("应用条件格式化...");
// 1. 销售额数据条
Range salesRange = reportSheet.Cells.CreateRange("C6:F13");
FormatCondition salesCondition = reportSheet.ConditionalFormattings.AddCondition(
FormatConditionType.DataBar, OperatorType.None, null, null);
salesCondition.Areas.Add(salesRange);
salesCondition.DataBar.Color = Color.SkyBlue;
salesCondition.DataBar.AxisColor = Color.Black;
salesCondition.DataBar.ShowValue = true;
// 2. 年度总计色阶
Range totalRange = reportSheet.Cells.CreateRange("G6:G13");
FormatCondition totalCondition = reportSheet.ConditionalFormattings.AddCondition(
FormatConditionType.ColorScale, OperatorType.None, null, null);
totalCondition.Areas.Add(totalRange);
totalCondition.ColorScale.Is3ColorScale = true;
totalCondition.ColorScale.MinCfvo.Type = FormatConditionValueType.Min;
totalCondition.ColorScale.MinColor = Color.Red;
totalCondition.ColorScale.MidCfvo.Type = FormatConditionValueType.Percentile;
totalCondition.ColorScale.MidCfvo.Value = 50;
totalCondition.ColorScale.MidColor = Color.Yellow;
totalCondition.ColorScale.MaxCfvo.Type = FormatConditionValueType.Max;
totalCondition.ColorScale.MaxColor = Color.Green;
// 3. 完成率图标集
Range rateRange = reportSheet.Cells.CreateRange("H6:H13");
FormatCondition rateCondition = reportSheet.ConditionalFormattings.AddCondition(
FormatConditionType.IconSet, OperatorType.None, null, null);
rateCondition.Areas.Add(rateRange);
rateCondition.IconSet.Type = IconSetType.TrafficLights31;
rateCondition.IconSet.Cfvos[0].Type = FormatConditionValueType.Percent;
rateCondition.IconSet.Cfvos[0].Value = 80;
rateCondition.IconSet.Cfvos[1].Type = FormatConditionValueType.Percent;
rateCondition.IconSet.Cfvos[1].Value = 100;
// 4. 等级高亮
FormatCondition excellentCondition = reportSheet.ConditionalFormattings.AddCondition(
FormatConditionType.CellValue, OperatorType.Equal, "优秀", null);
excellentCondition.Areas.Add(reportSheet.Cells.CreateRange("I6:I13"));
excellentCondition.Style.BackgroundColor = Color.LightGreen;
excellentCondition.Style.Pattern = BackgroundType.Solid;
excellentCondition.Style.Font.IsBold = true;
excellentCondition.Style.Font.Color = Color.DarkGreen;
FormatCondition improvementCondition = reportSheet.ConditionalFormattings.AddCondition(
FormatConditionType.CellValue, OperatorType.Equal, "待改进", null);
improvementCondition.Areas.Add(reportSheet.Cells.CreateRange("I6:I13"));
improvementCondition.Style.BackgroundColor = Color.LightCoral;
improvementCondition.Style.Pattern = BackgroundType.Solid;
improvementCondition.Style.Font.IsBold = true;
improvementCondition.Style.Font.Color = Color.DarkRed;
}
private void SetupDataValidation()
{
Console.WriteLine("设置数据验证...");
// 在配置表创建下拉选项数据
string[] regions = {"华北区", "华东区", "华南区", "华西区", "华中区", "东北区", "西北区", "西南区"};
string[] managers = {"张经理", "李经理", "王经理", "赵经理", "钱经理", "孙经理", "周经理", "吴经理"};
string[] grades = {"优秀", "达标", "良好", "待改进"};
for (int i = 0; i < regions.Length; i++)
{
configSheet.Cells[i, 0].Value = regions[i];
configSheet.Cells[i, 1].Value = managers[i];
}
for (int i = 0; i < grades.Length; i++)
{
configSheet.Cells[i, 2].Value = grades[i];
}
// 创建命名区域
workbook.Worksheets.Names.Add("RegionList", configSheet.Cells.CreateRange("A1:A8"));
workbook.Worksheets.Names.Add("ManagerList", configSheet.Cells.CreateRange("B1:B8"));
workbook.Worksheets.Names.Add("GradeList", configSheet.Cells.CreateRange("C1:C4"));
// 为新增行设置数据验证(第14行及以后)
// 1. 销售区域下拉验证
Range regionValidationRange = reportSheet.Cells.CreateRange("A14:A50");
Validation regionValidation = reportSheet.Validations[reportSheet.Validations.Add()];
regionValidation.AreaList.Add(regionValidationRange);
regionValidation.Type = ValidationType.List;
regionValidation.Formula1 = "=RegionList";
regionValidation.InCellDropDown = true;
regionValidation.InputTitle = "选择销售区域";
regionValidation.InputMessage = "请从列表中选择一个销售区域";
// 2. 销售经理下拉验证
Range managerValidationRange = reportSheet.Cells.CreateRange("B14:B50");
Validation managerValidation = reportSheet.Validations[reportSheet.Validations.Add()];
managerValidation.AreaList.Add(managerValidationRange);
managerValidation.Type = ValidationType.List;
managerValidation.Formula1 = "=ManagerList";
managerValidation.InCellDropDown = true;
managerValidation.InputTitle = "选择销售经理";
managerValidation.InputMessage = "请从列表中选择一个销售经理";
// 3. 销售额数值验证
Range salesValidationRange = reportSheet.Cells.CreateRange("C14:F50");
Validation salesValidation = reportSheet.Validations[reportSheet.Validations.Add()];
salesValidation.AreaList.Add(salesValidationRange);
salesValidation.Type = ValidationType.Decimal;
salesValidation.Operator = OperatorType.GreaterOrEqual;
salesValidation.Formula1 = "0";
salesValidation.InputTitle = "输入销售额";
salesValidation.InputMessage = "请输入大于等于0的销售额数值(单位:万元)";
salesValidation.ErrorTitle = "输入错误";
salesValidation.ErrorMessage = "销售额必须是大于等于0的数值!";
salesValidation.ShowError = true;
// 4. 等级下拉验证
Range gradeValidationRange = reportSheet.Cells.CreateRange("I14:I50");
Validation gradeValidation = reportSheet.Validations[reportSheet.Validations.Add()];
gradeValidation.AreaList.Add(gradeValidationRange);
gradeValidation.Type = ValidationType.List;
gradeValidation.Formula1 = "=GradeList";
gradeValidation.InCellDropDown = true;
gradeValidation.InputTitle = "选择等级";
gradeValidation.InputMessage = "请选择业绩等级";
}
private void CreateSummarySection()
{
Console.WriteLine("创建汇总区域...");
// 汇总标题
reportSheet.Cells["A15"].Value = "业绩汇总";
reportSheet.Cells.Merge(14, 0, 1, 9);
Style summaryTitleStyle = workbook.CreateStyle();
summaryTitleStyle.Font.Size = 14;
summaryTitleStyle.Font.IsBold = true;
summaryTitleStyle.Font.Color = Color.White;
summaryTitleStyle.HorizontalAlignment = TextAlignmentType.Center;
summaryTitleStyle.BackgroundColor = Color.DarkGreen;
summaryTitleStyle.Pattern = BackgroundType.Solid;
reportSheet.Cells["A15"].SetStyle(summaryTitleStyle);
reportSheet.Cells.SetRowHeight(14, 25);
// 汇总数据
string[] summaryLabels = {
"总销售额:", "平均销售额:", "最高销售额:", "最低销售额:",
"达标区域数:", "优秀区域数:", "整体完成率:"
};
string[] summaryFormulas = {
"=SUM(G6:G13)",
"=AVERAGE(G6:G13)",
"=MAX(G6:G13)",
"=MIN(G6:G13)",
"=COUNTIFS(I6:I13,\"达标\")+COUNTIFS(I6:I13,\"良好\")+COUNTIFS(I6:I13,\"优秀\")",
"=COUNTIF(I6:I13,\"优秀\")",
"=SUM(G6:G13)/(800*8)"
};
for (int i = 0; i < summaryLabels.Length; i++)
{
int row = 16 + i;
reportSheet.Cells[row - 1, 0].Value = summaryLabels[i];
reportSheet.Cells[row - 1, 1].Formula = summaryFormulas[i];
// 设置标签样式
Style labelStyle = workbook.CreateStyle();
labelStyle.Font.IsBold = true;
labelStyle.HorizontalAlignment = TextAlignmentType.Right;
reportSheet.Cells[row - 1, 0].SetStyle(labelStyle);
// 设置数值样式
Style valueStyle = workbook.CreateStyle();
if (i < 4 || i == 6) // 金额和百分比
{
valueStyle.Custom = i == 6 ? "0.0%" : "#,##0.0";
}
valueStyle.Font.IsBold = true;
valueStyle.BackgroundColor = Color.LightYellow;
valueStyle.Pattern = BackgroundType.Solid;
reportSheet.Cells[row - 1, 1].SetStyle(valueStyle);
}
// 计算汇总公式
workbook.CalculateFormula();
}
private void SetupPrintFormatting()
{
Console.WriteLine("设置打印格式...");
// 设置页面设置
PageSetup pageSetup = reportSheet.PageSetup;
pageSetup.Orientation = PageOrientationType.Landscape; // 横向
pageSetup.PaperSize = PaperSizeType.PaperA4;
pageSetup.FitToPagesWide = 1;
pageSetup.FitToPagesTall = 0; // 自动调整高度
// 设置页边距(单位:英寸)
pageSetup.LeftMargin = 0.75;
pageSetup.RightMargin = 0.75;
pageSetup.TopMargin = 1.0;
pageSetup.BottomMargin = 1.0;
pageSetup.HeaderMargin = 0.3;
pageSetup.FooterMargin = 0.3;
// 设置页眉页脚
pageSetup.SetHeader(0, "&C&\"微软雅黑,Bold\"&16ABC集团销售报表");
pageSetup.SetHeader(2, "&D"); // 右侧显示日期
pageSetup.SetFooter(1, "&P / &N"); // 中间显示页码
pageSetup.SetFooter(2, "机密文件 - 仅限内部使用");
// 设置打印标题(每页都打印表头)
pageSetup.PrintTitleRows = "$5:$5";
// 设置打印区域
pageSetup.PrintArea = "A1:I22";
// 打印网格线
pageSetup.PrintGridlines = true;
// 设置打印质量
pageSetup.PrintQuality = 600;
// 居中打印
pageSetup.CenterHorizontally = true;
pageSetup.CenterVertically = false;
}
public static void Main(string[] args)
{
try
{
EnterpriseReportFormatter formatter = new EnterpriseReportFormatter();
formatter.CreateFormattedSalesReport();
Console.WriteLine("\n报表特性说明:");
Console.WriteLine("1. 企业级标题设计,体现专业形象");
Console.WriteLine("2. 标准化表格格式,清晰的数据展示");
Console.WriteLine("3. 多种条件格式化,自动突出重点数据");
Console.WriteLine("4. 完善的数据验证,保证数据输入质量");
Console.WriteLine("5. 智能汇总计算,自动统计关键指标");
Console.WriteLine("6. 专业打印设置,适合正式场合使用");
Console.WriteLine("\n请查看生成的Excel文件!");
}
catch (Exception ex)
{
Console.WriteLine($"程序执行出错: {ex.Message}");
}
Console.ReadKey();
}
}
建立样式规范:
创建样式模板:
可读性原则:
直观性原则:
// 推荐:批量应用样式
Range range = worksheet.Cells.CreateRange("A1:Z100");
Style style = workbook.CreateStyle();
// 设置样式属性...
range.ApplyStyle(style, new StyleFlag { All = true });
// 不推荐:逐个单元格设置
for(int i = 0; i < 100; i++)
{
worksheet.Cells[i, 0].SetStyle(style); // 低效
}
// 创建一次,多次使用
Style headerStyle = workbook.CreateStyle();
// 配置样式...
// 在多个位置使用相同样式
range1.ApplyStyle(headerStyle, flag);
range2.ApplyStyle(headerStyle, flag);
Style tempStyle = workbook.CreateStyle();
// 使用样式...
tempStyle = null; // 释放引用
通过掌握这些样式与格式化技术,你可以创建出专业、美观且功能完善的 Excel 报表系统,满足企业级应用的各种需求。记住,好的格式化不仅仅是美观,更重要的是提升信息传达的效率和准确性。
Aspose.Cells for .NET下载地址 https://soft51.cc/software/175799451582733643