// Copy all cells in the specified range to a new location public static void CopyCellRange(SpreadsheetDocument document, WorksheetPart worksheet, int startRow, int startColumn, int endRow, int endColumn, int toRow, int toColumn) { int rowOffset = toRow - startRow; int columnOffset = toColumn - startColumn; XDocument worksheetXDocument = worksheet.GetXDocument(); for (int row = startRow; row <= endRow; row++) for (int column = startColumn; column <= endColumn; column++) { XElement oldCell = GetCell(worksheetXDocument, column, row); if (oldCell != null) { XElement newCell = new XElement(oldCell); newCell.SetAttributeValue(NoNamespace.r, GetColumnId(column + columnOffset) + (row + rowOffset).ToString()); XElement formula = newCell.Element(S.f); if (formula != null) { ParseFormula parser = new ParseFormula(formula.Value); formula.SetValue(parser.ReplaceRelativeCell(rowOffset, columnOffset)); } SetCell(worksheetXDocument, newCell); } } worksheet.PutXDocument(); ForceCalculateOnLoad(document); }
// Creates a new worksheet with the specified name and contents from a memory spreadsheet public static void SetSheetContents(SpreadsheetDocument document, WorksheetPart worksheet, MemorySpreadsheet contents) { XDocument worksheetXDocument = worksheet.GetXDocument(); worksheetXDocument.Root.Element(S.sheetData).ReplaceWith(contents.GetElements()); worksheet.PutXDocument(); }