要在Excel中使用C#插入图片,请遵循以下步骤:
首先,确保已安装Microsoft Office Interop Excel库。可以通过NuGet包管理器或Visual Studio的“工具”>“NuGet包管理器”>“管理解决方案的NuGet程序包”进行安装。搜索"Microsoft.Office.Interop.Excel"并安装。
添加以下命名空间引用到你的代码文件:
using Microsoft.Office.Interop.Excel;
using System.Drawing;
using System.IO;
public void InsertPictureIntoExcel(string filePath, string imagePath)
{
    // 创建一个Excel应用程序实例
    Application excelApp = new Application();
    excelApp.Visible = true;
    // 创建一个新的Excel工作簿
    Workbook workbook = excelApp.Workbooks.Add(Type.Missing);
    Worksheet worksheet = workbook.ActiveSheet;
    // 将图片插入到工作表中
    Image image = Image.FromFile(imagePath);
    Picture picture = worksheet.Pictures().Insert(imagePath);
    // 设置图片位置和大小
    picture.Left = worksheet.Cells[1, 1].Left;
    picture.Top = worksheet.Cells[1, 1].Top;
    picture.Width = image.Width;
    picture.Height = image.Height;
    // 保存工作簿
    workbook.SaveAs(filePath);
    // 关闭工作簿并退出Excel应用程序
    workbook.Close();
    excelApp.Quit();
}
string excelFilePath = @"C:\path\to\your\excel\file.xlsx";
string imagePath = @"C:\path\to\your\image\file.jpg";
InsertPictureIntoExcel(excelFilePath, imagePath);
这将在指定的Excel文件中插入图像。注意,这个示例假设你已经有一个Excel文件,并且希望将图像插入到该文件中。如果你想创建一个新的Excel文件并插入图像,请参考上面的第3步。