/// <summary>
/// 在 Excel 文件中插入图片
/// </summary>
/// <param name="path">Excel 文件路径</param>
/// <param name="fileName">需要修改的工作表名称</param>
public static void SetImg(string path, string fileName)
{
Microsoft.Office.Interop.Excel.Application m_Excel = new Microsoft.Office.Interop.Excel.Application();//创建一个Excel对象(同时启动EXCEL.EXE进程)
m_Excel.SheetsInNewWorkbook = 1;//工作表的个数
//打開工作簿
Microsoft.Office.Interop.Excel._Workbook m_Book = m_Excel.Workbooks._Open("E://test//File//" + path + ".xls"
, Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
Microsoft.Office.Interop.Excel._Worksheet m_Sheet = (Microsoft.Office.Interop.Excel._Worksheet)m_Book.Worksheets[fileName];
//插入图片
m_Sheet.Shapes.AddPicture(@"E:/test/File/test.jpg", MsoTriState.msoFalse, MsoTriState.msoTrue, 80, 10, 320, 350);
//将新 Excel 文件复制到新文件夹中
m_Book.SaveCopyAs("E://test//NewFile//" + path + ".xls");
//关闭工作簿时,第一个参数为 true 时保存文件
m_Book.Close(false, Missing.Value, Missing.Value);
m_Excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_Book);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_Excel);
m_Book = null;
m_Sheet = null;
m_Excel = null;
GC.Collect();
}
在 Excel 文件中插入图片
最新推荐文章于 2022-08-18 11:07:43 发布