Exemplo n.º 1
0
 public static TResult Read <TResult>(Stream stream,
                                      Func <IUnderstandSheets, TResult> onRead)
 {
     using (var workbook = new OpenXmlWorkbook(stream))
     {
         return(onRead(workbook));
     }
 }
Exemplo n.º 2
0
        public static TResult Create <TResult>(System.IO.Stream stream,
                                               Func <IUnderstandSheets, TResult> callback)
        {
            using (var workbook = new OpenXmlWorkbook(SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook)))
            {
                var result = callback(workbook);

                #region Create sheets that link to the workbook pages

                var oxw = OpenXmlWriter.Create(workbook.workbook.WorkbookPart);
                oxw.WriteStartElement(new Workbook());
                oxw.WriteStartElement(new DocumentFormat.OpenXml.Spreadsheet.Sheets());

                // you can use object initialisers like this only when the properties
                // are actual properties. SDK classes sometimes have property-like properties
                // but are actually classes. For example, the Cell class has the CellValue
                // "property" but is actually a child class internally.
                // If the properties correspond to actual XML attributes, then you're fine.

                UInt32Value sheetCount = 1;
                foreach (var sheet in workbook.sheets)
                {
                    var sheetCreated = new Sheet()
                    {
                        Name    = sheet.Key,
                        SheetId = sheetCount,
                        Id      = workbook.workbook.WorkbookPart.GetIdOfPart(sheet.Value)
                    };
                    oxw.WriteElement(sheetCreated);
                    sheetCount++;
                }

                // this is for Sheets
                oxw.WriteEndElement();
                // this is for Workbook
                oxw.WriteEndElement();
                oxw.Close();

                #endregion

                return(result);
            }
        }