/// <summary> /// copy the style of old range to new range /// </summary> /// <param name="to">the new range where style will applied to</param> /// <param name="offset">the old range where style will be copied</param> /// <param name="skip">the number of rows or columns to skip</param> /// <returns>The <paramref name="ExcelRange"/> after new style applied</returns> public static ExcelRange CopyStyleFrom(this ExcelRange to, ExcelRange from, InsertMode mode = InsertMode.ColumnRight, bool withValue = false, int skip = 0) { var fromIndex = from.GetRangeIndex(); var toIndex = to.GetRangeIndex(); var offset = (mode.In(InsertMode.RowAfter, InsertMode.RowBefore) ? fromIndex[2] - fromIndex[0] : fromIndex[3] - fromIndex[1]) + 1; var range = (mode.In(InsertMode.RowAfter, InsertMode.RowBefore) ? toIndex[2] - toIndex[0] : toIndex[3] - toIndex[1]) + 1; var rest = range % offset; var loop = (range - rest) / offset; var index = 0; for (; index < loop; index++) { ExcelRange tmpRange = null; if (mode.In(InsertMode.RowAfter, InsertMode.RowBefore)) { tmpRange = to.GetRange(new[] { fromIndex[0] + (index + 1 + skip) * offset, fromIndex[1], fromIndex[2] + (index + 1 + skip) * offset, fromIndex[3] }); } else { tmpRange = to.GetRange(new[] { fromIndex[0], fromIndex[1] + (index + 1 + skip) * offset, fromIndex[2], fromIndex[3] + (index + 1 + skip) * offset }); } from.Copy(tmpRange); if (!withValue) { tmpRange.ForEach(t => t.Value = ""); } } if (rest > 0) { var restRange = to.GetRange(new[] { fromIndex[0], fromIndex[1] + index * offset, fromIndex[2], fromIndex[1] + index * offset + rest - 1 }); var x = from.Start.Address.AddressToNumber(); var y = from.End.Address.AddressToNumber(); var restFrom = from[x[0], x[1], y[0], x[1] + rest - 1]; restFrom.Copy(restRange); if (!withValue) { restRange.ForEach(t => t.Value = ""); } } return(to); }
public static void AllBorder(this ExcelRange range, ExcelBorderStyle borderStyle) { range.ForEach(r => r.Style.Border.BorderAround(borderStyle)); }