Esempio n. 1
0
        public System.Data.DataSet AsDataSet(bool convertOADateTime)
        {
            if (!_isValid)
            {
                return(null);
            }

            DataSet dataset = new DataSet();

            for (int ind = 0; ind < _workbook.Sheets.Count; ind++)
            {
                DataTable table = new DataTable(_workbook.Sheets[ind].Name);

                ReadSheetGlobals(_workbook.Sheets[ind]);

                if (_workbook.Sheets[ind].Dimension == null)
                {
                    continue;
                }

                _depth         = 0;
                _emptyRowCount = 0;

                //DataTable columns
                if (!_isFirstRowAsColumnNames)
                {
                    for (int i = 0; i < _workbook.Sheets[ind].ColumnsCount; i++)
                    {
                        table.Columns.Add(null, typeof(Object));
                    }
                }
                else if (ReadSheetRow(_workbook.Sheets[ind]))
                {
                    for (int index = 0; index < _cellsValues.Length; index++)
                    {
                        if (_cellsValues[index] != null && _cellsValues[index].ToString().Length > 0)
                        {
                            Helpers.AddColumnHandleDuplicate(table, _cellsValues[index].ToString());
                        }
                        else
                        {
                            Helpers.AddColumnHandleDuplicate(table, string.Concat(COLUMN, index));
                        }
                    }
                }
                else
                {
                    continue;
                }

                table.BeginLoadData();

                while (ReadSheetRow(_workbook.Sheets[ind]))
                {
                    table.Rows.Add(_cellsValues);
                }

                if (table.Rows.Count > 0)
                {
                    dataset.Tables.Add(table);
                }
                table.EndLoadData();
            }
            dataset.AcceptChanges();
            Helpers.FixDataTypes(dataset);
            return(dataset);
        }
Esempio n. 2
0
        private DataTable readWholeWorkSheet(XlsWorksheet sheet)
        {
            XlsBiffIndex idx;

            if (!readWorkSheetGlobals(sheet, out idx))
            {
                return(null);
            }

            DataTable table = new DataTable(sheet.Name);

            bool triggerCreateColumns = true;

            m_dbCellAddrs = idx.DbCellAddresses;

            for (int index = 0; index < m_dbCellAddrs.Length; index++)
            {
                if (m_depht == m_maxRow)
                {
                    break;
                }

                // init reading data
                m_cellOffset = findFirstDataCellOffset((int)m_dbCellAddrs[index]);

                //DataTable columns
                if (triggerCreateColumns)
                {
                    if (_isFirstRowAsColumnNames && readWorkSheetRow() || (_isFirstRowAsColumnNames && m_maxRow == 1))
                    {
                        for (int i = 0; i < m_maxCol; i++)
                        {
                            if (m_cellsValues[i] != null && m_cellsValues[i].ToString().Length > 0)
                            {
                                table.Columns.Add(m_cellsValues[i].ToString());
                            }
                            else
                            {
                                table.Columns.Add(string.Concat(COLUMN, i));
                            }
                        }
                    }
                    else
                    {
                        for (int i = 0; i < m_maxCol; i++)
                        {
                            table.Columns.Add();
                        }
                    }

                    triggerCreateColumns = false;

                    table.BeginLoadData();
                }

                while (readWorkSheetRow())
                {
                    table.Rows.Add(m_cellsValues);
                }

                if (m_depht > 0 && !(_isFirstRowAsColumnNames && m_maxRow == 1))
                {
                    table.Rows.Add(m_cellsValues);
                }
            }

            table.EndLoadData();
            return(table);
        }