+-

我正在从数据表中生成一个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#将图像插入Excel单元格 - 乐贴网