コード例 #1
0
        private HyperLinkIndex ReadHyperLinkFormula(string thisSheetName, string formula)
        {
            var sb = new StringBuilder();
            var f  = formula.Substring(10);

            //HYPERLINK(#REF!,RIGHT(#REF!,3))
            //HYPERLINK(#REF!,SUBSTITUTE(#REF!,"https://coding.net/u/",""))
            //Console.WriteLine(formula);
            if (formula.StartsWith("HYPERLINK(#REF!"))
            {
                //var begin = formula.IndexOf("\"");
                //var rest = formula.Substring(begin);
                //var end = rest.IndexOf("\"");
                //var h = rest.Substring(0,end);
                //Console.WriteLine(h);
                return(null);
            }

            for (var i = 0; i < f.Length; i++)
            {
                var c = f[i];

                if (c == ',')
                {
                    var link = sb.ToString();
                    var pos  = link.IndexOf("!");

                    var sheetName = "";
                    int col       = 0;
                    int row       = 0;
                    if (pos >= 0)
                    {
                        Console.WriteLine(pos);
                        Console.WriteLine(link);
                        sheetName = link.Substring(0, pos);
                        var cellName = link.Substring(pos + 1);
                        XlsxDimension.XlsxDim(cellName, out col, out row);
                    }
                    else
                    {
                        sheetName = thisSheetName;
                        var cellName = link.ToString();
                        //Console.WriteLine(cellName);
                        XlsxDimension.XlsxDim(cellName, out col, out row);
                    }

                    return(new HyperLinkIndex()
                    {
                        Sheet = sheetName,
                        Col = col,
                        Row = row
                    });
                }

                sb.Append(c);
            }
            return(null);
        }
コード例 #2
0
        private HyperLinkIndex ReadHyperLinkFormula(string thisSheetName, string formula)
        {
            var sb = new StringBuilder();
            var f  = formula.Substring(10);

            for (var i = 0; i < f.Length; i++)
            {
                var c = f[i];

                if (c == ',')
                {
                    var link = sb.ToString();
                    var pos  = link.IndexOf("!");

                    var sheetName = "";
                    int col       = 0;
                    int row       = 0;
                    if (pos >= 0)
                    {
                        sheetName = link.Substring(0, pos);
                        var cellName = link.Substring(pos + 1);
                        XlsxDimension.XlsxDim(cellName, out col, out row);
                    }
                    else
                    {
                        sheetName = thisSheetName;
                        var cellName = link.ToString();
                        //Console.WriteLine(cellName);
                        XlsxDimension.XlsxDim(cellName, out col, out row);
                    }

                    return(new HyperLinkIndex()
                    {
                        Sheet = sheetName,
                        Col = col,
                        Row = row
                    });
                }

                sb.Append(c);
            }
            return(null);
        }
コード例 #3
0
        private Dictionary <int, XlsxDimension> DetectDemension()
        {
            var dict = new Dictionary <int, XlsxDimension>();

            for (int sheetIndex = 0; sheetIndex < m_workbook.Sheets.Count; sheetIndex++)
            {
                var sheet = m_workbook.Sheets[sheetIndex];

                ReadSheetGlobals(sheet);

                if (sheet.Dimension != null)
                {
                    m_depth         = 0;
                    m_emptyRowCount = 0;

                    // 检测100行
                    int detectRows     = Math.Min(sheet.Dimension.LastRow, 100);
                    int maxColumnCount = 0;
                    while (detectRows > 0)
                    {
                        ReadSheetRow(sheet);
                        maxColumnCount = Math.Max(LastIndexOfNonNull(m_cellsValues) + 1, maxColumnCount);
                        detectRows--;
                    }

                    // 如果实际检测出来的列个数小于元数据里的列数,
                    if (maxColumnCount < sheet.Dimension.LastCol)
                    {
                        dict[sheetIndex] = new XlsxDimension(sheet.Dimension.LastRow, maxColumnCount);
                    }
                    else
                    {
                        dict[sheetIndex] = sheet.Dimension;
                    }
                }
                else
                {
                    dict[sheetIndex] = sheet.Dimension;
                }
            }
            return(dict);
        }
コード例 #4
0
        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];

                var rowIndex = int.Parse(_xmlReader.GetAttribute(XlsxWorksheet.A_r));
                if (rowIndex != (_depth + 1))
                {
                    _emptyRowCount = rowIndex - _depth - 1;
                }
                var hasValue = false;
                var a_s      = String.Empty;
                var a_t      = String.Empty;
                var a_r      = String.Empty;
                var col      = 0;
                var 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)
                        {
                            var xf = _workbook.Styles.CellXfs[int.Parse(a_s)];

                            if (xf.ApplyNumberFormat && IsDateTimeStyle(xf.NumFmtId) && o != null && o.ToString() != string.Empty)
                            {
                                try {
                                    o = DateTime.FromOADate(Convert.ToDouble(o, CultureInfo.InvariantCulture));
                                } catch {
                                    // just don't try and convert it then
                                }
                            }
                        }

                        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);
        }
コード例 #5
0
        private bool ReadHyperLinks(XlsxWorksheet sheet, DataTable table)
        {
            // ReadTo HyperLinks Node
            if (m_xmlReader == null)
            {
                //Console.WriteLine("m_xmlReader is null");
                return(false);
            }

            //Console.WriteLine(m_xmlReader.Depth.ToString());

            m_xmlReader.ReadToFollowing(XlsxWorksheet.N_hyperlinks);
            if (m_xmlReader.IsEmptyElement)
            {
                //Console.WriteLine("not find hyperlink");
                return(false);
            }

            // Read Realtionship Table
            //Console.WriteLine("sheetrel:{0}", sheet.Path);
            var sheetRelStream = m_zipWorker.GetWorksheetRelsStream(sheet.Path);
            var hyperDict      = new Dictionary <string, string>();

            if (sheetRelStream != null)
            {
                using (var reader = XmlReader.Create(sheetRelStream))
                {
                    while (reader.Read())
                    {
                        if (reader.NodeType == XmlNodeType.Element && reader.LocalName == XlsxWorkbook.N_rel)
                        {
                            string rid = reader.GetAttribute(XlsxWorkbook.A_id);
                            hyperDict[rid] = reader.GetAttribute(XlsxWorkbook.A_target);
                        }
                    }
                    sheetRelStream.Close();
                }
            }


            // Read All HyperLink Node
            while (m_xmlReader.Read())
            {
                if (m_xmlReader.NodeType != XmlNodeType.Element)
                {
                    break;
                }
                if (m_xmlReader.LocalName != XlsxWorksheet.N_hyperlink)
                {
                    break;
                }
                string aref    = m_xmlReader.GetAttribute(XlsxWorksheet.A_ref);
                string display = m_xmlReader.GetAttribute(XlsxWorksheet.A_display);
                string rid     = m_xmlReader.GetAttribute(XlsxWorksheet.A_rid);
                ////Console.WriteLine("{0}:{1}", aref.Substring(1), display);

                //Console.WriteLine("hyperlink:{0}",hyperDict[rid]);
                var hyperlink = display;
                if (hyperDict.ContainsKey(rid))
                {
                    hyperlink = hyperDict[rid];
                }

                int col = -1;
                int row = -1;
                XlsxDimension.XlsxDim(aref, out col, out row);
                //Console.WriteLine("{0}:[{1},{2}]",aref, row, col);
                if (col >= 1 && row >= 1)
                {
                    row = row - 1;
                    col = col - 1;
                    if (row == 0 && m_isFirstRowAsColumnNames)
                    {
                        // TODO(fanfeilong):
                        var     value = table.Columns[col].ColumnName;
                        XlsCell cell  = new XlsCell(value);
                        cell.SetHyperLink(hyperlink);
                        table.Columns[col].DefaultValue = cell;
                    }
                    else
                    {
                        var value = table.Rows[row][col];
                        var cell  = new XlsCell(value);
                        cell.SetHyperLink(hyperlink);
                        //Console.WriteLine(cell.MarkDownText);
                        table.Rows[row][col] = cell;
                    }
                }
            }

            // Close
            m_xmlReader.Close();
            if (m_sheetStream != null)
            {
                m_sheetStream.Close();
            }

            return(true);
        }
コード例 #6
0
        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);
            }
        }
コード例 #7
0
ファイル: ExcelOpenXmlReader.cs プロジェクト: yichudu/exceltk
        private bool ReadHyperLinks(XlsxWorksheet sheet, DataTable table)
        {
            // ReadTo HyperLinks Node
            if (m_xmlReader == null)
            {
                return(false);
            }

            m_xmlReader.ReadToFollowing(XlsxWorksheet.N_hyperlinks);
            if (m_xmlReader.IsEmptyElement)
            {
                return(false);
            }

            // Read Realtionship Table
            Stream sheetRelStream = m_zipWorker.GetWorksheetRelsStream(sheet.Path);
            var    hyperDict      = new Dictionary <string, string>();

            if (sheetRelStream != null)
            {
                using (XmlReader reader = XmlReader.Create(sheetRelStream)) {
                    while (reader.Read())
                    {
                        if (reader.NodeType == XmlNodeType.Element && reader.LocalName == XlsxWorkbook.N_rel)
                        {
                            string rid = reader.GetAttribute(XlsxWorkbook.A_id);
                            Debug.Assert(rid != null);
                            hyperDict[rid] = reader.GetAttribute(XlsxWorkbook.A_target);
                        }
                    }
                    sheetRelStream.Close();
                }
            }


            // Read All HyperLink Node
            while (m_xmlReader.Read())
            {
                if (m_xmlReader.NodeType != XmlNodeType.Element)
                {
                    break;
                }

                if (m_xmlReader.LocalName != XlsxWorksheet.N_hyperlink)
                {
                    break;
                }

                string aref      = m_xmlReader.GetAttribute(XlsxWorksheet.A_ref);
                string display   = m_xmlReader.GetAttribute(XlsxWorksheet.A_display);
                string rid       = m_xmlReader.GetAttribute(XlsxWorksheet.A_rid);
                string hyperlink = display;

                Debug.Assert(rid != null);
                if (hyperDict.ContainsKey(rid))
                {
                    hyperlink = hyperDict[rid];
                }

                int col = -1;
                int row = -1;
                XlsxDimension.XlsxDim(aref, out col, out row);
                if (col >= 1 && row >= 1)
                {
                    row = row - 1;
                    col = col - 1;
                    if (row < table.Rows.Count)
                    {
                        if (col < table.Rows[row].Count)
                        {
                            object value = table.Rows[row][col];
                            var    cell  = new XlsCell(value);
                            cell.SetHyperLink(hyperlink);
                            table.Rows[row][col] = cell;
                        }
                    }
                }
            }

            // Close
            m_xmlReader.Close();
            if (m_sheetStream != null)
            {
                m_sheetStream.Close();
            }

            return(true);
        }
コード例 #8
0
ファイル: ExcelOpenXmlReader.cs プロジェクト: yichudu/exceltk
        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);
            }
        }
コード例 #9
0
        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);
        }
コード例 #10
0
        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);
        }
コード例 #11
0
        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);
        }
コード例 #12
0
ファイル: ExcelOpenXmlReader.cs プロジェクト: q425163005/GF
        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);
        }