Wie füge ich ein Bild aus der C # -App in Excel ein?
Ich versuche, mit meiner C # -Anwendung ein Bild in eine Excel-Tabelle einzufügen.
Ich habe die folgenden als meine Quelle verwendet.http://csharp.net-informations.com/excel/csharp-insert-picture-excel.htm
Diese ganze Linie ist blau unterstrichen.
xlWorkSheet.Shapes.AddPicture("C:\\pic.JPG", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 50, 50, 300, 45);
Mein Code:
private void btnWriteSpreedSheet_Click(object sender, EventArgs e)
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
//xlWorkSheet.SetBackgroundPicture("C:/Users/Shaun/Documents/Visual Studio 2010/Projects/TestXMLToEXCEL/TestXMLToEXCEL/bin/Debugpic.JPG"); //
//add some text
xlWorkSheet.Cells[1, 1] = "http://csharp.net-informations.com";
xlWorkSheet.Cells[2, 1] = "Adding picture in Excel File";
xlWorkSheet.Shapes.AddPicture("C:\\pic.JPG", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 50, 50, 300, 45); //C:\\csharp-xl-picture.JPG
xlWorkBook.SaveAs("csharp.net-informations.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlApp);
releaseObject(xlWorkBook);
releaseObject(xlWorkSheet);
MessageBox.Show ("File created !");
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Unable to release the Object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
Fehlermeldungen:
Die beste überladene Methodenübereinstimmung für 'Microsoft.Office.Interop.Excel.Shapes.AddPicture (Zeichenfolge, Microsoft.Office.Core.MsoTriState, Microsoft.Office.Core.MsoTriState, float, float, float, float)' hat einige ungültige Argumente
Der Typ "Microsoft.Office.Core.MsoTriState" ist in einer Assembly definiert, auf die nicht verwiesen wird. Sie müssen einen Verweis auf Assembly 'office, Version = 12.0.0.0, Culture = neutral, PublicKeyToken = 71e9bce111e9429c' hinzufügen.
Argument 2: Konvertierung von "Microsoft.Office.Core.MsoTriState [c: \ users \ shaun \ documents \ visual studio 2010 \ Projects \ TestXMLToEXCEL \ TestXMLToEXCEL \ CreateSpreadSheet.cs]" nach "Microsoft.Office.Core.MsoTriState" nicht möglich
Argument 3: Konvertierung von "Microsoft.Office.Core.MsoTriState [c: \ users \ shaun \ documents \ visual studio 2010 \ Projects \ TestXMLToEXCEL \ TestXMLToEXCEL \ CreateSpreadSheet.cs]" nach "Microsoft.Office.Core.MsoTriState" nicht möglich
Meine Referenzen:
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Core;
using Microsoft.Office;
using System.Xml;