Beispiel #1
0
        public models.ExcelWorkbook Create(Stream stream)
        {
            var excelPackage = new ExcelPackage(stream);
            var worksheets   = new List <models.ExcelWorksheet>();

            foreach (var ws in excelPackage.Workbook.Worksheets)
            {
                var cells = ws.Cells.Where(cell => cell.Value != null).Select(cell => AdaptCell(cell)).ToList();
                worksheets.Add(new models.ExcelWorksheet(ws.Name, cells));
            }

            var result = new models.ExcelWorkbook(worksheets);

            return(result);
        }
Beispiel #2
0
        public void WriteToStream(models.ExcelWorkbook workbook)
        {
            var excelPackage = new ExcelPackage();

            foreach (var worksheet in workbook.Worksheets)
            {
                var ws = excelPackage.Workbook.Worksheets.Add(worksheet.Name);
                foreach (var cell in worksheet.Cells)
                {
                    var nativeCell = ws.Cells[cell.Address.Row, cell.Address.Column];
                    nativeCell.Value = cell.Value;
                    nativeCell.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
                    nativeCell.Style.Fill.BackgroundColor.SetColor(cell.Style.BackgroundColor);
                }
            }

            var fi = new FileInfo(@"C:\Users\mlm\Desktop\Test.xlsx");

            excelPackage.SaveAs(fi);
        }