Skip to content

Commit a2c748d

Browse files
committed
fix: 调整扩展方法,优化单元格设置
1 parent 5f2d564 commit a2c748d

File tree

11 files changed

+872
-63
lines changed

11 files changed

+872
-63
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
namespace Bing.Offices.Metadata
2+
{
3+
/// <summary>
4+
/// 合并区域信息
5+
/// </summary>
6+
public class MergedRegionInfo
7+
{
8+
/// <summary>
9+
/// 索引
10+
/// </summary>
11+
public int Index { get; set; }
12+
13+
/// <summary>
14+
/// 左上角X坐标
15+
/// </summary>
16+
public int FirstRow { get; set; }
17+
18+
/// <summary>
19+
/// 右下角X坐标
20+
/// </summary>
21+
public int LastRow { get; set; }
22+
23+
/// <summary>
24+
/// 左上角Y坐标
25+
/// </summary>
26+
public int FirstCol { get; set; }
27+
28+
/// <summary>
29+
/// 右下角Y坐标
30+
/// </summary>
31+
public int LastCol { get; set; }
32+
33+
/// <summary>
34+
/// 初始化一个<see cref="MergedRegionInfo"/>类型的实例
35+
/// </summary>
36+
/// <param name="index">索引</param>
37+
/// <param name="firstRow">左上角X坐标</param>
38+
/// <param name="lastRow">右下角X坐标</param>
39+
/// <param name="firstCol">左上角Y坐标</param>
40+
/// <param name="lastCol">右下角Y坐标</param>
41+
public MergedRegionInfo(int index, int firstRow, int lastRow, int firstCol, int lastCol)
42+
{
43+
Index = index;
44+
FirstRow = firstRow;
45+
LastRow = lastRow;
46+
FirstCol = firstCol;
47+
LastCol = lastCol;
48+
}
49+
}
50+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
namespace Bing.Offices.Metadata
2+
{
3+
/// <summary>
4+
/// 图片信息
5+
/// </summary>
6+
public class PictureInfo
7+
{
8+
/// <summary>
9+
/// 最小行索引
10+
/// </summary>
11+
public int MinRow { get; set; }
12+
13+
/// <summary>
14+
/// 最大行索引
15+
/// </summary>
16+
public int MaxRow { get; set; }
17+
18+
/// <summary>
19+
/// 最小列索引
20+
/// </summary>
21+
public int MinCol { get; set; }
22+
23+
/// <summary>
24+
/// 最大列索引
25+
/// </summary>
26+
public int MaxCol { get; set; }
27+
28+
/// <summary>
29+
/// 图片数据
30+
/// </summary>
31+
public byte[] PictureData { get; set; }
32+
33+
/// <summary>
34+
/// 图片样式
35+
/// </summary>
36+
public PictureStyle PictureStyle { get; set; }
37+
38+
/// <summary>
39+
/// 初始化一个<see cref="PictureInfo"/>类型的实例
40+
/// </summary>
41+
/// <param name="minRow">最小行索引</param>
42+
/// <param name="maxRow">最大行索引</param>
43+
/// <param name="minCol">最小列索引</param>
44+
/// <param name="maxCol">最大列索引</param>
45+
/// <param name="pictureData">图片数据</param>
46+
/// <param name="pictureStyle">图片样式</param>
47+
public PictureInfo(int minRow, int maxRow, int minCol, int maxCol, byte[] pictureData,
48+
PictureStyle pictureStyle)
49+
{
50+
MinRow = minRow;
51+
MaxRow = maxRow;
52+
MinCol = minCol;
53+
MaxCol = maxCol;
54+
PictureData = pictureData;
55+
PictureStyle = pictureStyle;
56+
}
57+
}
58+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace Bing.Offices.Metadata
2+
{
3+
/// <summary>
4+
/// 图片样式
5+
/// </summary>
6+
public class PictureStyle
7+
{
8+
public int AnchorDx1 { get; set; }
9+
public int AnchorDx2 { get; set; }
10+
public int AnchorDy1 { get; set; }
11+
public int AnchorDy2 { get; set; }
12+
public int FillColor { get; set; }
13+
public bool IsNoFill { get; set; }
14+
public int LineStyle { get; set; }
15+
public int LineStyleColor { get; set; }
16+
public double LineWidth { get; set; }
17+
}
18+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using NPOI.SS.Util;
4+
5+
namespace Bing.Offices.Npoi.Extensions
6+
{
7+
/// <summary>
8+
/// NPOI单元格(<see cref="NPOI.SS.UserModel.ICell"/>) 扩展
9+
/// </summary>
10+
public static partial class CellExtensions
11+
{
12+
#region AddConditionalFormattingRules(添加条件格式规则)
13+
14+
/// <summary>
15+
/// 添加条件格式规则
16+
/// </summary>
17+
/// <param name="cell">单元格</param>
18+
/// <param name="cfrs">条件格式规则</param>
19+
public static void AddConditionalFormattingRules(this NPOI.SS.UserModel.ICell cell, NPOI.SS.UserModel.IConditionalFormattingRule[] cfrs)
20+
{
21+
var regions = new[]
22+
{
23+
new CellRangeAddress(cell.RowIndex, cell.RowIndex, cell.ColumnIndex, cell.ColumnIndex),
24+
};
25+
cell.Sheet.SheetConditionalFormatting.AddConditionalFormatting(regions, cfrs);
26+
}
27+
28+
#endregion
29+
30+
#region GetConditionalFormattingRules(获取条件格式规则)
31+
32+
/// <summary>
33+
/// 获取条件格式规则
34+
/// </summary>
35+
/// <param name="cell">单元格</param>
36+
public static NPOI.SS.UserModel.IConditionalFormattingRule[] GetConditionalFormattingRules(this NPOI.SS.UserModel.ICell cell)
37+
{
38+
var cfrs = new List<NPOI.SS.UserModel.IConditionalFormattingRule>();
39+
var scf = cell.Sheet.SheetConditionalFormatting;
40+
for (var i = 0; i < scf.NumConditionalFormattings; i++)
41+
{
42+
var cf = scf.GetConditionalFormattingAt(i);
43+
if (cell.ExistConditionalFormatting(cf))
44+
{
45+
for (var j = 0; j < cf.NumberOfRules; j++)
46+
{
47+
cfrs.Add(cf.GetRule(j));
48+
}
49+
}
50+
}
51+
52+
return cfrs.ToArray();
53+
}
54+
55+
/// <summary>
56+
/// 判断单元格是否存在条件格式
57+
/// </summary>
58+
/// <param name="cell">单元格</param>
59+
/// <param name="cf">条件格式</param>
60+
private static bool ExistConditionalFormatting(this NPOI.SS.UserModel.ICell cell, NPOI.SS.UserModel.IConditionalFormatting cf)
61+
{
62+
return cf.GetFormattingRanges().Any(cra =>
63+
cell.RowIndex >= cra.FirstRow && cell.RowIndex <= cra.LastRow && cell.ColumnIndex >= cra.FirstColumn &&
64+
cell.ColumnIndex <= cra.LastColumn);
65+
}
66+
67+
#endregion
68+
}
69+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
using Bing.Offices.Exceptions;
3+
using Bing.Offices.Metadata;
4+
using NPOI.SS.Util;
5+
6+
namespace Bing.Offices.Npoi.Extensions
7+
{
8+
/// <summary>
9+
/// NPOI单元格(<see cref="NPOI.SS.UserModel.ICell"/>) 扩展
10+
/// </summary>
11+
public static partial class CellExtensions
12+
{
13+
/// <summary>
14+
/// 合并单元格
15+
/// </summary>
16+
/// <param name="fromCell">起始单元格</param>
17+
/// <param name="toCell">终止单元格</param>
18+
/// <param name="isExpand">扩充模式</param>
19+
public static void Merge(this NPOI.SS.UserModel.ICell fromCell, NPOI.SS.UserModel.ICell toCell,
20+
bool isExpand = false)
21+
{
22+
if (!fromCell.Sheet.Equals(toCell.Sheet))
23+
throw new OfficeException("单元格不在同一个工作表上");
24+
var sheet = fromCell.Sheet;
25+
var fromRange = fromCell.GetRangeInfo();
26+
var toRange = toCell.GetRangeInfo();
27+
var firstRowIndex = Math.Min(fromRange.FirstRow, toRange.FirstRow);
28+
var firstColIndex = Math.Min(fromRange.FirstCol, toRange.FirstCol);
29+
var lastRowIndex = Math.Max(fromRange.LastRow, toRange.LastRow);
30+
var lastColIndex = Math.Max(fromRange.LastCol, toRange.LastCol);
31+
var regionInfoList = sheet.GetMergedRegionInfos(firstRowIndex, lastRowIndex, firstColIndex, lastColIndex, false);
32+
foreach (var regionInfo in regionInfoList)
33+
{
34+
if (isExpand)
35+
{
36+
firstRowIndex = Math.Min(firstRowIndex, regionInfo.FirstRow);
37+
firstColIndex = Math.Min(firstColIndex, regionInfo.FirstCol);
38+
lastRowIndex = Math.Max(lastRowIndex, regionInfo.LastRow);
39+
lastColIndex = Math.Max(lastColIndex, regionInfo.LastCol);
40+
}
41+
sheet.RemoveMergedRegion(regionInfo.Index);
42+
}
43+
var region = new CellRangeAddress(firstRowIndex, lastRowIndex, firstColIndex, lastColIndex);
44+
fromCell.Sheet.AddMergedRegion(region);
45+
}
46+
47+
/// <summary>
48+
/// 获取合并区域信息
49+
/// </summary>
50+
/// <param name="cell">NPOI单元格</param>
51+
private static MergedRegionInfo GetRangeInfo(this NPOI.SS.UserModel.ICell cell)
52+
{
53+
var sheet = cell.Sheet;
54+
for (var i = 0; i < sheet.NumMergedRegions; i++)
55+
{
56+
var range = sheet.GetMergedRegion(i);
57+
if (range.IsInRange(cell.RowIndex, cell.ColumnIndex))
58+
return new MergedRegionInfo(i, range.FirstRow, range.LastRow, range.FirstColumn, range.LastColumn);
59+
}
60+
return new MergedRegionInfo(-1, cell.RowIndex, cell.RowIndex, cell.ColumnIndex, cell.ColumnIndex);
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)