public static ActiveInventoryCollection ImportFromSpreadsheet(string file) { ActiveInventoryCollection retVal = new ActiveInventoryCollection(); try { using (LumenWorks.Framework.IO.Csv.CsvReader reader = new LumenWorks.Framework.IO.Csv.CsvReader(new System.IO.StreamReader(file, Encoding.GetEncoding(1252)), true)) { reader.SupportsMultiline = false; //reader.ReadNextRecord(); //Header Record? //string x = reader["UPC"]; while (reader.ReadNextRecord()) { if (!string.IsNullOrEmpty(reader["SKU"]) && !reader["UPC"].Contains("~~")) { ActiveInventoryObject aio = new ActiveInventoryObject(); string data = reader.GetCurrentRawData(); aio.UPC = reader["UPC"]; int i = 0; if (int.TryParse(reader["Product ID"], out i)) { aio.ProductID = i; } aio.Distributor = reader["Distributor"]; aio.SKU = reader["SKU"]; aio.Name = reader["Description"]; //aio.Category = reader["Category"]; try { if (reader["Is Model Kit"].ToUpperInvariant() == "YES") { //aio.DiscountRateLevel = 0.05M; aio.IsModelKit = true; } else { aio.IsModelKit = false; } } catch (Exception ex) { if (_log.IsWarnEnabled) { _log.Warn("Error importing: ", ex); } } try { //Quantity of zero might be valid--this could change that. if (!int.TryParse(reader["Quantity"], out i)) { aio.Quantity = 0; } else { aio.Quantity = i; } } catch (Exception ex) { if (_log.IsWarnEnabled) { _log.Warn("Error importing: ", ex); } } decimal d = 0; try { if (decimal.TryParse(reader["Wholesale"].Replace("$", string.Empty), out d)) { aio.WholeSalePrice = d; } else { aio.WholeSalePrice = 0; } } catch (Exception ex) { if (_log.IsWarnEnabled) { _log.Warn("Error importing: ", ex); } } try { if (decimal.TryParse(reader["MSRP"].Replace("$", string.Empty), out d)) { aio.MSRP = d; } else { aio.MSRP = 0; } } catch (Exception ex) { if (_log.IsWarnEnabled) { _log.Warn("Error importing: ", ex); } } //try //{ // if (decimal.TryParse(reader["Add'l Overhead"].Replace("$", string.Empty), out d)) // { // aio.AdditionalOverhead = d; // } // else // { // aio.AdditionalOverhead = 0; // } //} //catch (Exception ex) //{ // if (_log.IsWarnEnabled) // { // _log.Warn("Error importing: ", ex); // } //} try { if (decimal.TryParse(reader["Price"].Replace("$", string.Empty), out d)) { aio.SellingPrice = d; } } catch (Exception ex) { if (_log.IsWarnEnabled) { _log.Warn("Error importing: ", ex); } } retVal.Add(aio); } } } } catch (Exception ex) { MessageBox.Show("Error on Import:\r\n\r\n" + ex.Message); retVal = null; if (_log.IsWarnEnabled) { _log.Warn("Error importing: ", ex); } } return(retVal); }
public static ActiveInventoryCollection ImportFromWebsiteBase(string file) { ActiveInventoryCollection retVal = new ActiveInventoryCollection(); decimal d = 0; try { using (LumenWorks.Framework.IO.Csv.CsvReader reader = new LumenWorks.Framework.IO.Csv.CsvReader(new System.IO.StreamReader(file, Encoding.GetEncoding(1252)), true)) { reader.SupportsMultiline = true; //reader.ReadNextRecord(); //Header Record? while (reader.ReadNextRecord()) { ActiveInventoryObject aio = new ActiveInventoryObject(); string data = reader.GetCurrentRawData(); aio.UPC = reader["upc"]; int i = 0; if (int.TryParse(reader["product_id"], out i)) { aio.ProductID = i; } aio.Name = reader["name"]; aio.SKU = reader["sku"]; //aio.Model = reader["model"]; aio.PictureURL = reader["image_name"]; aio.Description = reader["description"]; //aio.Category = reader["Category"]; if (!int.TryParse(reader["quantity"], out i)) { aio.Quantity = 0; } else { aio.Quantity = i; } try { if (decimal.TryParse(reader["price"], out d)) { aio.SellingPrice = d; } } catch { } aio.Metadata = reader["meta_description"]; aio.Keywords = reader["meta_keywords"]; retVal.Add(aio); } } } catch (Exception ex) { MessageBox.Show("Error on Import:\r\n\r\n" + ex.Message); retVal = null; if (_log.IsWarnEnabled) { _log.Warn("Error importing: ", ex); } } return(retVal); }