C#和NPOI處理中的EXCEL中的圖片

使用NPOI操作EXCEL中的圖片導入導出
//本地圖片插入Excel中
//參考資料:NPOI導出excel,插入圖片
//http://blog.csdn.net/zhenzhenzhao12/article/details/22170027
//author:zhenzhenzhao12

//Excel中的圖片導出
//參考資料:使用NPOI從Excel中提取圖片及圖片位置信息
// http://www.cnblogs.com/hanzhaoxin/p/4442369.html

1.準備信息
NPOI 1.2.5,要插入的圖片

2.插入方法

   private void InsertImageToExcel()
        {
             byte[] bytes = System.IO.File.ReadAllBytes(@"myImage.png");
                IWorkbook wk = new HSSFWorkbook();
                int pictureIndex = wk.AddPicture(bytes, PictureType.PNG);
                ISheet sheet = wk.CreateSheet("Sheet1");
                // Create the drawing patriarch(家長,宗長之類的意思,這裏取容器之意).  This is the top level container for all shapes.  
                //HSSFPatriarch patriarch = sheet.CreateDrawingPatriarch();
                IDrawing patriarch = sheet.CreateDrawingPatriarch();

                HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, 0, 0, 1, 3);

                IPicture pict = patriarch.CreatePicture(anchor, pictureIndex);
                //使圖像恢復到原始大小
                //pict.Resize();
                using (FileStream fsWrite = File.OpenWrite("tt.xls"))
                {
                    wk.Write(fsWrite);
                }
                MessageBox.Show("成功");
        }

3.導出方法

  private void ExcelToImage()
        {
            using (FileStream fsReader = File.OpenRead("tt.xls"))
            {
                IWorkbook wk = new HSSFWorkbook(fsReader);
                ISheet sheet = wk.GetSheetAt(0);
                List<PicturesInfo> pictures = sheet.GetAllPictureInfos();

                for (int i = 0; i < pictures.Count; i++)
                {
                    using (FileStream fsWrite = File.OpenWrite(i.ToString() + ".jpg"))
                    {
                        fsWrite.Write(pictures[i].PictureData, 0, pictures[i].PictureData.Length);
                    }
                }
            }
            MessageBox.Show("導出成功");
        }

4.其他類:
PicturesInfo 來自韓兆新的博客園(文前的第二個鏈接)

 public class PicturesInfo
    {
        public int MinRow { get; set; }
        public int MaxRow { get; set; }
        public int MinCol { get; set; }
        public int MaxCol { get; set; }
        public Byte[] PictureData { get; private set; }

        public PicturesInfo(int minRow, int maxRow, int minCol, int maxCol, Byte[] pictureData)
        {
            this.MinRow = minRow;
            this.MaxRow = maxRow;
            this.MinCol = minCol;
            this.MaxCol = maxCol;
            this.PictureData = pictureData;
        }
    }

NpoiExtend.cs類,同樣來自韓兆新的博客,有修改

    public static class NpoiExtend
    {
        public static List<PicturesInfo> GetAllPictureInfos(this ISheet sheet)
        {
            return sheet.GetAllPictureInfos(null, null, null, null);
        }

        public static List<PicturesInfo> GetAllPictureInfos(this ISheet sheet, int? minRow, int? maxRow, int? minCol, int? maxCol, bool onlyInternal = true)
        {
            if (sheet is HSSFSheet)
            {
                return GetAllPictureInfos((HSSFSheet)sheet, minRow, maxRow, minCol, maxCol, onlyInternal);
            }
            //else if (sheet is XSSFSheet)
            //{
            //    return GetAllPictureInfos((XSSFSheet)sheet, minRow, maxRow, minCol, maxCol, onlyInternal);
            //}
            else
            {
                throw new Exception("未處理類型,沒有爲該類型添加:GetAllPicturesInfos()擴展方法!");
            }
        }

        private static List<PicturesInfo> GetAllPictureInfos(HSSFSheet sheet, int? minRow, int? maxRow, int? minCol, int? maxCol, bool onlyInternal)
        {
            List<PicturesInfo> picturesInfoList = new List<PicturesInfo>();

            var shapeContainer = sheet.DrawingPatriarch as HSSFShapeContainer;
            if (null != shapeContainer)
            {
                var shapeList = shapeContainer.Children;
                foreach (var shape in shapeList)
                {
                    if (shape is HSSFPicture)
                    {
                        var picture = (HSSFPicture)shape;
                       // var anchor = (HSSFClientAnchor)shape;
                        var anchor = (HSSFClientAnchor)picture.Anchor;
                        if (IsInternalOrIntersect(minRow, maxRow, minCol, maxCol, anchor.Row1, anchor.Row2, anchor.Col1, anchor.Col2, onlyInternal))
                        {
                            picturesInfoList.Add(new PicturesInfo(anchor.Row1, anchor.Row2, anchor.Col1, anchor.Col2, picture.PictureData.Data));
                        }
                    }
                }
            }

            return picturesInfoList;
        }

        //private static List<PicturesInfo> GetAllPictureInfos(XSSFSheet sheet, int? minRow, int? maxRow, int? minCol, int? maxCol, bool onlyInternal)
        //{
        //    List<PicturesInfo> picturesInfoList = new List<PicturesInfo>();

        //    var documentPartList = sheet.GetRelations();
        //    foreach (var documentPart in documentPartList)
        //    {
        //        if (documentPart is XSSFDrawing)
        //        {
        //            var drawing = (XSSFDrawing)documentPart;
        //            var shapeList = drawing.GetShapes();
        //            foreach (var shape in shapeList)
        //            {
        //                if (shape is XSSFPicture)
        //                {
        //                    var picture = (XSSFPicture)shape;
        //                    var anchor = picture.GetPreferredSize();

        //                    if (IsInternalOrIntersect(minRow, maxRow, minCol, maxCol, anchor.Row1, anchor.Row2, anchor.Col1, anchor.Col2, onlyInternal))
        //                    {
        //                        picturesInfoList.Add(new PicturesInfo(anchor.Row1, anchor.Row2, anchor.Col1, anchor.Col2, picture.PictureData.Data));
        //                    }
        //                }
        //            }
        //        }
        //    }

        //    return picturesInfoList;
        //}

        private static bool IsInternalOrIntersect(int? rangeMinRow, int? rangeMaxRow, int? rangeMinCol, int? rangeMaxCol,
            int pictureMinRow, int pictureMaxRow, int pictureMinCol, int pictureMaxCol, bool onlyInternal)
        {
            int _rangeMinRow = rangeMinRow ?? pictureMinRow;
            int _rangeMaxRow = rangeMaxRow ?? pictureMaxRow;
            int _rangeMinCol = rangeMinCol ?? pictureMinCol;
            int _rangeMaxCol = rangeMaxCol ?? pictureMaxCol;

            if (onlyInternal)
            {
                return (_rangeMinRow <= pictureMinRow && _rangeMaxRow >= pictureMaxRow &&
                        _rangeMinCol <= pictureMinCol && _rangeMaxCol >= pictureMaxCol);
            }
            else
            {
                return ((Math.Abs(_rangeMaxRow - _rangeMinRow) + Math.Abs(pictureMaxRow - pictureMinRow) >= Math.Abs(_rangeMaxRow + _rangeMinRow - pictureMaxRow - pictureMinRow)) &&
                (Math.Abs(_rangeMaxCol - _rangeMinCol) + Math.Abs(pictureMaxCol - pictureMinCol) >= Math.Abs(_rangeMaxCol + _rangeMinCol - pictureMaxCol - pictureMinCol)));
            }
        }
    }

其他,本文參考資料已附上參考博文的鏈接,如有冒犯之處,請作者聯繫我,我必將其刪除,謝謝。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章