【问题标题】:How to display image in Excel如何在Excel中显示图像
【发布时间】:2014-07-01 17:11:34
【问题描述】:

我正在做一个 WPF 应用程序,使用 c# 将我的图像从我的文件夹导入到 excel 中。我已经成功使用了 microsoft excel,但我想知道如果用户没有安装 microsoft excel,是否可以查看该文件?下面是我尝试将图像转换为字符串但它没有显示在 excel 中的代码。

using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    using ExcelOpenXMLBasics;
    //----------------------------------
    using System.Configuration;
    using System.Data.SqlClient;
    using System.Data;
    using Microsoft.Win32;
    //----------------------------------
    namespace WpfApplication2
    {
        public partial class Window2 : Window
        {
            List<String> stringValue;
            List<int> intValue;
            string Imagefile;

            public Window2()
            {
                InitializeComponent();

                System.Threading.Thread.CurrentThread.CurrentUICulture = System.Threading.Thread.CurrentThread.CurrentCulture;
                System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
            }

            private void btnCreateBasicWorkbook_Click(object sender, RoutedEventArgs e)
            {
                this.CreateBasicWorkbook("try7.xlsx", true);
            }

            private void CreateBasicWorkbook(string workbookName, bool createStylesInCode)
            {
                #region select file
                OpenFileDialog fileChooser = new OpenFileDialog();
                fileChooser.Filter = "image files (*.png)|*.png|All files (*.*)|*.*";
                fileChooser.InitialDirectory = @"C:\";
                fileChooser.Title = "Check if the image has been saved into PC disk";
                bool? result = fileChooser.ShowDialog();
                Imagefile = fileChooser.FileName;
                #endregion

                #region 1
                DocumentFormat.OpenXml.Packaging.SpreadsheetDocument spreadsheet;
                DocumentFormat.OpenXml.Spreadsheet.Worksheet worksheet;
                System.IO.StreamReader styleXmlReader;
                string styleXml;

                spreadsheet = Excel.CreateWorkbook(workbookName);
                if (spreadsheet == null)
                {
                    return;
                }

                if (createStylesInCode)
                {
                    Excel.AddBasicStyles(spreadsheet);
                }
                else
                {
                    using (styleXmlReader = new System.IO.StreamReader("PredefinedStyles.xml"))
                    {
                        styleXml = styleXmlReader.ReadToEnd();
                        Excel.AddPredefinedStyles(spreadsheet, styleXml);
                    }
                }

                Excel.AddWorksheet(spreadsheet, "Test 1");
                worksheet = spreadsheet.WorkbookPart.WorksheetParts.First().Worksheet; 
                #endregion



                //BitmapImage _Image = new BitmapImage(new Uri(@"C:\Users\ifcdu1\Desktop\limin.PNG"));
                //string _img = _Image.ToString();
                //@"C:\Users\ifcdu1\Desktop\limin.PNG"
                //byte imageArray1 = File.ReadAllBytes(@"C:\Users\ifcdu1\Desktop\limin.PNG");
                byte[] imageArray = System.IO.File.ReadAllBytes(Imagefile);
                string base64ImageRepresentation = Convert.ToBase64String(imageArray);
                //string test = Imagefile;
                //byte[] bytes = System.Text.ASCIIEncoding.ASCII.GetBytes(test);
                //string base64String = System.Convert.ToBase64String(bytes);
                //Console.WriteLine("Base 64 string: " + base64String);
                Excel.SetCellValue(spreadsheet, worksheet, 1, 1, base64ImageRepresentation, true);

                #region 2
                worksheet.Save();
                spreadsheet.Close();
                System.Diagnostics.Process.Start(workbookName);
                #endregion
            }
        }
    }

【问题讨论】:

  • 嗨,我不想要 microsoft excel
  • 如果用户的计算机没有 MS excel,我不知道如何在 excel 中显示图像。
  • 您可以使用一些推荐的非互操作机制创建 Excel 文件 - stackoverflow.com/questions/151005/…。但是创建后用户如何打开生成的excel文件是另一个问题。
  • 我可以在没有安装 microsoft excel 的情况下创建 excel 文件,但我无法将图像动态插入到 excel 中......相反,插入的是图像的文件路径 uri

标签: c# wpf excel


【解决方案1】:

因此,如果用户拥有 Excel,则图像将显示。这样做是为了如果图像不显示(没有 Excel),图像会以其他方式打开,例如疼痛,或者您希望图像进入的任何程序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 2011-11-16
    • 1970-01-01
    • 2017-04-06
    • 2013-01-22
    • 2012-06-28
    • 2015-03-09
    相关资源
    最近更新 更多