コード例 #1
0
        public ExcelDataReader(Stream file,
                               ref List <DataItemINTEX> listItems,
                               ref List <DataPalletINTEX> listPallets,
                               ref List <DataCaseINTEX> listCases)
        {
            XlsHeader         hdr           = XlsHeader.ReadHeader(file);
            XlsRootDirectory  dir           = new XlsRootDirectory(hdr);
            XlsDirectoryEntry workbookEntry = dir.FindEntry("Workbook");

            if (workbookEntry == null)
            {
                workbookEntry = dir.FindEntry("Book");
            }
            if (workbookEntry == null)
            {
                throw new FileNotFoundException("Oops! Neither stream 'Workbook' nor 'Book' was found in file");
            }
            if (workbookEntry.EntryType != STGTY.STGTY_STREAM)
            {
                throw new FormatException("Oops! Workbook directory entry is not a Stream");
            }
            m_stream = new XlsBiffStream(hdr, workbookEntry.StreamFirstSector);
            ReadWorkbookGlobals();
            GC.Collect();
            m_workbookData = new DataSet();
            // first sheet : boxes
            for (int iSheet = 0; iSheet < m_sheets.Count; ++iSheet)
            {
                if (string.Equals(m_sheets[iSheet].Name, "Articles", StringComparison.CurrentCultureIgnoreCase))
                {
                    listItems = new List <DataItemINTEX>();
                    if (ReadWorksheet(m_sheets[iSheet]))
                    {
                        DataTable dt = m_sheets[iSheet].Data;
                        for (int iRow = 4; iRow < dt.Rows.Count; ++iRow)
                        {
                            try
                            {
                                DataItemINTEX item = new DataItemINTEX();
                                // ref
                                item._ref = ParseObjectToString(dt.Rows[iRow][0], "Ref");
                                // description
                                if (DBNull.Value != dt.Rows[iRow][1])
                                {
                                    item._description = (string)dt.Rows[iRow][1];
                                }
                                // UPC
                                item._UPC = ParseObjectToString(dt.Rows[iRow][2], "UPC");
                                // PCB
                                item._PCB = ParseObjectToInt(dt.Rows[iRow][3], "PCB");
                                // GenCode
                                item._gencode = ParseObjectToString(dt.Rows[iRow][4], "GenCode");
                                // weight
                                item._weight = ParseObjectToDouble(dt.Rows[iRow][5], "Weight");
                                // length / width / eight
                                item._length = ParseObjectToDouble(dt.Rows[iRow][6], "Length");
                                item._width  = ParseObjectToDouble(dt.Rows[iRow][7], "Width");;
                                item._height = ParseObjectToDouble(dt.Rows[iRow][8], "Height");;
                                listItems.Add(item);
                            }
                            catch (ExcelDataException ex)
                            { _log.InfoFormat("Row {0} - {1} is null or empty -> skipping...", iRow, ex.Message); }
                            catch (Exception ex)
                            { _log.ErrorFormat("Row = {0} -> {1}", iRow, ex.Message); }
                        }
                    }
                }
                // Pallets
                else if (string.Equals(m_sheets[iSheet].Name, "Palettes", StringComparison.CurrentCultureIgnoreCase))
                {
                    listPallets = new List <DataPalletINTEX>();
                    if (ReadWorksheet(m_sheets[iSheet]))
                    {
                        DataTable dt = m_sheets[iSheet].Data;
                        for (int iRow = 4; iRow < dt.Rows.Count; ++iRow)
                        {
                            try
                            {
                                DataPalletINTEX pallet = new DataPalletINTEX();
                                if (DBNull.Value != dt.Rows[iRow][0])
                                {
                                    pallet._type = (string)dt.Rows[iRow][0];
                                }
                                pallet._length = ParseObjectToDouble(dt.Rows[iRow][1], "Pallet length");
                                pallet._width  = ParseObjectToDouble(dt.Rows[iRow][2], "Pallet width");
                                pallet._height = ParseObjectToDouble(dt.Rows[iRow][3], "Pallet height");
                                if (!DBNull.Value.Equals(dt.Rows[iRow][4]))
                                {
                                    pallet._weight = double.Parse((string)dt.Rows[iRow][4], System.Globalization.CultureInfo.InvariantCulture);
                                }
                                listPallets.Add(pallet);
                            }
                            catch (ExcelDataException ex)
                            { _log.InfoFormat("Row {0} - {1} is null or empty -> skipping...", iRow, ex.Message); }
                            catch (Exception ex)
                            { _log.ErrorFormat("Row = {0} -> {1}", iRow, ex.Message); }
                        }
                    }
                }
                // Caisses
                else if (string.Equals(m_sheets[iSheet].Name, "Caisses", StringComparison.CurrentCultureIgnoreCase))
                {
                    listCases = new List <DataCaseINTEX>();
                    if (ReadWorksheet(m_sheets[iSheet]))
                    {
                        DataTable dt = m_sheets[iSheet].Data;
                        for (int iRow = 4; iRow < dt.Rows.Count; ++iRow)
                        {
                            try
                            {
                                DataCaseINTEX caseItem = new DataCaseINTEX();
                                caseItem._ref       = ParseObjectToString(dt.Rows[iRow][0], "Ref");
                                caseItem._lengthExt = ParseObjectToDouble(dt.Rows[iRow][1], "Case length Ext.");
                                caseItem._widthExt  = ParseObjectToDouble(dt.Rows[iRow][2], "Case width Ext.");
                                caseItem._heightExt = ParseObjectToDouble(dt.Rows[iRow][3], "Case height Ext.");
                                if (!DBNull.Value.Equals(dt.Rows[iRow][4]))
                                {
                                    caseItem._lengthInt = double.Parse((string)dt.Rows[iRow][4], System.Globalization.CultureInfo.InvariantCulture);
                                }
                                if (!DBNull.Value.Equals(dt.Rows[iRow][5]))
                                {
                                    caseItem._widthInt = double.Parse((string)dt.Rows[iRow][5], System.Globalization.CultureInfo.InvariantCulture);
                                }
                                if (!DBNull.Value.Equals(dt.Rows[iRow][6]))
                                {
                                    caseItem._heightInt = double.Parse((string)dt.Rows[iRow][6], System.Globalization.CultureInfo.InvariantCulture);
                                }
                                if (!DBNull.Value.Equals(dt.Rows[iRow][7]))
                                {
                                    caseItem._weight = double.Parse((string)dt.Rows[iRow][7], System.Globalization.CultureInfo.InvariantCulture);
                                }
                                listCases.Add(caseItem);
                            }
                            catch (ExcelDataException ex)
                            { _log.InfoFormat("Row {0} - {1} is null or empty -> skipping...", iRow, ex.Message); }
                            catch (Exception ex)
                            { _log.Error(ex.ToString()); }
                        }
                    }
                }
            }

            m_globals.SST = null;
            m_globals     = null;
            m_sheets      = null;
            m_stream      = null;
            hdr           = null;
            GC.Collect();
        }
コード例 #2
0
        public ExcelDataReader(Stream file,
                               ref List <DataItemINTEX> listItems,
                               ref List <DataPalletINTEX> listPallets,
                               ref List <DataCaseINTEX> listCases)
        {
            XlsHeader         hdr           = XlsHeader.ReadHeader(file);
            XlsRootDirectory  dir           = new XlsRootDirectory(hdr);
            XlsDirectoryEntry workbookEntry = dir.FindEntry("Workbook");

            if (workbookEntry == null)
            {
                workbookEntry = dir.FindEntry("Book");
            }
            if (workbookEntry == null)
            {
                throw new FileNotFoundException("Oops! Neither stream 'Workbook' nor 'Book' was found in file");
            }
            if (workbookEntry.EntryType != STGTY.STGTY_STREAM)
            {
                throw new FormatException("Oops! Workbook directory entry is not a Stream");
            }
            m_stream = new XlsBiffStream(hdr, workbookEntry.StreamFirstSector);
            ReadWorkbookGlobals();
            GC.Collect();
            m_workbookData = new DataSet();
            // first sheet : boxes
            for (int iSheet = 0; iSheet < m_sheets.Count; ++iSheet)
            {
                if (string.Equals(m_sheets[iSheet].Name, "Articles", StringComparison.CurrentCultureIgnoreCase))
                {
                    listItems = new List <DataItemINTEX>();
                    if (ReadWorksheet(m_sheets[iSheet]))
                    {
                        DataTable dt = m_sheets[iSheet].Data;
                        for (int iRow = 4; iRow < dt.Rows.Count; ++iRow)
                        {
                            try
                            {
                                DataItemINTEX item = new DataItemINTEX();
                                item._ref         = (string)dt.Rows[iRow][0];
                                item._description = (string)dt.Rows[iRow][1];
                                if (DBNull.Value != dt.Rows[iRow][2])
                                {
                                    item._UPC = (string)dt.Rows[iRow][2];
                                }
                                if (DBNull.Value != dt.Rows[iRow][3])
                                {
                                    item._PCB = Convert.ToInt32((string)dt.Rows[iRow][3]);
                                }
                                if (DBNull.Value != dt.Rows[iRow][4])
                                {
                                    item._gencode = (string)dt.Rows[iRow][4];
                                }
                                item._weight = double.Parse((string)dt.Rows[iRow][5], System.Globalization.CultureInfo.InvariantCulture);
                                item._length = double.Parse((string)dt.Rows[iRow][6], System.Globalization.CultureInfo.InvariantCulture);
                                item._width  = double.Parse((string)dt.Rows[iRow][7], System.Globalization.CultureInfo.InvariantCulture);
                                item._height = double.Parse((string)dt.Rows[iRow][8], System.Globalization.CultureInfo.InvariantCulture);
                                listItems.Add(item);
                            }
                            catch (Exception /*ex*/)
                            {
                            }
                        }
                    }
                }
                // Pallets
                else if (string.Equals(m_sheets[iSheet].Name, "Palettes", StringComparison.CurrentCultureIgnoreCase))
                {
                    listPallets = new List <DataPalletINTEX>();
                    if (ReadWorksheet(m_sheets[iSheet]))
                    {
                        DataTable dt = m_sheets[iSheet].Data;
                        for (int iRow = 4; iRow < dt.Rows.Count; ++iRow)
                        {
                            try
                            {
                                DataPalletINTEX pallet = new DataPalletINTEX();
                                pallet._type   = (string)dt.Rows[iRow][0];
                                pallet._length = double.Parse((string)dt.Rows[iRow][1], System.Globalization.CultureInfo.InvariantCulture);
                                pallet._width  = double.Parse((string)dt.Rows[iRow][2], System.Globalization.CultureInfo.InvariantCulture);
                                pallet._height = double.Parse((string)dt.Rows[iRow][3], System.Globalization.CultureInfo.InvariantCulture);
                                if (!DBNull.Value.Equals(dt.Rows[iRow][4]))
                                {
                                    pallet._weight = double.Parse((string)dt.Rows[iRow][4], System.Globalization.CultureInfo.InvariantCulture);
                                }
                                listPallets.Add(pallet);
                            }
                            catch (Exception /*ex*/)
                            {
                            }
                        }
                    }
                }
                // Caisses
                else if (string.Equals(m_sheets[iSheet].Name, "Caisses", StringComparison.CurrentCultureIgnoreCase))
                {
                    listCases = new List <DataCaseINTEX>();
                    if (ReadWorksheet(m_sheets[iSheet]))
                    {
                        DataTable dt = m_sheets[iSheet].Data;
                        for (int iRow = 4; iRow < dt.Rows.Count; ++iRow)
                        {
                            try
                            {
                                DataCaseINTEX caseItem = new DataCaseINTEX();
                                caseItem._ref       = (string)dt.Rows[iRow][0];
                                caseItem._lengthExt = double.Parse((string)dt.Rows[iRow][1], System.Globalization.CultureInfo.InvariantCulture);
                                caseItem._widthExt  = double.Parse((string)dt.Rows[iRow][2], System.Globalization.CultureInfo.InvariantCulture);
                                caseItem._heightExt = double.Parse((string)dt.Rows[iRow][3], System.Globalization.CultureInfo.InvariantCulture);
                                if (!DBNull.Value.Equals(dt.Rows[iRow][4]))
                                {
                                    caseItem._lengthInt = double.Parse((string)dt.Rows[iRow][4], System.Globalization.CultureInfo.InvariantCulture);
                                }
                                if (!DBNull.Value.Equals(dt.Rows[iRow][5]))
                                {
                                    caseItem._widthInt = double.Parse((string)dt.Rows[iRow][5], System.Globalization.CultureInfo.InvariantCulture);
                                }
                                if (!DBNull.Value.Equals(dt.Rows[iRow][6]))
                                {
                                    caseItem._heightInt = double.Parse((string)dt.Rows[iRow][6], System.Globalization.CultureInfo.InvariantCulture);
                                }
                                if (!DBNull.Value.Equals(dt.Rows[iRow][7]))
                                {
                                    caseItem._weight = double.Parse((string)dt.Rows[iRow][7], System.Globalization.CultureInfo.InvariantCulture);
                                }
                                listCases.Add(caseItem);
                            }
                            catch (Exception /*ex*/)
                            {
                            }
                        }
                    }
                }
            }

            m_globals.SST = null;
            m_globals     = null;
            m_sheets      = null;
            m_stream      = null;
            hdr           = null;
            GC.Collect();
        }