开源 .NET 电子表格控件
概述
Griddy.Spreadsheet 是一款开源高效的 .NET 电子表格控件,类似于 Excel,reogrid。
它提供了丰富的功能,如单元格合并、边框样式、图案背景颜色、数据格式、formula engine,condition format ,表格事件等。
支持 Winform。
项目地址
https://github.com/griddy-software/Griddy.Spreadsheet
功能特点
- 公式,函数。高效可扩展的计算引擎。
- 样式支持。可以设置边框,填充,数据格式,字体,对齐方式等
- 条件格式。可以通过公式的方式动态的设置单元格样式。
- 合并单元格。
- 支持markdown等单元格类型。
- 行列数支持同excel.最大行数1048576,最大列数16384
- Shape,Chart(开发中)
快速开始
IWorksheet worksheet;
worksheet.Settings.HorizontalGridLine = false;
worksheet.Settings.VerticalGridLine = false;
IRange A1 = worksheet.Cells["A1"];
//设置字体
A1.Value = "Hello World";
A1.Font.Bold = true;
A1.Font.Size = 16;
A1.Font.Color = Color.Red;
A1.Font.Underline = true;
A1.Font.Italic = true;
//设置边框
A1.Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThick;
A1.Borders[XlBordersIndex.xlEdgeBottom].LineStyle = X1LineStyle.xlDash;
A1.Borders[XlBordersIndex.xlEdgeBottom].Color = Color.Blue;
//设置填充
A1.Interior.Color = Color.Yellow;
A1.Interior.Pattern = XlPattern.xlPatternSolid;
A1.Interior.PatternColor = Color.Green;
A1.Interior.PatternTintAndShade = 0.5f;
A1.AutoFit();
公式
通过IRange.Formula可以设置或读取单元格的公式。
目前已经支持SUM,AVERAGE,ABS,MOD,SUMIFS,AND,NOT,OR等函数
IRange B1 = worksheet.Cells["B1"];
B1.Formula = "=1+2";
IRange D1 = worksheet.Cells["D1"];
D1.Formula = "=SUM(1,2,3,4,5)";
IRange D2 = worksheet.Cells["D2"];
D2.Formula = "=AVERAGE(1,2,3,4,5)";
当然你也可以通过扩展实现自己的函数,方法如下
- 实现函数。继承Function,并在ExecuteInternal方法里实现函数逻辑。
- FunctionRegistry 注册函数
//实现函数
internal class MyFunction : Function
{
public override string Name => "My";
protected override object? ExecuteInternal(CalcContext calContext, object[]? arguments)
{
return "I'm Griddy.Spreadsheet!";
}
}
//注册函数
FunctionManager.RegisterFunction(new MyFunction());
边框
通过IRange.Borders 可以设置单元格或区域边框。
设置行列边框,高效不卡顿。
//所有列
IRange columns = worksheet.Columns;
columns.Borders[XlBordersIndex.xlInsideVertical].LineStyle = X1LineStyle.xlContinuous;
columns.Borders[XlBordersIndex.xlInsideVertical].Color = Color.Red;
//所有行
IRange rows = worksheet.Rows;
rows.Borders[XlBordersIndex.xlInsideHorizontal].LineStyle = X1LineStyle.xlDashDot;
rows.Borders[XlBordersIndex.xlInsideHorizontal].Color = Color.Blue;
显示效果

填充
通过IRange.Interior设置单元格或区域的填充样色,填充样式
设置Worksheet,Row,Column,Cell 填充样式,瞬间响应,无延迟。
//设置A1单元格的填充
IRange A1 = worksheet.Cells["A1"];
A1.Interior.Color = Color.LightBlue;
A1.Interior.PatternColor = Color.Blue;
A1.Interior.Pattern = XlPattern.xlPatternCrissCross;
A1.Value = "CrissCross";
//设置整行填充
IRange row1 = worksheet.Rows[1];
row1.Interior.Pattern = XlPattern.xlPatternCrissCross;
row1.Interior.PatternColor = Color.Red;

字体
通过IRange.Font,设置单元格或区域字体。
//设置A1单元格字体
IRange A1 = worksheet.Cells["A1"];
A1.Font.Color = Color.Red;
A1.Font.Bold = true;
A1.Font.Italic = true;
A1.Font.Underline = true;
A1.Font.Size = 20;
A1.Font.Name = "华文行楷";
A1.Font.StrikeThrough = true;
A1.Value = "海阔天空";
//根据单元格内容,自动调整行高,列宽。
//需要Griddy.Spreadsheet.UI模块支持
A1.AutoFit();
//设置第一列的字体颜色为红色
IRange columnA = worksheet.Columns["A"];
columnA.Font.Color = Color.Red;
条件格式
通过IRange.FormatConditions 来设置条件格式
//值在1到5之间的单元格,字体设置成 红色,粗体,20字号
IFormatCondition formatCondition = formatConditions.Add(XlFormatConditionType.xlCellValue, XlFormatConditionOperator.xlBetween, "1","5");
formatCondition.Font.Color = Color.Red;
formatCondition.Font.Bold = true;
formatCondition.Font.Size = 20;
事件
//行高更改事件
//RowHeightChanged
//列宽更改事件
//ColumnWidthChanged
//worksheet放大缩小事件
//ZoomChanged
worksheet.RowHeightChanged += (s, e) =>
{
IRange ax = worksheet.Cells[e.StartRow, 1];
ax.Value = $"Row {e.StartRow} To {e.EndRow} height changed to {e.Height}";
};
Sample 工程
Griddy.Spreadsheet.UI.Sample 工程里集成了更多的案例,可以直接运行看看效果。

浙公网安备 33010602011771号