0

I am using using OfficeOpenXml; or EPPlus package I'm trying to add an embed image to the excel cell of WPS office ,there are 2 types of images in WPS office (floating image and embed image) I have tried to use DISPIMG equation but the name is not working also add picture make floating image

using System;
using OfficeOpenXml;
using System.IO;
using System.Linq;
using OfficeOpenXml.Drawing;
using System.Collections.Generic;

using System.Windows;


namespace ExcelImageAdder
{
    class Program
    {
        static void Main(string[] args) 
        {
            FileInfo newFile = new FileInfo(@"WpsTest.xlsx");

            using (ExcelPackage package = new ExcelPackage(newFile))
            {
                ExcelWorksheet worksheet = package.Workbook.Worksheets.FirstOrDefault(f => f.View.TabSelected);

                if (worksheet != null)
                {
                    ExcelRangeBase cell = worksheet.Cells[3, 1]; // Cell at row 3, column 1

                    
                    using (FileStream imageStream = new FileStream(@"me.png", FileMode.Open, FileAccess.Read))
                    {
                        string pictureName = "test"+ DateTime.Now.ToString("yyyyMMddHHmmss");

                        ExcelPicture picture = worksheet.Drawings.AddPicture(pictureName, imageStream);
                        Console.WriteLine(picture.Name);
                        WriteTextToCell(worksheet, 1, 1, $"=DISPIMG(\"{picture.Name}\", 1)");
                        package.Save();

                    }
                }
            }
        }
        static void WriteTextToCell(ExcelWorksheet worksheet, int rowIndex, int columnIndex, string text)
        {
            worksheet.Cells[rowIndex, columnIndex].Value = text;
          
        }
    }
}

0 Answers0