Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.81 KB | None | 0 0
  1.      
  2.   private void barButtonItem8_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  3.         {
  4.             GetList(dt);
  5.         }
  6.  private void barButtonItem9_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  7.         {
  8.             ReadCsv();
  9.         }
  10.  
  11.         public void ReadCsv()
  12.         {
  13.             System.IO.Stream myStream = null;
  14.             //TWORZENIE TABELI NADANIE NAZW KOLUMNOWM
  15.             System.Data.DataTable table = new System.Data.DataTable("produkt");
  16.             table.Columns.Add("ST", typeof(String));
  17.             table.Columns.Add("GE", typeof(String));
  18.             table.Columns.Add("Boxes", typeof(String));
  19.             table.Columns.Add("ManufDate", typeof(String));
  20.             table.Columns.Add("ManufNumber", typeof(String));
  21.             table.Columns.Add("Expireddate", typeof(String));
  22.             table.Columns.Add("FROM", typeof(String));
  23.             table.Columns.Add("TO", typeof(String));
  24.  
  25.             // ROZPOCZECIE ODCZYTU PLIKu
  26.  
  27.             //TWORZENIE OBIEKTU OKNA DO WYBORU PLIKU
  28.             OpenFileDialog ofd = new OpenFileDialog();
  29.  
  30.             //USTAWIENIA FILTROWANIA PLIKOW
  31.             ofd.InitialDirectory = "c:\\";
  32.             ofd.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm;*.txt;*.csv";
  33.             ofd.FilterIndex = 2;
  34.             ofd.Multiselect = false;
  35.             // IEnumerable<Order> records;
  36.  
  37.             if (ofd.ShowDialog() == DialogResult.OK)
  38.             {
  39.                 try
  40.                 {
  41.                     if ((myStream = ofd.OpenFile()) != null)
  42.                         using (myStream = ofd.OpenFile())
  43.                         {
  44.                             fileLocation = ofd.FileName;
  45.  
  46.                             //  StreamReader reader = new StreamReader(myStream,Encoding.Default, true);
  47.  
  48.                             IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(myStream);
  49.  
  50.                             //// reader.IsFirstRowAsColumnNames
  51.                             var conf = new ExcelDataSetConfiguration
  52.                             {
  53.                                 ConfigureDataTable = _ => new ExcelDataTableConfiguration
  54.                                 {
  55.                                     EmptyColumnNamePrefix = "duppppppp",
  56.                                     FilterRow = rowReader => rowReader.Depth > 1,
  57.                                     UseHeaderRow = true,
  58.                                     ReadHeaderRow = (rowReader) =>
  59.                                     {
  60.                                         // F.ex skip the first row and use the 2nd row as column headers:
  61.                                         rowReader.Read();
  62.                                     },
  63.                                 }
  64.                             };
  65.  
  66.                             var dataSet = excelReader.AsDataSet(conf);
  67.                             table = dataSet.Tables[0];
  68.                             gridControl1.DataSource = table;
  69.  
  70.                             //...
  71.                         }
  72.  
  73.                 }
  74.                 catch (FileNotFoundException)
  75.                 {
  76.                     MessageBox.Show("FileNotFoundException");
  77.                 }
  78.                 catch (IOException)
  79.                 {
  80.                     MessageBox.Show("IOException");
  81.                 }
  82.                 catch (OutOfMemoryException)
  83.                 {
  84.                     MessageBox.Show("OutOfMemoryException");
  85.                 }
  86.                 finally
  87.                 {
  88.                     dt = table;
  89.                 }
  90.             }
  91.         }
  92.  
  93.         public static List<Order> GetList(System.Data.DataTable dt)
  94.         {
  95.             //List<Order> list = dt.AsEnumerable().Select(x => new Order
  96.             //{
  97.             //    ST = (string)(x["ST"]),
  98.             //    GE =(string)(x["GE"]),
  99.             //      Boxes = (string)(x["Boxes"]),
  100.             //    ManufDate = (string)(x["ManufDate"]),
  101.             //    ManufNumber = (string)(x["ManufNumber"]),
  102.             //    Expireddate = (string)(x["Expireddate"]),
  103.             //    From = (string)(x["FROM"]),
  104.             //    To = (string)(x["TO"]),
  105.             //}).ToList();
  106.             List<Order> list = new List<Order>();
  107.             foreach (DataRow row in dt.Rows
  108.             {
  109.                 Order item = new Order();
  110.                 item.ST = row[0].ToString().TrimStart('S', 'T');
  111.                 item.GE = row[1].ToString();
  112.                 item.Boxes = row[2].ToString();
  113.                 item.ManufDate = row[3].ToString();
  114.                 item.ManufNumber = row[4].ToString();
  115.                 item.Expireddate = row[5].ToString();
  116.                 item.From = row[6].ToString();
  117.                 item.To = row[7].ToString();
  118.                 list.Add(item);
  119.             }
  120.             return list;
  121.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement