public void Insert(CellRangeTemplate top, double?rowHeight = null) { InsertDatas(top, new List <object>() { new { } }, rowHeight); }
public void InsertData <T>(CellRangeTemplate top, T data, double?rowHeight = null) { InsertDatas(top, new List <object>() { data }, rowHeight); }
public CellRangeTemplate CreateTemplate(string DefinedName) { var range = new CellRangeTemplate() { CellRange = GetDefinedName(DefinedName), }; range.Init(document); return(range); }
public void InsertDatas <T>(CellRangeTemplate top, IEnumerable <T> datas, double?rowHeight = null) { datas = datas ?? new List <T>(); for (int i = 0; i < datas.Count(); i++) { var data = datas.ElementAt(i); top.FillData(data); InsertTemplate(top, rowHeight); } }
private void InsertTemplate(CellRangeTemplate range, double?rowHeight = null) { CopyRange(ref range, currentSheetData, currentWorksheet, ref currentPosition, rowHeight); if (direction == DirectionType.TOP_TO_DOWN) { currentPosition.Row = currentPosition.Row + range.CellRange.Height + 1; } else { currentPosition.Column = currentPosition.Column + range.CellRange.Width + 1; } }
private void CopyRange(ref CellRangeTemplate sourceRange, SheetData sheetData, Worksheet worksheet, ref CellPosition target, double?rowHeight = null) { #region Sheet sheetTemplate = sourceRange.CellRange.SheetTemplate; var workbookPartTemplate = sourceRange.CellRange.WorksheetPart; MergeCells mergeCellsTemplate = sourceRange.CellRange.MergeCells; MergeCells mergeCells = worksheet.GetFirstChild <MergeCells>(); if (false && workbookPartTemplate.DrawingsPart != null && worksheet.WorksheetPart.DrawingsPart == null) { var drawingsPart = worksheet.WorksheetPart.AddPart <DrawingsPart>(workbookPartTemplate.DrawingsPart); drawingsPart = worksheet.WorksheetPart.DrawingsPart; if (!worksheet.WorksheetPart.Worksheet.ChildElements.OfType <Drawing>().Any()) { worksheet.WorksheetPart.Worksheet.Append(new Drawing { Id = worksheet.WorksheetPart.GetIdOfPart(drawingsPart) }); } } Dictionary <string, MergeCell> mergeCellTemplateDic = sourceRange.CellRange.MergeCellsDic; #endregion CellPosition source = sourceRange.CellRange.Start; CellPosition offset = target.CalculateOffset(source); var cellsToCopy = document.FindCellsByRange(sourceRange.CellRange); for (int i = 0; i < cellsToCopy.Count(); i++) { var rowGroup = cellsToCopy.ElementAt(i); Row keyRow = rowGroup.Key; Row targetRow = new Row() { RowIndex = (UInt32)(keyRow.RowIndex + offset.Row), Height = (short)-1, }; if (rowHeight != null) { targetRow.Height = rowHeight; targetRow.CustomHeight = true; } MoveCurrentRow((int)targetRow.RowIndex.Value); sheetData.InsertBefore(targetRow, currentRow); foreach (Cell cellToCopy in rowGroup) { Cell targetCell = (Cell)cellToCopy.Clone(); targetCell.CellReference = CellPosition.OffsetIt(targetCell.CellReference, offset); targetRow.Append(targetCell); MergeCell _findMerge; if (mergeCellTemplateDic != null && mergeCellTemplateDic.TryGetValue(cellToCopy.CellReference.Value.ToUpper(), out _findMerge)) { var positionParent = _findMerge.Reference.Value.Split(':'); CellPosition offsetStart = new CellPosition(positionParent[0]); CellPosition offsetEnd = new CellPosition(positionParent[1]); var celRefNew = new CellPosition(targetCell.CellReference); if (mergeCells == null) { var a = new MergeCells(); worksheet.InsertAfter(a, sheetData); mergeCells = worksheet.GetFirstChild <MergeCells>(); } var mergeCell = new MergeCell(); mergeCell.Reference = celRefNew.ToString() + ":" + new CellPosition(celRefNew.Row + (offsetEnd.Row - offsetStart.Row), celRefNew.Column + (offsetEnd.Column - offsetStart.Column)).ToString(); mergeCells.Append(mergeCell); mergeCells.Count = (mergeCells.Count ?? 0) + 1; } } } }