【问题标题】:How to add an image from URL to Excel Worksheet via C#如何通过 C# 将 URL 中的图像添加到 Excel 工作表
【发布时间】:2015-12-04 19:28:13
【问题描述】:

这是目标:

1) 从 URL 获取图片,在本例中为 Google Static Maps API

2) 将此图像插入 Excel 工作表。如果我必须创建(或使用现有的)形状并将背景设置为图像,我可以。我也可以插入特定的单元格。我可以通过 Google Static Maps API(请参阅上面的 URL)定义图像大小,以便始终知道它。


我不完全清楚如何在不先将文件直接保存到文件系统的情况下执行此操作。

我目前有这样的代码,它以 MemoryStream 格式获取图像:

public static MemoryStream GetStaticMapMemoryStream(string requestUrl, string strFileLocation)
{
    try
    {
        HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            if (response.StatusCode != HttpStatusCode.OK)
                throw new Exception(String.Format(
                "Server error (HTTP {0}: {1}).",
                response.StatusCode,
                response.StatusDescription));

            using (BinaryReader reader = new BinaryReader(response.GetResponseStream()))
            {
                Byte[] lnByte = reader.ReadBytes(1 * 700 * 500 * 10);
                using (FileStream lxFS = new FileStream(strFileLocation, FileMode.Create))
                {
                    lxFS.Write(lnByte, 0, lnByte.Length);
                }

                MemoryStream msNew = new MemoryStream();
                msNew.Write(lnByte, 0, lnByte.Length);
                return msNew;
            }
        }
    }
    catch (Exception e)
    {
        System.Windows.Forms.MessageBox.Show(e.Message);
        return null;
    }

}

请注意,在上述代码的中间,我也将图像写入文件系统。如果可能的话,我想避免这部分。

无论如何,我的代码可以创建一个矩形,调用上面保存图像的序列,然后抓取图像并填充矩形的背景:

Excel.Shape shapeStaticMap = wsNew2.Shapes.AddShape(Office.MsoAutoShapeType.msoShapeRectangle, 0, 0, 700, 500);

string strFileLocation = @"C:\Temp\test.jpg";

MemoryStream newMS = GetStaticMapMemoryStream(strStaticMapUrl, strFileLocation);

shapeStaticMap.Fill.UserPicture(strFileLocation);

所以这里真正的问题是我想来回跳过“写入文件然后从文件中抓取”。这似乎是一个不必要的步骤,而且我预计它也会因为文件权限而变得混乱。

更新

好的,所以我基本上放弃了并使用本地文件离开了它。这工作了一段时间,但现在我正在尝试重新编写此代码以从我提前知道图像大小的不同来源获取图像。上面的方法需要我提前知道图片的SIZE。如何修改上面的代码以动态使用任何图像大小?

【问题讨论】:

  • 将图像直接下载到最终位置或首先下载到临时/中间位置的事实不会产生任何明显的差异。大的时间/资源交易是下载过程本身(或者,在这种情况下,也将图像添加到 Excel);中间变量/位置无关紧要。此外,您还有什么其他选择?将MemoryStream 直接输入 Excel?! (这似乎不是一个好主意;如果可能的话,我不确定)。在中间存储方面,您的代码现在看起来还不错。
  • @varocarbas - 好的,所以我放弃并保持原样,但正如您在我最近的更新中看到的那样,如果我不这样做,我不确定如何处理可变性提前知道图像大小...有什么想法吗?

标签: c# excel google-maps-api-3 memorystream


【解决方案1】:

使用此版本的 GetStaticMapMemoryStream:

    public static MemoryStream GetStaticMapMemoryStream(string requestUrl)
    {
        try
        {
            HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                if (response.StatusCode != HttpStatusCode.OK)
                    throw new Exception(String.Format(
                    "Server error (HTTP {0}: {1}).",
                    response.StatusCode,
                    response.StatusDescription));

                var responseStream = response.GetResponseStream();
                var memoryStream = new MemoryStream();

                responseStream.CopyTo(memoryStream);
                memoryStream.Position = 0;

                return memoryStream;
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            return null;
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    • 2017-01-08
    • 1970-01-01
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    • 2012-08-27
    相关资源
    最近更新 更多