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
}
}
}