Пример #1
0
        public async Task Export(string filePath, List <Rec> collection, IProgress <ProgressModel> progress) => await Task.Run(() =>
        {
            Rec rec             = new Rec();
            string sheetName    = Path.GetFileNameWithoutExtension(filePath);
            XSSFWorkbook xSSF   = new XSSFWorkbook();
            ISheet sheet        = xSSF.CreateSheet(sheetName);
            var propertiesName  = rec.GetType().GetProperties();
            ProgressModel model = new ProgressModel();

            var requiredProps = settingHelper.GetSettingNames();
            var headerRow     = sheet.CreateRow(0);
            for (int i = 0, j = 0; i < propertiesName.Length; i++)
            {
                if (!requiredProps.Any(x => x.Equals(propertiesName[i].Name)))
                {
                    continue;
                }

                var cell = headerRow.CreateCell(j);
                cell.SetCellValue(propertiesName[i].Name);
                j++;
            }

            for (int i = 0; i < collection.Count; i++)
            {
                var rowIndex     = i + 1;
                var row          = sheet.CreateRow(rowIndex);
                model.Percentage = i;
                progress.Report(model);
                var o = collection[i];
                for (int j = 0, k = 0; j < propertiesName.Length; j++)
                {
                    if (!requiredProps.Any(x => x.Equals(propertiesName[j].Name)))
                    {
                        continue;
                    }

                    var value         = o.GetType().GetProperty(propertiesName[j].Name).GetValue(o, null).ToString();
                    CellType cellType = propertiesName[j].PropertyType.Name == "String" ? CellType.String : (propertiesName[j].PropertyType.Name == "Nullable`1" ? CellType.String : CellType.Numeric);
                    var cell          = row.CreateCell(k);
                    k++;
                    if (cellType == CellType.Numeric)
                    {
                        var result = double.Parse(value);
                        cell.SetCellValue(result);
                    }
                    else
                    {
                        cell.SetCellValue(value);
                    }
                    cell.SetCellType(cellType);
                }
            }
            model.Percentage = 0;
            progress.Report(model);
            FileStream file = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write);
            xSSF.Write(file);
            file.Dispose();
            file.Close();
            Process.Start(filePath);
        });