Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using BST.ExcelImporter.Interfaces;
- using OfficeOpenXml;
- using ExcelRow = BST.ExcelImporter.Models.ExcelRow;
- using BST.ExcelImporter.Extensions;
- namespace BST.ExcelImporter.Controllers
- {
- public sealed class ReadFileController : IReadFileController
- {
- //Private
- private readonly FileInfo _file;
- //CTOR
- public ReadFileController(string path)
- {
- _file = new FileInfo(path);
- }
- //Public
- public ICollection<ExcelRow> Read()
- {
- //Prepare variables
- ICollection<ExcelRow> result = new List<ExcelRow>();
- List<string> headers = new List<string>();
- //Read file
- using (ExcelPackage package = new ExcelPackage(_file))
- {
- //Init excel values
- ExcelWorksheet worksheet = package.Workbook.Worksheets.First();
- ExcelAddressBase dimensions = worksheet.GetValuedDimension();
- int colCount = dimensions.End.Column;
- int rowCount = dimensions.End.Row;
- //Get headers;
- for (int y = 1; y <= colCount; y++)
- {
- headers.Add(worksheet.Cells[1, y].Value.ToString());
- }
- //Get values
- for (int x = 2; x <= rowCount; x++)
- {
- ExcelRow row = new ExcelRow();
- for (int y = 1; y <= colCount; y++)
- {
- row.Fields.Add(headers[y - 1], worksheet.Cells[x, y]);
- }
- result.Add(row);
- }
- }
- //Return
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement