gpt4 book ai didi

c# - 使用c#将图像插入到excel单元格中

转载 作者:行者123 更新时间:2023-11-30 15:27:51 26 4
gpt4 key购买 nike

我正在从数据表动态生成 Excel 工作表。我可以根据需要将文本添加到不同的单元格中。但是我不知道如何将图片添加到指定范围..

        Excel.Application oApp = new Excel.Application();
oApp.Application.Workbooks.Add(Type.Missing);

oApp.Range["B2", "C4"].Merge(Type.Missing);

在这里我要添加图片..

我正在尝试

        System.Drawing.Image imgg = System.Drawing.Image.FromFile("c:\\D.jpg");

现在我如何将我的 imgg 添加/复制到这个范围内??例如

App.Range["B2", "C4"]

最佳答案

您可以从以下方面受益

using System;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Excel.Application xlApp ;
Excel.Workbook xlWorkBook ;
Excel.Worksheet xlWorkSheet ;
object misValue = System.Reflection.Missing.Value;

xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

//add some text
xlWorkSheet.Cells[1, 1] = "Text1";
xlWorkSheet.Cells[2, 1] = "Text2";

xlWorkSheet.Shapes.AddPicture("C:\\sample.jpg", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 50, 50, 300, 45);


xlWorkBook.SaveAs("MyExcelFile.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();
}
}

}
}

关于c# - 使用c#将图像插入到excel单元格中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26777431/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com