private WorksheetPart GetWorkSheetPart(DefinedNameVal definedName) { //get worksheet based on defined name string relId = InMemoryDocument.WorkbookPart.Workbook.Descendants <Sheet>().First(s => definedName.SheetName.Equals(s.Name)).Id; return((WorksheetPart)InMemoryDocument.WorkbookPart.GetPartById(relId)); }
private static SheetData GetWorkSheetData(WorksheetPart worksheetPart, DefinedNameVal definedName) { //get worksheet based on defined name var result = worksheetPart.Worksheet.Descendants <SheetData>().FirstOrDefault(); return(result); }
private Row CreateContentRow(int index, DataRow dataRow, Row templateRow, DefinedNameVal namedRange) { //Create new row var r = new Row { RowIndex = (UInt32)index, Spans = templateRow.Spans, StyleIndex = templateRow.StyleIndex }; //First cell is a text cell, so create it and append it var columnIndexStart = GetColumnNumberFromColumnId(namedRange.StartColumn); var columnIndexEnd = GetColumnNumberFromColumnId(namedRange.EndColumn); //NOTE: For the purpose of this sample, it is assumed that the named range is no more than a single row. for (var columnIndex = columnIndexStart; columnIndex <= columnIndexEnd; columnIndex++) { Cell cell; var columnId = GetColumnAlpha(columnIndex); var templateCell = templateRow.Descendants <Cell>().ElementAtOrDefault(columnIndex - 1); var templateCellValue = GetValue(templateCell); if (templateCellValue.StartsWith("[") && templateCellValue.EndsWith("]")) { var templateCellFieldName = templateCellValue.Substring(1, templateCellValue.Length - 2); var dataValue = dataRow[templateCellFieldName]; if (dataValue != null) { cell = CreateCellFromTemplate(columnId, index, templateCell, dataValue); } else { //the named field was not found in the dataRow, so just use the templateCellValue for the new cell. cell = CreateCellFromTemplate(columnId, index, templateCell, templateCellValue); } } else { //cell doesn not contain a field name, so just use the templateCellValue for the new cell. cell = CreateCellFromTemplate(columnId, index, templateCell, templateCellValue); } r.AppendChild(cell); } return(r); }