private bool ReadSheetRow(XlsxWorksheet sheet) { if (sheet.ColumnsCount < 0) { return(false); } if (null == m_xmlReader) { return(false); } if (m_emptyRowCount != 0) { m_cellsValues = new object[sheet.ColumnsCount]; m_emptyRowCount--; m_depth++; return(true); } if (m_savedCellsValues != null) { m_cellsValues = m_savedCellsValues; m_savedCellsValues = null; m_depth++; return(true); } bool isRow = false; bool isSheetData = (m_xmlReader.NodeType == XmlNodeType.Element && m_xmlReader.LocalName == XlsxWorksheet.N_sheetData); if (isSheetData) { isRow = m_xmlReader.ReadToFollowing(XlsxWorksheet.N_row, m_namespaceUri); } else { if (m_xmlReader.LocalName == XlsxWorksheet.N_row && m_xmlReader.NodeType == XmlNodeType.EndElement) { m_xmlReader.Read(); } isRow = (m_xmlReader.NodeType == XmlNodeType.Element && m_xmlReader.LocalName == XlsxWorksheet.N_row); } if (isRow) { m_cellsValues = new object[sheet.ColumnsCount]; if (sheet.ColumnsCount > 13) { int i = sheet.ColumnsCount; } var rowIndexText = m_xmlReader.GetAttribute(XlsxWorksheet.A_r); Debug.Assert(rowIndexText != null); int rowIndex = int.Parse(rowIndexText); if (rowIndex != (m_depth + 1)) { m_emptyRowCount = rowIndex - m_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 (m_xmlReader.Read()) { if (m_xmlReader.Depth == 2) { break; } if (m_xmlReader.NodeType == XmlNodeType.Element) { hasValue = false; if (m_xmlReader.LocalName == XlsxWorksheet.N_c) { a_s = m_xmlReader.GetAttribute(XlsxWorksheet.A_s); a_t = m_xmlReader.GetAttribute(XlsxWorksheet.A_t); a_r = m_xmlReader.GetAttribute(XlsxWorksheet.A_r); XlsxDimension.XlsxDim(a_r, out col, out row); } else if (m_xmlReader.LocalName == XlsxWorksheet.N_v || m_xmlReader.LocalName == XlsxWorksheet.N_t) { hasValue = true; } else { // Ignore } } if (m_xmlReader.NodeType == XmlNodeType.Text && hasValue) { double number; object o = m_xmlReader.Value; #region Read Cell Value if (double.TryParse(o.ToString(), NumberStyles.Any, CultureInfo.InvariantCulture, out number)) { // numeric o = number; } if (null != a_t && a_t == XlsxWorksheet.A_s) { // string o = m_workbook.SST[int.Parse(o.ToString())].ConvertEscapeChars(); } else if (null != a_t && a_t == XlsxWorksheet.N_inlineStr) { // string inline o = o.ToString().ConvertEscapeChars(); } else if (a_t == "b") { // boolean o = m_xmlReader.Value == "1"; } else if (a_t == "str") { // string o = m_xmlReader.Value; } else if (null != a_s) { //something else XlsxXf xf = m_workbook.Styles.CellXfs[int.Parse(a_s)]; if (xf.ApplyNumberFormat && o != null && o.ToString() != string.Empty && IsDateTimeStyle(xf.NumFmtId)) { o = number.ConvertFromOATime(); } else if (xf.NumFmtId == 49) { o = o.ToString(); } } #endregion if (col - 1 < m_cellsValues.Length) { m_cellsValues[col - 1] = o; } } } if (m_emptyRowCount > 0) { m_savedCellsValues = m_cellsValues; return(ReadSheetRow(sheet)); } m_depth++; return(true); } else { return(false); } }
private bool ReadSheetRow(XlsxWorksheet sheet) { if (sheet.ColumnsCount < 0) { //Console.WriteLine("Columons Count Can NOT BE Negative"); return(false); } if (null == m_xmlReader) { return(false); } if (m_emptyRowCount != 0) { m_cellsValues = new object[sheet.ColumnsCount]; m_emptyRowCount--; m_depth++; return(true); } if (m_savedCellsValues != null) { m_cellsValues = m_savedCellsValues; m_savedCellsValues = null; m_depth++; return(true); } bool isRow = false; bool isSheetData = (m_xmlReader.NodeType == XmlNodeType.Element && m_xmlReader.LocalName == XlsxWorksheet.N_sheetData); if (isSheetData) { isRow = m_xmlReader.ReadToFollowing(XlsxWorksheet.N_row, m_namespaceUri); } else { if (m_xmlReader.LocalName == XlsxWorksheet.N_row && m_xmlReader.NodeType == XmlNodeType.EndElement) { m_xmlReader.Read(); } isRow = (m_xmlReader.NodeType == XmlNodeType.Element && m_xmlReader.LocalName == XlsxWorksheet.N_row); //Console.WriteLine("isRow:{0}/{1}/{2}", isRow,m_xmlReader.NodeType, m_xmlReader.LocalName); } if (isRow) { m_cellsValues = new object[sheet.ColumnsCount]; if (sheet.ColumnsCount > 13) { int i = sheet.ColumnsCount; } int rowIndex = int.Parse(m_xmlReader.GetAttribute(XlsxWorksheet.A_r)); if (rowIndex != (m_depth + 1)) { m_emptyRowCount = rowIndex - m_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 (m_xmlReader.Read()) { if (m_xmlReader.Depth == 2) { break; } if (m_xmlReader.NodeType == XmlNodeType.Element) { hasValue = false; if (m_xmlReader.LocalName == XlsxWorksheet.N_c) { a_s = m_xmlReader.GetAttribute(XlsxWorksheet.A_s); a_t = m_xmlReader.GetAttribute(XlsxWorksheet.A_t); a_r = m_xmlReader.GetAttribute(XlsxWorksheet.A_r); XlsxDimension.XlsxDim(a_r, out col, out row); } else if (m_xmlReader.LocalName == XlsxWorksheet.N_v || m_xmlReader.LocalName == XlsxWorksheet.N_t) { hasValue = true; } else { //Console.WriteLine("Error"); } } if (m_xmlReader.NodeType == XmlNodeType.Text && hasValue) { double number; object o = m_xmlReader.Value; var style = NumberStyles.Any; var culture = CultureInfo.InvariantCulture; if (double.TryParse(o.ToString(), style, culture, out number)) { o = number; } #region Read Cell Value if (null != a_t && a_t == XlsxWorksheet.A_s) //if string { o = m_workbook.SST[int.Parse(o.ToString())].ConvertEscapeChars(); } // 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 = o.ToString().ConvertEscapeChars(); } else if (a_t == "b") //boolean { o = m_xmlReader.Value == "1"; } else if (a_t == "str") { o = m_xmlReader.Value.ToString(); } else if (null != a_s) //if something else { XlsxXf xf = m_workbook.Styles.CellXfs[int.Parse(a_s)]; if (xf.ApplyNumberFormat && o != null && o.ToString() != string.Empty && IsDateTimeStyle(xf.NumFmtId)) { o = number.ConvertFromOATime(); } else if (xf.NumFmtId == 49) { o = o.ToString(); } } #endregion if (col - 1 < m_cellsValues.Length) { //Console.WriteLine(o); if (string.IsNullOrEmpty(o.ToString())) { //Console.WriteLine("Error"); } m_cellsValues[col - 1] = o; } else { //Console.WriteLine("Error"); } } else { if (m_xmlReader.LocalName == XlsxWorksheet.N_v) { //Console.WriteLine("No Value"); } } } if (m_emptyRowCount > 0) { m_savedCellsValues = m_cellsValues; return(ReadSheetRow(sheet)); } m_depth++; return(true); } else { //Console.WriteLine(m_xmlReader.LocalName.ToString()); return(false); } }
private bool ReadSheetRow(XlsxWorksheet sheet) { if (sheet.ColumnsCount < 0) { return(false); } if (null == m_xmlReader) { return(false); } if (m_emptyRowCount != 0) { m_cellsValues = new object[sheet.ColumnsCount]; m_emptyRowCount--; m_depth++; return(true); } if (m_savedCellsValues != null) { m_cellsValues = m_savedCellsValues; m_savedCellsValues = null; m_depth++; return(true); } bool isRow = false; bool isSheetData = (m_xmlReader.NodeType == XmlNodeType.Element && m_xmlReader.LocalName == XlsxWorksheet.N_sheetData); if (isSheetData) { isRow = m_xmlReader.ReadToFollowing(XlsxWorksheet.N_row, m_namespaceUri); } else { if (m_xmlReader.LocalName == XlsxWorksheet.N_row && m_xmlReader.NodeType == XmlNodeType.EndElement) { //Console.WriteLine("read"); m_xmlReader.Read(); } isRow = (m_xmlReader.NodeType == XmlNodeType.Element && m_xmlReader.LocalName == XlsxWorksheet.N_row); } if (!isRow) { return(false); } //Console.WriteLine("New Row"); m_cellsValues = new object[sheet.ColumnsCount]; if (sheet.ColumnsCount > 13) { int i = sheet.ColumnsCount; } var rowIndexText = m_xmlReader.GetAttribute(XlsxWorksheet.A_r); Debug.Assert(rowIndexText != null); int rowIndex = int.Parse(rowIndexText); if (rowIndex != (m_depth + 1)) { m_emptyRowCount = rowIndex - m_depth - 1; } bool hasValue = false; bool hasFormula = false; HyperLinkIndex hyperlinkIndex = null; string a_s = String.Empty; string a_t = String.Empty; string a_r = String.Empty; string f = String.Empty; int col = 0; int row = 0; while (m_xmlReader.Read()) { //Console.WriteLine("m_xmlReader.LocalName:{0}",m_xmlReader.LocalName); //Console.WriteLine("m_xmlReader.Value:{0}",m_xmlReader.Value); if (m_xmlReader.Depth == 2) { break; } if (m_xmlReader.NodeType == XmlNodeType.Element) { hasValue = false; if (m_xmlReader.LocalName == XlsxWorksheet.N_c) { a_s = m_xmlReader.GetAttribute(XlsxWorksheet.A_s); a_t = m_xmlReader.GetAttribute(XlsxWorksheet.A_t); a_r = m_xmlReader.GetAttribute(XlsxWorksheet.A_r); XlsxDimension.XlsxDim(a_r, out col, out row); } else if (m_xmlReader.LocalName == XlsxWorksheet.N_f) { hasFormula = true; } else if (m_xmlReader.LocalName == XlsxWorksheet.N_v || m_xmlReader.LocalName == XlsxWorksheet.N_t) { hasValue = true; hasFormula = false; } else { //Console.WriteLine("m_xmlReader.LocalName:{0}",m_xmlReader.LocalName); // Ignore } } bool hasHyperLinkFormula = false; if (m_xmlReader.NodeType == XmlNodeType.Text && hasFormula) { string formula = m_xmlReader.Value.ToString(); if (formula.StartsWith("HYPERLINK(")) { hyperlinkIndex = this.ReadHyperLinkFormula(sheet.Name, formula); } } if (m_xmlReader.NodeType == XmlNodeType.Text && hasValue) { double number; object o = m_xmlReader.Value; //Console.WriteLine("O:{0}", o); if (double.TryParse(o.ToString(), NumberStyles.Any, CultureInfo.InvariantCulture, out number)) { // numeric o = number; } if (null != a_t && a_t == XlsxWorksheet.A_s) { // string var sstStr = m_workbook.SST[int.Parse(o.ToString())]; //Console.WriteLine(sstStr); o = sstStr.ConvertEscapeChars(); } else if (null != a_t && a_t == XlsxWorksheet.N_inlineStr) { // string inline o = o.ToString().ConvertEscapeChars(); } else if (a_t == "b") { // boolean o = m_xmlReader.Value == "1"; } else if (a_t == "str") { // string o = m_xmlReader.Value; } else if (null != a_s) { //something else XlsxXf xf = m_workbook.Styles.CellXfs[int.Parse(a_s)]; if (xf.ApplyNumberFormat && o != null && o.ToString() != string.Empty && IsDateTimeStyle(xf.NumFmtId)) { o = number.ConvertFromOATime(); } else if (xf.NumFmtId == 49) { o = o.ToString(); } } //Console.WriteLine(o); if (col - 1 < m_cellsValues.Length) { if (hyperlinkIndex != null) { var co = new XlsCell(o); co.HyperLinkIndex = hyperlinkIndex; m_cellsValues[col - 1] = co; hyperlinkIndex = null; } else { m_cellsValues[col - 1] = o; } } } else { //Console.WriteLine(m_xmlReader.Value.ToString()); } } if (m_emptyRowCount > 0) { //Console.WriteLine("Again"); m_savedCellsValues = m_cellsValues; return(ReadSheetRow(sheet)); } m_depth++; return(true); }
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)) { 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); } ((IDisposable)_xmlReader).Dispose(); if (_sheetStream != null) { _sheetStream.Dispose(); } return(false); }
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.Name == XlsxWorksheet.N_row) || _xmlReader.ReadToFollowing(XlsxWorksheet.N_row)) { _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.Name == 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.Name == XlsxWorksheet.N_v) { hasValue = true; } } if (_xmlReader.NodeType == XmlNodeType.Text && hasValue) { object o = _xmlReader.Value; if (null != a_t && a_t == XlsxWorksheet.A_s) { o = _workbook.SST[Convert.ToInt32(o)]; } else if (null != a_s) { XlsxXf xf = _workbook.Styles.CellXfs[int.Parse(a_s)]; if (xf.ApplyNumberFormat && IsDateTimeStyle(xf.NumFmtId) && o != null && o.ToString() != string.Empty) { o = DateTime.FromOADate(Convert.ToDouble(o, CultureInfo.InvariantCulture)); } } if (col - 1 < _cellsValues.Length) { _cellsValues[col - 1] = o; } } } if (_emptyRowCount > 0) { _savedCellsValues = _cellsValues; return(ReadSheetRow(sheet)); } else { Depth++; } return(true); } _xmlReader.Close(); if (_sheetStream != null) { _sheetStream.Close(); } return(false); }
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.Name == XlsxWorksheet.N_row) || _xmlReader.ReadToFollowing(XlsxWorksheet.N_row)) { _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.Name == 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.Name == XlsxWorksheet.N_v) { hasValue = true; } } if (_xmlReader.NodeType == XmlNodeType.Text && hasValue) { double number; object o = _xmlReader.Value; if (double.TryParse(o.ToString(), out number)) { o = number; } if (null != a_t && a_t == XlsxWorksheet.A_s) //if string { o = Helpers.ConvertEscapeChars(_workbook.SST[int.Parse(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 (xf.ApplyNumberFormat && 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 bool ReadSheetRow(XlsxWorksheet sheet) { if (this._xmlReader == null) { return(false); } if (this._emptyRowCount != 0) { this._cellsValues = new object[sheet.ColumnsCount]; this._emptyRowCount--; this._depth++; return(true); } if (this._savedCellsValues != null) { this._cellsValues = this._savedCellsValues; this._savedCellsValues = null; this._depth++; return(true); } if (((this._xmlReader.NodeType != XmlNodeType.Element) || (this._xmlReader.Name != "row")) && !this._xmlReader.ReadToFollowing("row")) { this._xmlReader.Close(); if (this._sheetStream != null) { this._sheetStream.Close(); } return(false); } this._cellsValues = new object[sheet.ColumnsCount]; int num = int.Parse(this._xmlReader.GetAttribute("r")); if (num != (this._depth + 1)) { this._emptyRowCount = (num - this._depth) - 1; } bool flag = false; string s = string.Empty; string attribute = string.Empty; int num2 = 0; int num3 = 0; while (this._xmlReader.Read()) { if (this._xmlReader.Depth == 2) { break; } if (this._xmlReader.NodeType == XmlNodeType.Element) { flag = false; if (this._xmlReader.Name == "c") { s = this._xmlReader.GetAttribute("s"); attribute = this._xmlReader.GetAttribute("t"); XlsxDimension.XlsxDim(this._xmlReader.GetAttribute("r"), out num2, out num3); } else if (this._xmlReader.Name == "v") { flag = true; } } if ((this._xmlReader.NodeType == XmlNodeType.Text) && flag) { double num4; object obj2 = this._xmlReader.Value; if (double.TryParse(obj2.ToString(), out num4)) { obj2 = num4; } if ((attribute != null) && (attribute == "s")) { obj2 = Helpers.ConvertEscapeChars(this._workbook.SST[int.Parse(obj2.ToString())]); } else if (attribute == "b") { obj2 = this._xmlReader.Value == "1"; } else if (s != null) { XlsxXf xf = this._workbook.Styles.CellXfs[int.Parse(s)]; if ((xf.ApplyNumberFormat && (obj2 != null)) && ((obj2.ToString() != string.Empty) && this.IsDateTimeStyle(xf.NumFmtId))) { obj2 = Helpers.ConvertFromOATime(num4); } else if (xf.NumFmtId == 0x31) { obj2 = obj2.ToString(); } } if ((num2 - 1) < this._cellsValues.Length) { this._cellsValues[num2 - 1] = obj2; } } } if (this._emptyRowCount > 0) { this._savedCellsValues = this._cellsValues; return(this.ReadSheetRow(sheet)); } this._depth++; return(true); }