Exemplo n.º 1
0
        public virtual void WriteExcelDocument <TObject>(string filePath, Dictionary <string, List <TObject> > lstObject)
            where TObject : class
        {
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }

            var document     = SpreadsheetDocument.Create(filePath, SpreadsheetDocumentType.Workbook);
            var workbookpart = document.AddWorkbookPart();

            workbookpart.Workbook = new Workbook();
            var sheets = document.WorkbookPart.Workbook.AppendChild(new Sheets());

            var    headers = Activator.CreateInstance <TObject>().GetExcelHeaders();
            UInt32 i       = 1;

            foreach (var item in lstObject)
            {
                var worksheetPart = document.WorkbookPart.AddNewPart <WorksheetPart>();
                var sheetData     = new SheetData();
                var sheetName     = item.Key;
                worksheetPart.Worksheet = new Worksheet(sheetData);

                var data = new OpenXmlEntities.SLExcelData
                {
                    Headers   = headers.ToList(),
                    SheetName = sheetName
                };

                foreach (var itemRow in item.Value)
                {
                    var row = ObjectParser.FillExcelObject(itemRow);
                    data.DataRows.Add(row.ToList());
                }

                #region Incluindo Sheet

                var sheet = new Sheet
                {
                    Id = document.WorkbookPart
                         .GetIdOfPart(worksheetPart),
                    SheetId = i++,
                    Name    = sheetName
                };
                workbookpart.Workbook.LastChild.AppendChild(sheet);

                #endregion

                this.InsertSheetData(document, sheetData, data);
            }

            document.Close();
        }
Exemplo n.º 2
0
        public void WriteExcelDocument <TObject>(IEnumerable <TObject> lstObject, string filePath, string sheetName, List <string> headers) where TObject : class
        {
            var data = new OpenXmlEntities.SLExcelData
            {
                Headers   = headers.ToList(),
                SheetName = sheetName
            };

            foreach (var item in lstObject)
            {
                var row = ObjectParser.GetValuesFromObject(item, headers);
                data.DataRows.Add(row.ToList());
            }

            OpenXmlWriter.GenerateExcel(data, filePath);
        }
Exemplo n.º 3
0
        public MemoryStream WriteExcelDocumentStream <TObject>(IEnumerable <TObject> lstObject, string sheetName, List <string> headers) where TObject : class
        {
            var data = new OpenXmlEntities.SLExcelData
            {
                Headers   = headers.ToList(),
                SheetName = sheetName
            };

            foreach (var item in lstObject)
            {
                var row = ObjectParser.FillExcelObject(item);
                data.DataRows.Add(row.ToList());
            }

            return(OpenXmlWriter.GenerateExcelStream(data));
        }
Exemplo n.º 4
0
        public void WriteExcelDocument <TObject>(IEnumerable <TObject> lstObject, string filePath, string sheetName) where TObject : class
        {
            var headers = Activator.CreateInstance <TObject>().GetExcelHeaders();
            var data    = new OpenXmlEntities.SLExcelData
            {
                Headers   = headers.ToList(),
                SheetName = sheetName
            };

            foreach (var item in lstObject)
            {
                var row = ObjectParser.FillExcelObject(item);
                data.DataRows.Add(row.ToList());
            }

            OpenXmlWriter.GenerateExcel(data, filePath);
        }