예제 #1
0
        /// <summary>
        /// 获取Worksheet
        /// </summary>
        /// <param name="document">document对象</param>
        /// <param name="sheetName">sheetName可空</param>
        /// <returns>Worksheet对象</returns>
        public static SheetEx GetWorksheet(this SpreadsheetDocument document, string sheetName = null)
        {
            var sheets = document.WorkbookPart.Workbook.Descendants <Sheet>();
            var sheet  = (sheetName == null
                                  ? sheets.FirstOrDefault()
                                  : sheets.FirstOrDefault(s => s.Name == sheetName)) ?? sheets.FirstOrDefault();
            var     worksheetPart = (WorksheetPart)document.WorkbookPart.GetPartById(sheet.Id);
            SheetEx sheetEx       = new SheetEx();

            sheetEx.Worksheet = worksheetPart.Worksheet;
            sheetEx.SheetId   = sheet.Id;
            sheetEx.SheetName = sheet.Name;
            return(sheetEx);
        }
예제 #2
0
        /// <summary>
        /// 获取Worksheet
        /// </summary>
        /// <param name="document">document对象</param>
        /// <param name="sheetName">sheetName可空</param>
        /// <returns>Worksheet对象</returns>
        public static List <SheetEx> GetWorksheets(this SpreadsheetDocument document)
        {
            List <SheetEx> list   = new List <SheetEx>();
            var            sheets = document.WorkbookPart.Workbook.Descendants <Sheet>();

            foreach (var sheet in sheets)
            {
                var     worksheetPart = (WorksheetPart)document.WorkbookPart.GetPartById(sheet.Id);
                SheetEx sheetEx       = new SheetEx();
                sheetEx.Worksheet = worksheetPart.Worksheet;
                sheetEx.SheetId   = sheet.Id;
                sheetEx.SheetName = sheet.Name;
                list.Add(sheetEx);
            }
            return(list);
        }
예제 #3
0
        private List <OpenXmlImportImages> GetOpenXmlImportImages(SheetEx sheet, WorkbookPart workbookPart)
        {
            WorksheetPart wsPart                = (WorksheetPart)workbookPart.GetPartById(sheet.SheetId);
            DrawingsPart  drawingPart           = wsPart.GetPartsOfType <DrawingsPart>().ToList().FirstOrDefault();
            List <OpenXmlImportImages> pictures = new List <OpenXmlImportImages>();

            if (drawingPart != null)
            {
                foreach (var part in drawingPart.Parts)
                {
                    OpenXmlImportImages pic     = new OpenXmlImportImages();
                    ImagePart           imgPart = (ImagePart)part.OpenXmlPart;
                    pic.Image = StreamToBytes(imgPart.GetStream());
                    pic.RefId = part.RelationshipId;
                    pictures.Add(pic);
                }

                var worksheetDrawings = drawingPart.WorksheetDrawing.Where(c => c.ChildElements.Any
                                                                               (a => a.GetType().FullName == "DocumentFormat.OpenXml.Drawing.Spreadsheet.Picture")).ToList();
                foreach (var worksheetDrawing in worksheetDrawings)
                {
                    if (worksheetDrawing.GetType().FullName ==
                        "DocumentFormat.OpenXml.Drawing.Spreadsheet.TwoCellAnchor")
                    {
                        TwoCellAnchor anchor = (TwoCellAnchor)worksheetDrawing;
                        DocumentFormat.OpenXml.Drawing.Spreadsheet.Picture picDef =
                            (DocumentFormat.OpenXml.Drawing.Spreadsheet.Picture)
                            anchor.ChildElements.FirstOrDefault(c => c.GetType().FullName ==
                                                                "DocumentFormat.OpenXml.Drawing.Spreadsheet.Picture");
                        if (picDef != null)
                        {
                            var embed = picDef.BlipFill.Blip.Embed;
                            if (embed != null)
                            {
                                var picMapping = pictures.FirstOrDefault(c => c.RefId == embed.InnerText);
                                picMapping.FromCol = int.Parse(anchor.FromMarker.ColumnId.InnerText);
                                picMapping.FromRow = int.Parse(anchor.FromMarker.RowId.InnerText);
                            }
                        }
                    }
                }
            }
            return(pictures);
        }