private static string GetCellValue(BidirectionalDictionary <string, string> sharedStrings, Cell cell) { if (cell.CellValue == null) { return(string.Empty); } if (cell.DataType == null || cell.DataType != CellValues.SharedString) { return(cell.CellValue.InnerText); } sharedStrings.TryGetKey(cell.CellValue.InnerText, out string sharedStringValue); return(sharedStringValue); }
/// <summary> /// Initializes a new instance of the <see cref="WorksheetReader{TClass, TClassMap}"/> class. /// </summary> /// <param name="worksheetName"></param> /// <param name="spreadsheetDocument"></param> /// <param name="sharedStrings"></param> /// <param name="headerRowIndex"></param> public WorksheetReader(string worksheetName, SpreadsheetDocument spreadsheetDocument, BidirectionalDictionary <string, string> sharedStrings, uint headerRowIndex = 1) { this.sharedStrings = sharedStrings; this.spreadsheetDocument = spreadsheetDocument; // reader setup var worksheetId = this.spreadsheetDocument.WorkbookPart.Workbook.Descendants <Sheet>().First(s => worksheetName.Equals(s.Name)).Id; var worksheetPart = this.spreadsheetDocument.WorkbookPart.GetPartById(worksheetId); this.reader = OpenXmlReader.Create(worksheetPart); this.reader.Read(); this.ReadHeader(headerRowIndex); // map setup var classMap = Activator.CreateInstance <TClassMap>(); this.propertyMaps = classMap.PropertyMaps.Where(x => !x.PropertyData.IgnoreRead).ToList().AsReadOnly(); }
/// <summary> /// Initializes a new instance of the <see cref="WorksheetWriter{TClass, TClassMap}"/> class. /// </summary> /// <param name="worksheetName">The worksheet's name.</param> /// <param name="worksheetPositionIndex">The position where the worksheet should be inserted in the workbook.</param> /// <param name="spreadsheetDocument">A reference to the worksheet's parent <see cref="spreadsheetDocument"/>.</param> /// <param name="worksheetStyle">The workseet's style definitions.</param> /// <param name="sharedStrings">The shared strings collection shared by all worksheets.</param> /// <param name="spreadsheetStyles">The spreadsheet stylesheet shared by all worksheets.</param> public WorksheetWriter(string worksheetName, int worksheetPositionIndex, SpreadsheetDocument spreadsheetDocument, WorksheetStyle worksheetStyle, BidirectionalDictionary <string, string> sharedStrings, StylesCollection spreadsheetStyles) { this.sharedStrings = sharedStrings; this.spreadsheetDocument = spreadsheetDocument; this.spreadsheetStyles = spreadsheetStyles; this.worksheetName = worksheetName; this.worksheetPositionIndex = worksheetPositionIndex; this.worksheetStyle = worksheetStyle; // setup worksheet this.currentRowIndex = this.worksheetStyle.HeaderRowIndex; this.worksheetPart = this.spreadsheetDocument.WorkbookPart.AddNewPart <WorksheetPart>(); // property maps this.orderedPropertyMaps = this.CreateOrderedPropertyMaps().ToList(); // worksheet setup this.CacheWorksheetData(); this.writer = OpenXmlWriter.Create(this.worksheetPart, System.Text.Encoding.UTF8); this.WriteInitialWorksheetElements(); }
/// <summary> /// Initializes a new instance of the <see cref="ReaderRow"/> class. /// </summary> /// <param name="headers">A dictionary containing header column indexes and their associated names.</param> /// <param name="rowValues">A dictionary containing row column indeces and their associated values.</param> public ReaderRow(BidirectionalDictionary <uint, string> headers, Dictionary <uint, string> rowValues) { this.headers = headers; this.rowValues = rowValues; }