Download Image for Excel

0 thích 0 không thích
1 lượt xem
đã hỏi 3 Tháng 5, 2023 bởi nguyenthao (9,000 điểm)
namespace DemoAddinExcel
{
    public static class MyFunctions
    {
        [ExcelFunction(Category = "GetImageFromURL", Description = "Tải hình ảnh từ website vào Excel")]
        public static  void GETIMAGE(string url, [ExcelArgument(AllowReference = true)] object arg)
        {
            ExcelReference theRef = (ExcelReference)arg;
           
            Excel.Application xlapp = (Excel.Application)ExcelDnaUtil.Application;
            Workbook wbook = xlapp.ActiveWorkbook;
            Worksheet worksheet = wbook.ActiveSheet as Worksheet;
            var cell = wbook.ActiveSheet.Cells[theRef.RowFirst + 1, theRef.ColumnFirst + 1];
         

            var imageStreamAysnc = DownloadAsync(url);
          
               
                string tempFilename = Path.GetTempFileName();
                 File.WriteAllBytes(tempFilename, (byte[])imageStreamAysnc);
                cell.Worksheet.Shapes.AddPicture(tempFilename, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, cell.Left, cell.Top, cell.Width, cell.Height);            
                File.Delete(tempFilename);
           ;
          
        }

        public static object DownloadAsync(string url)
        {
            // Don't do anything else here - might run at unexpected times...
            return ExcelAsyncUtil.Run("DownloadAsync", url,
                delegate { return Download(url); });
        }

        static byte[] Download(string url)
        {
            var data = new WebClient().DownloadData(url);
            return data;
        }

    }

}

    

2 Câu trả lời

0 thích 0 không thích
đã trả lời 3 Tháng 5, 2023 bởi nguyenthao (9,000 điểm)
Đã test ok. good job
0 thích 0 không thích
đã trả lời 3 Tháng 5, 2023 bởi nguyenthao (9,000 điểm)
XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.PreserveWhitespace = true;
        xmlDoc.Load("ENVIO_DTE_345508.xml");
        SignedXml signedXml = new SignedXml(xmlDoc);
        XmlNodeList nodeList = xmlDoc.GetElementsByTagName("Signature");
        XmlNodeList certificates = xmlDoc.GetElementsByTagName("X509Certificate");
        X509Certificate2 dcert2 = new X509Certificate2(Convert.FromBase64String(certificates[0].InnerText));
        foreach (XmlElement element in nodeList) {
            signedXml.LoadXml(element);
            bool passes = signedXml.CheckSignature(dcert2, true);
        }
...