原創|使用教程|編輯:何躍|2022-01-17 14:36:27.830|閱讀 201 次
概述:有時候在一些自定義場景下我們需要給excel表格打上標簽格式,比如說財務想給每個月花費最高和最低員工打上標記,在輸出表格時內容就已經處理完畢。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
首先,下載Spire.xls功能類庫 ,點擊這里下載 ;
然后,在后端代碼引入命名空間。
using Spire.Xls; using Spire.Xls.Core.Spreadsheet.ConditionalFormatting; using Spire.Xls.Core.Spreadsheet.Collections; using Spire.Xls.Core;
Workbook workbook = new Workbook(); workbook.LoadFromFile(@"..\..\..\..\..\..\Data\ConditionalFormatting.xlsx"); //按索引指定sheet表 Worksheet sheet = workbook.Worksheets[0]; sheet.AllocatedRange.RowHeight = 15; sheet.AllocatedRange.ColumnWidth = 16; //創建條件格式樣式 XlsConditionalFormats xcfs1 = sheet.ConditionalFormats.Add(); xcfs1.AddRange(sheet.Range["A1:D1"]); IConditionalFormat cf1 = xcfs1.AddCondition(); cf1.FormatType = ConditionalFormatType.CellValue; cf1.FirstFormula = "150"; cf1.Operator = ComparisonOperatorType.Greater; cf1.FontColor = Color.Red; cf1.BackColor = Color.LightBlue; XlsConditionalFormats xcfs2 = sheet.ConditionalFormats.Add(); xcfs2.AddRange(sheet.Range["A2:D2"]); IConditionalFormat cf2 = xcfs2.AddCondition(); cf2.FormatType = ConditionalFormatType.CellValue; cf2.FirstFormula = "300"; cf2.Operator = ComparisonOperatorType.Less; //設置邊框 cf2.LeftBorderColor = Color.Pink; cf2.RightBorderColor = Color.Pink; cf2.TopBorderColor = Color.DeepSkyBlue; cf2.BottomBorderColor = Color.DeepSkyBlue; cf2.LeftBorderStyle = LineStyleType.Medium; cf2.RightBorderStyle = LineStyleType.Thick; cf2.TopBorderStyle = LineStyleType.Double; cf2.BottomBorderStyle = LineStyleType.Double; //添加數據條 XlsConditionalFormats xcfs3 = sheet.ConditionalFormats.Add(); xcfs3.AddRange(sheet.Range["A3:D3"]); IConditionalFormat cf3 = xcfs3.AddCondition(); cf3.FormatType = ConditionalFormatType.DataBar; cf3.DataBar.BarColor = Color.CadetBlue; //添加icon XlsConditionalFormats xcfs4 = sheet.ConditionalFormats.Add(); xcfs4.AddRange(sheet.Range["A4:D4"]); IConditionalFormat cf4 = xcfs4.AddCondition(); cf4.FormatType = ConditionalFormatType.IconSet; cf4.IconSet.IconSetType = IconSetType.ThreeTrafficLights1; //添加顏色 XlsConditionalFormats xcfs5 = sheet.ConditionalFormats.Add(); xcfs5.AddRange(sheet.Range["A5:D5"]); IConditionalFormat cf5 = xcfs5.AddCondition(); cf5.FormatType = ConditionalFormatType.ColorScale; //在 "A7:D7 "范圍內用BurlyWood顏色突出重復的數值 XlsConditionalFormats xcfs6 = sheet.ConditionalFormats.Add(); xcfs6.AddRange(sheet.Range["A6:D6"]); IConditionalFormat cf6 = xcfs6.AddCondition(); cf6.FormatType = ConditionalFormatType.DuplicateValues; cf6.BackColor = Color.BurlyWood; //保存 workbook.SaveToFile("Output.xlsx", ExcelVersion.Version2010);
以上內容設置,我們可以在應用程序中為數據貼標簽后輸出,效率會比輸出后人工打標簽更為快捷。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn