欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

EPPlus批量插入图片到Excel

程序员文章站 2024-01-04 17:35:10
#region 测试EPPlus插入图片 public static void Createsheel2() { WebClient client = new WebClient(); var downloadUrl = FileOperater.GetDownloadUrl("f736cf1950 ......

#region 测试EPPlus插入图片
        public static void Createsheel2()
        {          
            WebClient client = new WebClient();
            var downloadUrl = FileOperater.GetDownloadUrl("f736cf1950e64400b3ba69001b0b5a1b");
            var excelData = client.DownloadData(downloadUrl);
            var bytes = client.DownloadData("http://img1.soufunimg.com/message/images/card/tuanproj/201511/2015112703584458_s.jpg");
           
            using (Stream ms = new MemoryStream(excelData))
            {
                ExcelPackage excel;
                try
                {   
                    excel = new ExcelPackage(ms);
                    ExcelWorksheet ws = excel.Workbook.Worksheets[1];
                    ExcelWorkbook wb = excel.Workbook;
                    for (int i = 1; i < 7; i++)
                    {
                        excel.Workbook.Worksheets.Copy(ws.Name, ws.Name+"_"+i);//复制模板sheet
                        var drawings = excel.Workbook.Worksheets[i].Drawings;
                        foreach (ExcelDrawing excelDrawing in drawings)
                        {
                            ExcelPicture excelPicture = excelDrawing as ExcelPicture;
                            var name = excelPicture.Name;
                            //var aa = excelPicture.Image;
                            //var cc = excelPicture.ImageFormat;
                            //excelPicture.Image.Save(imgms, System.Drawing.Imaging.ImageFormat.Jpeg);
                        }
                    }
                    int vSheetCount = excel.Workbook.Worksheets.Count; //获取总Sheet页    
                    for (int i = 1; i <= vSheetCount; i++)
                    {
                        ws = excel.Workbook.Worksheets[i];
                        using (Stream imgms = new MemoryStream(bytes))
                        {
                            System.Drawing.Bitmap myimge = new System.Drawing.Bitmap(imgms);
                            ExcelPicture picture = null;
                            for (int j = 0; j <4; j++)
                            {
                                picture = ws.Drawings.AddPicture("a" + j, myimge);//插入图片
                                if (j >1)
                                {
                                    picture.SetPosition(560, 250 * (j - 2) + (j - 1) * 40);//设置图片的位置
                                }
                                else
                                {
                                    picture.SetPosition(330, 250 * j +(j+1)*40);//设置图片的位置
                                }
                                // picture.SetPosition(100*j, 100*j);//设置图片的位置                            
                                picture.SetSize(250, 220);//设置图片的大小
                            }
                        }
                    }
                    FileOperater.FileUpdate("f736cf1950e64400b3ba69001b0b5a1b", excel.GetAsByteArray());
                }
                catch (Exception ex)
                {
                    throw;
                }
            }             
        }
        #endregion