0

i am doing a WPF application to import my image from my folder into excel using c#. I had been successful with using microsoft excel but i would like to know if it is possible to view the file if the user does not have microsoft excel installed? Below is the code which i tried converting the image to a String but it does not display in the 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
            }
        }
    }
  • possible duplicate of [How to insert a picture in to Excel from C# app?](http://stackoverflow.com/questions/11716873/how-to-insert-a-picture-in-to-excel-from-c-sharp-app) – Eugene Podskal Jul 01 '14 at 09:00
  • Hi, i am not wanting microsoft excel – user262942 Jul 01 '14 at 09:02
  • I am lost as to how i can display a image in excel if the user's computer does not have MS excel. – user262942 Jul 01 '14 at 09:04
  • You can create Excel files with some recommended non-Interop mechanics - http://stackoverflow.com/questions/151005/create-excel-xls-and-xlsx-file-from-c-sharp. But how the user will open the resulting excel file after the creation is another problem. – Eugene Podskal Jul 01 '14 at 09:10
  • i can create excel file without microsoft excel installed but i cant insert the image into excel dynamically....instead, what is being inserted is the file path uri for image – user262942 Jul 01 '14 at 09:18
  • You wrote You was able to insert image into Excel. Have You done it just by opening Excel application and now want to insert the image from Your C# code or is this another issue? Your code uses OpenXML to operate on Excel file and it should allow You to insert image as well (even if user does not have Excel installed). But user will still need Excel (or some kind of **.xlsx* files viewer) to display the file. – Lukasz M Jul 01 '14 at 10:33

1 Answers1

0

So if the user has Excel the image will show. Make it so that if the image does not show (there is no Excel) the image opens in something else like pain, or whatever program you want the image to be in.