Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
303 views
in Technique[技术] by (71.8m points)

c# - Excel sheet in Microsoft excel interop. Save an image to a file

I have installed EPPlus in my c# project and used it to locate the proper image in an Excel worksheet. I want to take the image now and save it as a PNG file.

 FileInfo fi = new FileInfo(@"c:/folder/workbook.xlsx");
 using (ExcelPackage excelPackage = new ExcelPackage(fi))
  {
   ExcelWorksheet ws = excelPackage.Workbook.Worksheets[0];
   int imageCount = firstWorksheet.Drawings.Count;
   for (int i = 0; i <= imageCount-1; i++)
    {
       if (firstWorksheet.Drawings[i].DrawingType.ToString().ToLower() == "picture")
        {
         save it to a file;
        }
    }
  }

Here is what I did that works great. Of course this is a little abbreviated.

OfficeOpenXml.Drawing.ExcelDrawing image = firstWorksheet.Drawings[i]; OfficeOpenXml.Drawing.ExcelPicture p = (OfficeOpenXml.Drawing.ExcelPicture)image; p.Image.Save(@"c:/autocell/becbec.png");

This works with embedded xml files / tables. I also pasted in jpegs and pngs and bitmaps and this code found and saved them just fine too.

question from:https://stackoverflow.com/questions/65879890/excel-sheet-in-microsoft-excel-interop-save-an-image-to-a-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can give the export URL and then it will be shown in the excel file

Get Absolute URL function

private string GetAbsoluteUrl(string relativeUrl)
{
    relativeUrl = relativeUrl.Replace("~/", string.Empty);
    string[] splits = Request.Url.AbsoluteUri.Split('/');
    if (splits.Length >= 2)
    {
        string url = splits[0] + "//";
        for (int i = 2; i < splits.Length - 1; i++)
        {
            url += splits[i];
            url += "/";
        }
 
        return url + relativeUrl;
    }
    return relativeUrl;
}

and save it like this

//Convert the Relative Url to Absolute Url and set it to Image control.

Image1.ImageUrl = this.GetAbsoluteUrl(Image1.ImageUrl);


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...