private void ReadWorkbookRels(Stream xmlFileStream) { using (XmlReader reader = XmlReader.Create(xmlFileStream)) { while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.LocalName == N_rel) { string rid = reader.GetAttribute(A_id); for (int i = 0; i < sheets.Count; i++) { XlsxWorksheet tempSheet = sheets[i]; if (tempSheet.RID == rid) { tempSheet.Path = reader.GetAttribute(A_target); sheets[i] = tempSheet; break; } } } } xmlFileStream.Dispose(); } }
private bool ReadSheetRow(XlsxWorksheet sheet) { if (null == _xmlReader) { return(false); } if (_emptyRowCount != 0) { _cellsValues = new object[sheet.ColumnsCount]; _emptyRowCount--; _depth++; return(true); } if (_savedCellsValues != null) { _cellsValues = _savedCellsValues; _savedCellsValues = null; _depth++; return(true); } if ((_xmlReader.NodeType == XmlNodeType.Element && _xmlReader.LocalName == XlsxWorksheet.N_row) || _xmlReader.ReadToFollowing(XlsxWorksheet.N_row, _namespaceUri)) { _cellsValues = new object[sheet.ColumnsCount]; int rowIndex = int.Parse(_xmlReader.GetAttribute(XlsxWorksheet.A_r)); if (rowIndex != (_depth + 1)) { _emptyRowCount = rowIndex - _depth - 1; } bool hasValue = false; string a_s = String.Empty; string a_t = String.Empty; string a_r = String.Empty; int col = 0; int row = 0; while (_xmlReader.Read()) { if (_xmlReader.Depth == 2) { break; } if (_xmlReader.NodeType == XmlNodeType.Element) { hasValue = false; if (_xmlReader.LocalName == XlsxWorksheet.N_c) { a_s = _xmlReader.GetAttribute(XlsxWorksheet.A_s); a_t = _xmlReader.GetAttribute(XlsxWorksheet.A_t); a_r = _xmlReader.GetAttribute(XlsxWorksheet.A_r); XlsxDimension.XlsxDim(a_r, out col, out row); } else if (_xmlReader.LocalName == XlsxWorksheet.N_v || _xmlReader.LocalName == XlsxWorksheet.N_t) { hasValue = true; } } if (_xmlReader.NodeType == XmlNodeType.Text && hasValue) { double number; object o = _xmlReader.Value; var style = NumberStyles.Any; var culture = CultureInfo.InvariantCulture; if (double.TryParse(o.ToString(), style, culture, out number)) { o = number; } if (null != a_t && a_t == XlsxWorksheet.A_s) //if string { o = Helpers.ConvertEscapeChars(_workbook.SST[int.Parse(o.ToString())]); } // Requested change 4: missing (it appears that if should be else if) else if (null != a_t && a_t == XlsxWorksheet.N_inlineStr) //if string inline { o = Helpers.ConvertEscapeChars(o.ToString()); } else if (a_t == "b") //boolean { o = _xmlReader.Value == "1"; } else if (null != a_s) //if something else { XlsxXf xf = _workbook.Styles.CellXfs[int.Parse(a_s)]; if (o != null && o.ToString() != string.Empty && IsDateTimeStyle(xf.NumFmtId)) { o = Helpers.ConvertFromOATime(number); } else if (xf.NumFmtId == 49) { o = o.ToString(); } } if (col - 1 < _cellsValues.Length) { _cellsValues[col - 1] = o; } } } if (_emptyRowCount > 0) { _savedCellsValues = _cellsValues; return(ReadSheetRow(sheet)); } _depth++; return(true); } _xmlReader.Close(); if (_sheetStream != null) { _sheetStream.Close(); } return(false); }
private void ReadSheetGlobals(XlsxWorksheet sheet) { if (_xmlReader != null) { _xmlReader.Close(); } if (_sheetStream != null) { _sheetStream.Close(); } string sheetPath = sheet.Path; if (sheetPath.StartsWith("/xl/")) { sheetPath = sheetPath.Substring(4); } _sheetStream = mZipExtractor.GetStream(XL + sheetPath); if (null == _sheetStream) { return; } _xmlReader = XmlReader.Create(_sheetStream); //count rows and cols in case there is no dimension elements int rows = 0; int cols = 0; _namespaceUri = null; int biggestColumn = 0; //used when no col elements and no dimension while (_xmlReader.Read()) { if (_xmlReader.NodeType == XmlNodeType.Element && _xmlReader.LocalName == XlsxWorksheet.N_worksheet) { //grab the namespaceuri from the worksheet element _namespaceUri = _xmlReader.NamespaceURI; } if (_xmlReader.NodeType == XmlNodeType.Element && _xmlReader.LocalName == XlsxWorksheet.N_dimension) { string dimValue = _xmlReader.GetAttribute(XlsxWorksheet.A_ref); sheet.Dimension = new XlsxDimension(dimValue); break; } //removed: Do not use col to work out number of columns as this is really for defining formatting, so may not contain all columns //if (_xmlReader.NodeType == XmlNodeType.Element && _xmlReader.LocalName == XlsxWorksheet.N_col) // cols++; if (_xmlReader.NodeType == XmlNodeType.Element && _xmlReader.LocalName == XlsxWorksheet.N_row) { rows++; } //check cells so we can find size of sheet if can't work it out from dimension or col elements (dimension should have been set before the cells if it was available) //ditto for cols if (sheet.Dimension == null && cols == 0 && _xmlReader.NodeType == XmlNodeType.Element && _xmlReader.LocalName == XlsxWorksheet.N_c) { var refAttribute = _xmlReader.GetAttribute(XlsxWorksheet.A_r); if (refAttribute != null) { var thisRef = ReferenceHelper.ReferenceToColumnAndRow(refAttribute); if (thisRef[1] > biggestColumn) { biggestColumn = thisRef[1]; } } } } //if we didn't get a dimension element then use the calculated rows/cols to create it if (sheet.Dimension == null) { if (cols == 0) { cols = biggestColumn; } if (rows == 0 || cols == 0) { sheet.IsEmpty = true; return; } sheet.Dimension = new XlsxDimension(rows, cols); //we need to reset our position to sheet data _xmlReader.Close(); _sheetStream.Close(); _sheetStream = mZipExtractor.GetStream(XL + sheetPath); _xmlReader = XmlReader.Create(_sheetStream); } //read up to the sheetData element. if this element is empty then there aren't any rows and we need to null out dimension _xmlReader.ReadToFollowing(XlsxWorksheet.N_sheetData, _namespaceUri); if (_xmlReader.IsEmptyElement) { sheet.IsEmpty = true; } }