如何从C#应用程序插入图片到Excel? [英] How to insert a picture in to Excel from C# app?
问题描述
我正在使用我的C#应用程序将图片插入Excel Spread Sheet。
I am trying to insert a picture into Excel Spread Sheet using my C# application.
我已经使用以下作为我的来源。 http://csharp.net-informations.com/excel/ csharp-insert-picture-excel.htm
I have used the following as my source. http://csharp.net-informations.com/excel/csharp-insert-picture-excel.htm
这整行用蓝色加下划线。
This whole line is underlined in blue.
xlWorkSheet.Shapes.AddPicture("C:\\pic.JPG", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 50, 50, 300, 45);
我的代码:
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();
}
}
错误消息:
Error messages:
最好的重载方法匹配
'Microsoft.Office.Interop.Excel.Shapes.AddPicture(string,
Microsoft.Office.Core.MsoTriState,Microsoft.Office.Core.MsoTriState,
float,float,float,float)'有一些无效的参数
The best overloaded method match for 'Microsoft.Office.Interop.Excel.Shapes.AddPicture(string, Microsoft.Office.Core.MsoTriState, Microsoft.Office.Core.MsoTriState, float, float, float, float)' has some invalid arguments
类型'Microsoft.Office.Core.MsoTriState'在未引用的程序集
中定义。您必须添加对程序集
Version = 12.0.0.0,Culture = neutral,PublicKeyToken = 71e9bce111e9429c的引用。
The type 'Microsoft.Office.Core.MsoTriState' is defined in an assembly that is not referenced. You must add a reference to assembly 'office, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'.
参数2:无法转换'Microsoft.Office.Core.MsoTriState
[c:\users\shaun\documents\visual studio
2010 \Projects\TestXMLToEXCEL\TestXMLToEXCEL\CreateSpreadSheet.cs]'
'Microsoft.Office.Core.MsoTriState'
Argument 2: cannot convert from 'Microsoft.Office.Core.MsoTriState [c:\users\shaun\documents\visual studio 2010\Projects\TestXMLToEXCEL\TestXMLToEXCEL\CreateSpreadSheet.cs]' to 'Microsoft.Office.Core.MsoTriState'
参数3:无法从Microsoft.Office.Core.MsoTriState
[c:\\ \\users\shaun\documents\visual studio
2010 \Projects\TestXMLToEXCEL\TestXMLToEXCEL\CreateSpreadSheet.cs]'到
'Microsoft.Office.Core.MsoTriState'
Argument 3: cannot convert from 'Microsoft.Office.Core.MsoTriState [c:\users\shaun\documents\visual studio 2010\Projects\TestXMLToEXCEL\TestXMLToEXCEL\CreateSpreadSheet.cs]' to 'Microsoft.Office.Core.MsoTriState'
我的参考文献:
My References:
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Core;
using Microsoft.Office;
using System.Xml;
推荐答案
添加以下参考文献:
-
.Net选项卡中的Microsoft.Office.Interop.Excel
-
COM选项卡中的Microsoft Office 14.0对象库
添加以下内容使用语句:
Add the following using statements:
using Microsoft.Office.Core;
using Excel = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
然后这里是你的方法(稍微改变):
And then here is your method (slightly altered):
private void BtnWriteSpreedSheetClick(object sender, EventArgs e)
{
var xlApp = new Excel.Application();
Excel.Workbook xlWorkBook = xlApp.Workbooks.Add();
Excel.Worksheet xlWorkSheet = xlWorkBook.Sheets[1];
xlWorkSheet.Cells[1, 1] = "http://csharp.net-informations.com";
xlWorkSheet.Cells[2, 1] = "Adding picture in Excel File";
xlWorkSheet.Shapes.AddPicture(@"C:\pic.JPG", MsoTriState.msoFalse, MsoTriState.msoCTrue, 50, 50, 300, 45);
xlWorkBook.SaveAs("csharp.net-informations.xls", Excel.XlFileFormat.xlWorkbookNormal);
xlWorkBook.Close(true);
xlApp.Quit();
Marshal.ReleaseComObject(xlApp);
MessageBox.Show("File created !");
}
这篇关于如何从C#应用程序插入图片到Excel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!