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