前言
Excel中提供了功能強大的公式,EPPlus同樣也支持Excel格式。
實現
// RowCount、ColumnCount 整數,分別是行計數器、列計數器
ThisCell = Worksheet.Cells[RowCount, ColumnCount];
string StartCell = Worksheet.Cells[4, ColumnCount].Address;
string EndCell = Worksheet.Cells[(RowCount - 1), ColumnCount].Address;
Formula = String.Format("=SUM({0}:{1})", StartCell, EndCell);
ThisCell.Formula = Formula;
由於Excel公式是採用A1、B1之類的字符串來定位單元格的。但是,在EPPlus通常都用正整數來定位單元格,例如Worksheet.Cells[1, 1]則表示A1,Worksheet.Cells[1, 2]則表示B1。可用Address來獲取該字符串:
string cell = Worksheet.Cells[1, 1].Address;