C#实现使用Excel COM组件向Excel文件中添加图片的Demo

需要添加Microsoft.Office.Interop.Excel;及Microsoft.Office.Core;两个命名空间及对应的引用。
比如我Windows 7下装的Office 2007,是添加的是一个COM组件和一个.Net组件的引用,如下图:
image
而我公司的电脑添加的是Microsoft Excel Object Library 和Microsoft Object Library 。
代码如下:
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Reflection; using System.IO; using Microsoft.Office.Interop.Excel; using Microsoft.Office.Core; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { Microsoft.Office.Interop.Excel.Application app = new Application(); app.Visible = false; app.DisplayAlerts = false; Workbook workBook = app.Workbooks.Open(Server.MapPath("test_ Excel.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, Missing.Value, Missing.Value); //设置为操作当前workBook的sheet1 Worksheet workSheet = (Worksheet)workBook.Worksheets[1]; string imgPath = Server.MapPath("test_Img.jpg"); byte[] bytesArr = PicToByteArr(imgPath); System.Drawing.Image bmp = ReturnPhoto(bytesArr);//图片数据 int x = 1; int y = 1; Range rangeTemp = workSheet.get_Range((Range)workSheet.Cells[x, y], (Range)workSheet.Cells[x, y]); //rangeTemp.Select(); float PicLeft, PicTop; PicLeft = Convert.ToSingle(rangeTemp.Left) + 2; PicTop = Convert.ToSingle(rangeTemp.Top) + 1; workSheet.Shapes.AddPicture(imgPath, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, PicLeft, PicTop, bmp.Width * 8 / 10, bmp.Height * 8 / 10); workBook.Save(); workBook.Close(null, null, null); app.Workbooks.Close(); app.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(rangeTemp); System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet); System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook); System.Runtime.InteropServices.Marshal.ReleaseComObject(app); System.GC.Collect(); System.GC.WaitForPendingFinalizers(); } /// summary /// 将图片转换为字节数组 /// /summary /// param name="path"图片的路径/param /// returns字节数组/returns public byte[] PicToByteArr(string path) { FileStream fs = new FileStream(path, FileMode.Open);//将图片写入流中。 int filelength = 0; filelength = (int)fs.Length; //获得文件长度 Byte[] byteArr = new Byte[filelength]; //建立一个字节数组 fs.Read(byteArr, 0, filelength); //按字节流读取 fs.Close(); return byteArr; } /// summary /// 参数是byte返回图片 /// /summary /// param name="byteArr"字节数组/param /// returns图片/returns public System.Drawing.Image ReturnPhoto(byte[] byteArr) { MemoryStream ms = new MemoryStream(byteArr); System.Drawing.Image img = System.Drawing.Image.FromStream(ms); ms.Close(); return img; } }

  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值