Exemple #1
0
 public ExcelUtil(ExcelDomain excelDomain)
 {
     this.ExcelDomain = excelDomain;
 }
Exemple #2
0
        public ExcelDomain ImportTemplate(string path)
        {
            var fileName = this.GetFileName(path);

            this.ExcelDomain = new ExcelDomain
            {
                LocalPath = path.Substring(0, path.LastIndexOf("\\")),
                FileName  = fileName,
                Sheet     = new SheetDomain
                {
                    Name = fileName
                },
                BodyDomain = new BodyDomain
                {
                    DataList            = new List <RowDomain>(),
                    StyleDomain         = GetStyleBody(),
                    CannotReadRowNumber = new List <int>()
                },
                TitleList = new List <TitleDomain>(),
                ErrorFlag = false
            };
            Excel.Application excelApplication = new Excel.Application();
            Excel.Workbook    excelBook        = excelApplication.Workbooks.Open(path, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            Excel.Worksheet   excelSheet       = excelBook.Worksheets.get_Item(1);
            Excel.Range       excelRange       = excelSheet.UsedRange;
            //excelSheet.get_Range();

            // read title
            string title = excelRange.Cells[1, 1].Value2;

            this.ReadTitle(title);

            // read column name
            int rowCount    = 0;
            int columnCount = 0;

            for (columnCount = 1; columnCount <= excelRange.Columns.Count; columnCount++)
            {
                string columnName = excelRange.Cells[2, columnCount].Value2;
                this.ReadColumnName(columnCount, columnName);
            }

            List <ErrorDomain> errorList = new List <ErrorDomain>();

            // read body
            for (rowCount = 3; rowCount <= excelRange.Rows.Count; rowCount++)
            {
                var rowDomain = this.ReadRow(rowCount);
                for (columnCount = 1; columnCount < excelRange.Columns.Count; columnCount++)
                {
                    try
                    {
                        var cellValue = excelRange.Cells[rowCount, columnCount].Value2;
                        this.ReadCell(columnCount, cellValue, rowDomain);
                    }
                    catch (Exception)
                    {
                        this.ExcelDomain.BodyDomain.CannotReadRowNumber.Add(rowCount);
                        continue;
                    }
                }
                this.ReadBody(rowDomain);
            }
            return(this.ExcelDomain);
        }