10

我正在尝试使用我的 C# 应用程序将图片插入 Excel 电子表格。

我使用以下内容作为我的来源。http://csharp.net-informations.com/excel/csharp-insert-picture-excel.htm

整条线用蓝色下划线。

 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();
    }
}

错误信息:

'Microsoft.Office.Interop.Excel.Shapes.AddPicture(string, Microsoft.Office.Core.MsoTriState, Microsoft.Office.Core.MsoTriState, float, float, float, float)' 的最佳重载方法匹配有一些无效参数

类型“Microsoft.Office.Core.MsoTriState”在未引用的程序集中定义。您必须添加对程序集“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'

参数 3:无法从 'Microsoft.Office.Core.MsoTriState [c:\users\shaun\documents\visual studio 2010\Projects\TestXMLToEXCEL\TestXMLToEXCEL\CreateSpreadSheet.cs]' 转换为 'Microsoft.Office.Core.MsoTriState'


我的参考资料:

using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Core;
using Microsoft.Office;
using System.Xml;
4

6 回答 6

18

添加以下引用:

  • Microsoft.Office.Interop.Excel从 .Net 选项卡
  • Microsoft Office 14.0 Object Library从 COM 选项卡

添加以下 using 语句:

using Microsoft.Office.Core;
using Excel = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;

然后这是您的方法(略有改动):

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 !");
}
于 2012-09-25T08:12:46.377 回答
2

您需要添加 Microsoft excel 库。

在此处输入图像描述

于 2012-07-30T08:57:22.217 回答
2

作为替代方案,您可以使用其中一个 Open Xml 库(例如EPPlus)来执行此操作。

在我看来,EPPlus 比 Excel 互操作更容易、更直观,无需手动释放资源。它还有一个额外的好处,那就是它可以在没有安装 Excel 的机器上执行。

像这样简单的 EPPlus 效果很好:

using (var excel = new ExcelPackage())
{
    var wks = excel.Workbook.Worksheets.Add("Sheet1");
    wks.Cells[1, 1].Value = "Adding picture below:";
    var pic = wks.Drawings.AddPicture("MyPhoto", new FileInfo("image.png"));
    pic.SetPosition(2, 0, 1, 0);
    excel.SaveAs(new FileInfo("outputfile.xlsx"));
}
于 2017-10-19T12:03:33.627 回答
1

只需添加

using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Core;

到您的代码并在再次运行之前重建您的解决方案。

于 2012-09-25T06:46:03.380 回答
1

我将此代码与 Microsoft.Office.Interop.Excel v 14.0 一起使用:

xlWorksheet.Shapes.AddPicture(@"c:\pics\logosmall.jpg", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 0, 0, 185, 42);

并且在我的 excel 表格中有图像(jpg和)。png

于 2015-12-11T17:09:15.923 回答
0
    private void Button1_Click(object sender, EventArgs e)
    {

        Microsoft.Office.Interop.Excel._Application application = new Microsoft.Office.Interop.Excel.Application();
        Microsoft.Office.Interop.Excel._Workbook workbook = application.Workbooks.Add(Type.Missing);
        Microsoft.Office.Interop.Excel._Worksheet worksheet = null;

        SaveFileDialog saveDialog = new SaveFileDialog();
        saveDialog.FileName = "ImageToExcel.xlsx";
        if (saveDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            try
            {
                worksheet = workbook.ActiveSheet;
                worksheet.Name = "Image to Excel";
                string imageNetworkLocation = "\\\\192.168.240.110\\images\\image.png";
                worksheet.Shapes.AddPicture(imageNetworkLocation, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 0, 0, 170, 85);
                workbook.SaveAs(saveDialog.FileName);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            finally
            {
                application.Quit();
                workbook = null;
                application = null;

            }
        }

        else
            try
            {
                workbook.Close(0);
                application.Quit();
                workbook = null;
                application = null;
                System.Runtime.InteropServices.Marshal.ReleaseComObject(application);
            }
            catch (System.Exception ex) 
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
    }
于 2019-10-05T05:30:00.010 回答