internal Table(WorkSheet workSheet, TableDescription tbDes) { this._WorkSheet = workSheet; this.Name = tbDes.TableName; this.Address = Range.Parse(workSheet, tbDes.BeginAddress.ColumnIndex, tbDes.BeginAddress.RowIndex, tbDes.BeginAddress.ColumnIndex, tbDes.BeginAddress.RowIndex); this.Columns.InitColumns(tbDes.AllColumns); }
/// <summary> /// 填充数据后同步表格的属性 /// </summary> /// <param name="dv"></param> private void SyncTablePropertiesAfterFillData(DataView dv) { Range address = this.Address; this.Address = Range.Parse(address.StartColumn, address.StartRow, address.EndColumn, address.StartRow + dv.Count); this.SyncDataValidationRange(); }
public void InserRows(int rowIndex, int count, int copyStyleIndex) { this.Rows.Insert(rowIndex, count); if (copyStyleIndex != 0) { if (this.Rows.ContainsKey(copyStyleIndex)) { Row currentRow = null; Cell currentCell = null; Row copyRow = this.Rows[copyStyleIndex]; List <Cell> copyRowList = new List <Cell>(); List <int> formulaList = new List <int>(); for (int col = this.Dimension.StartColumn; col <= this.Dimension.EndColumn; col++) { copyRowList.Add(this.Cells[copyStyleIndex, col]); } int currentRowIndex; for (int i = 1; i <= count; i++) { currentRowIndex = rowIndex + i; currentRow = this.Rows[currentRowIndex]; copyRow.CopyTo(currentRow); foreach (Cell cell in copyRowList) { //if (cell.IsMerge) //todo: 设置合并单元格 if (cell.Formula.IsNotEmpty() && !formulaList.Contains(cell.Column.Index)) { formulaList.Add(cell.Column.Index); if (this._SharedFormulas.ContainsKey(cell.Formula)) { this._SharedFormulas[cell.Formula].Address = Range.Parse(this, cell.Column.Index, currentRowIndex - 1, cell.Column.Index, currentRowIndex + count - 1); } else { this.SetFormulas(Range.Parse(this, cell.Column.Index, currentRowIndex - 1, cell.Column.Index, currentRowIndex + count - 1), cell); } } else if (cell.Formula.IsNullOrEmpty()) { currentCell = new Cell() { Column = cell.Column, Row = currentRow, Style = cell.Style, _Comment = cell._Comment, DataType = cell.DataType }; this.Cells.Add(currentCell); } } } } } this.ChangTables(rowIndex, count); }
public DefinedName Add(string Address, string name, bool IsHidden = false) { DefinedName nameRang = new DefinedName(name, this._Worksheet) { Address = Range.Parse(this._Worksheet, Address), IsNameHidden = IsHidden }; base.Add(nameRang); return(nameRang); }
/// <summary> /// 同步校验规则的范围 /// </summary> private void SyncDataValidationRange() { foreach (IDataValidation validation in this._WorkSheet.Validations) { if (validation.Address.IsSubset(this.Address)) { if (validation.Address.EndRow < this.Address.EndRow) { validation.Address = Range.Parse(validation.Address.StartColumn, this.Address.StartRow + 1, validation.Address.EndColumn, this.Address.EndRow); } } } }
/// <summary> /// 批量设置公式 /// </summary> /// <param name="rangeAddress">Range地址</param> /// <param name="formulaValue">公式</param> public void SetFormulas(int startRow, int startColumn, int endRow, int endColumn, string formulaValue) { formulaValue.IsNullOrEmpty().TrueThrow("公式不能为空!{0}", formulaValue); Range setAddress = Range.Parse(this, startColumn, startRow, endColumn, endRow); if (setAddress.StartColumn == setAddress.EndColumn && setAddress.StartRow == setAddress.EndRow) { this.Cells[setAddress.StartRow, setAddress.StartColumn].Formula = formulaValue; } else { this.SetFormulas(setAddress, formulaValue); } }
private void ChangTables(int beginRow, int rowCount) { foreach (Table table in this.Tables) { if (beginRow <= table.Address.StartRow) { table.Address = Range.Parse(this, table.Address.StartColumn, table.Address.StartRow + rowCount, table.Address.EndColumn, table.Address.EndRow + rowCount); } else if (beginRow > table.Address.StartRow && beginRow <= table.Address.EndRow) { table.Address = Range.Parse(this, table.Address.StartColumn, table.Address.StartRow, table.Address.EndColumn, table.Address.EndRow + rowCount); } } }
/// <summary> /// 设置合并单元格 /// </summary> /// <param name="rangeAddress"></param> public void SetMerge(string rangeAddress) { Range setAddress = Range.Parse(this, rangeAddress); ExceptionHelper.TrueThrow(setAddress.StartColumn == setAddress.EndColumn && setAddress.StartRow == setAddress.EndRow, "已是最小单元格,不能设置合并单元格!"); for (int i = setAddress.StartColumn; i <= setAddress.EndColumn; i++) { for (int j = setAddress.StartRow; j <= setAddress.EndRow; j++) { this.Cells[j, i].IsMerge = true; } } this._MergeCells.Add(Range.Parse(this, rangeAddress)); }
public void Remove(string key) { int colIndex = this.FindIndexByID(key); if (colIndex != int.MinValue) { TableColumn item = this[key]; this._Table.Rows.Remove(item); for (int i = colIndex + 1; i < this.Count; i++) { item = this._TableColumns[i]; this._TableColumnNames[item.Name] -= 1; } this._Table.Address = Range.Parse(this._Table.Address.StartColumn, this._Table.Address.StartRow, this._Table.Address.EndColumn - 1, this._Table.Address.EndRow); this._TableColumnNames.Remove(key); this._TableColumns.RemoveAt(colIndex); } }
/// <summary> /// 初始化Table集合 /// </summary> internal void InitTableAddress() { ExceptionHelper.TrueThrow(this.Address.StartColumn < 0 && this.Address.StartRow < 0, "Excel Table地址不符合标准"); if (this.Columns.Count == 0 && this.Address.EndRow > this.Address.StartRow && this.Address.EndColumn >= this.Address.StartColumn) { this.InitTableColumns(); } else { if (this.OldRange.Equals(this.Address) == false) { int endRow = this.Address.StartRow + this.Rows.Count; if (this.Rows.Count == 0) { endRow++; } this.Address = Range.Parse(this._WorkSheet, this.Address.StartColumn, this.Address.StartRow, this.Address.StartColumn + this.Columns.Count - 1, endRow); } } }
/// <summary> /// 打印区域,传入空将删除原有的 /// </summary> public void PrintArea(string rangeAddress) { string key = "_xlnm.Print_Area"; if (this._WorkSheet.Names.ContainsKey(key)) { if (rangeAddress.IsNotEmpty()) { this._WorkSheet.Names[key].Address = Range.Parse(this._WorkSheet, rangeAddress); } else { this._WorkSheet.Names.Remove(key); } } else { if (rangeAddress.IsNotEmpty()) { this._WorkSheet.Names.Add(rangeAddress, key); } } }
/// <summary> /// 创建一个新的Table /// </summary> /// <param name="name">名称</param> /// <param name="rangeAddress">Table开始到结束(例如:B2:D8)</param> /// <returns></returns> public Table Add(string name, string rangeAddress) { Table table = new Table(this._WorkSheet, name, Range.Parse(rangeAddress)); return(this.Add(table)); }
public Table(WorkSheet workSheet, string name, CellAddress beginAddress) : this(workSheet, name, Range.Parse(workSheet, beginAddress.ColumnIndex, beginAddress.RowIndex, beginAddress.ColumnIndex, beginAddress.RowIndex)) { }