示例#1
0
        public PriceListModel Parse(UserInputMessage message)
        {
            using (var package = new ExcelPackage(new FileInfo(message.FileUrl)))
            {
                var result = new PriceListModel
                {
                    Items = new List <PriceListItemModel>()
                };

                var sheet = package.Workbook.Worksheets.FirstOrDefault();
                if (sheet == null)
                {
                    throw new FileNotFoundException($"file {message.FileUrl} not found or does not have any sheets");
                }
                var rowCnt = sheet?.Dimension?.End?.Row ?? 0;

                for (int rowNum = 1; rowNum < rowCnt + 1; rowNum++)
                {
                    var row = new FileRow
                    {
                        Id    = sheet.Cells[rowNum, message.TitleColumn].Value?.ToString(),
                        Price = sheet.Cells[rowNum, message.PriceColumn].Value?.ToString()
                    };

                    var rowModel = _parseInputRowService.ParsePriceFromRow(row);

                    if (rowModel != null)
                    {
                        result.Items.Add(rowModel);
                    }
                }

                return(result);
            }
        }
示例#2
0
        public PriceListModel Parse(UserInputMessage message)
        {
            FileStream stream = File.Open(message.FileUrl, FileMode.Open, FileAccess.Read);

            //Choose one of either 1 or 2
            //1. Reading from a binary Excel file ('97-2003 format; *.xls)
            using (IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream))
            {
                //Choose one of either 3, 4, or 5
                //3. DataSet - The result of each spreadsheet will be created in the result.Tables
                DataSet table = excelReader.AsDataSet();


                var rowCnt = table.Tables[0].Rows.Count;
                var result = new PriceListModel
                {
                    Items = new List <PriceListItemModel>()
                };

                for (int rowNum = 0; rowNum < rowCnt; rowNum++)
                {
                    var row = new FileRow
                    {
                        Id    = table.Tables[0].Rows[rowNum].ItemArray[message.TitleColumn - 1]?.ToString(),
                        Price = table.Tables[0].Rows[rowNum].ItemArray[message.PriceColumn - 1]?.ToString()
                    };

                    var rowModel = _parseInputRowService.ParsePriceFromRow(row);

                    if (rowModel != null)
                    {
                        result.Items.Add(rowModel);
                    }
                }


                return(result);
            }
        }