Esempio n. 1
0
        public async Task <IActionResult> PutSheet1(int id, Sheet1 sheet1)
        {
            if (id != sheet1.Id)
            {
                return(BadRequest());
            }
            _context.Entry(sheet1).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!Sheet1Exists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        private async Task UploadFile()
        {
            string res = "";

            using (FileStream stream = new FileStream("Книга1.xlsx", FileMode.Open))
            {
                using (var reader = ExcelReaderFactory.CreateReader(stream))
                {
                    int j = -1;
                    do
                    {
                        j++;
                        int      i       = 0;
                        string[] colName = new string[reader.FieldCount];
                        while (reader.Read())
                        {
                            for (int column = 0; column < reader.FieldCount; column++)
                            {
                                Console.WriteLine(reader.GetValue(column));
                                if (i == 0)
                                {
                                    colName[column] = reader.GetValue(column).ToString();
                                }
                                else
                                {
                                    Sheet sheet;

                                    if (j == 0)
                                    {
                                        sheet = new Sheet1()
                                        {
                                            ColumnName = colName[column],
                                            Value      = reader.GetValue(column).ToString(),
                                            DateTime   = DateTime.Now
                                        };
                                        await _context.Sheet1s.AddAsync((Sheet1)sheet);
                                    }
                                    else
                                    {
                                        sheet = new Sheet2()
                                        {
                                            ColumnName = colName[column],
                                            Value      = reader.GetValue(column).ToString(),
                                            DateTime   = DateTime.Now
                                        };
                                        await _context.Sheet2s.AddAsync((Sheet2)sheet);
                                    }
                                }
                            }
                            await _context.SaveChangesAsync();

                            i++;
                        }
                        j++;
                    } while (reader.NextResult());
                }
            }
        }