C#实现使用Excel COM组件向Excel文件中添加图片的Demo
2013-01-07 09:34
766 查看
需要添加Microsoft.Office.Interop.Excel;及Microsoft.Office.Core;两个命名空间及对应的引用。
比如我Windows 7下装的Office 2007,是添加的是一个COM组件和一个.Net组件的引用,如下图:
![](https://oscdn.geek-share.com/Uploads/Images/Content/201910/23/60f385599bc70f56ccf0dcd9d91fef89.png)
而我公司的电脑添加的是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; } }
比如我Windows 7下装的Office 2007,是添加的是一个COM组件和一个.Net组件的引用,如下图:
![](https://oscdn.geek-share.com/Uploads/Images/Content/201910/23/60f385599bc70f56ccf0dcd9d91fef89.png)
而我公司的电脑添加的是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; } }
相关文章推荐
- C#实现使用Excel COM组件向Excel文件中添加图片的Demo
- C#实现PDF文件添加图片背景
- 如何使用C#在PDF文件添加图片印章
- C#实现使用form上传Excel文件,导入Excel数据到SQL SERVER数据库
- 本文使用c#通过oledb可获取excel文件表的结构信息,同理也可以获得access等数据库的表结构信息,实现如下:
- ADO.NET .net core2.0添加json文件并转化成类注入控制器使用 简单了解 iTextSharp实现HTML to PDF ASP.NET MVC 中 Autofac依赖注入DI 控制反转IOC 了解一下 C# AutoMapper 了解一下
- JPEG图片文件格式浅析,以及使用C#实现图片的解析
- c#使用 主题文件实现多界面
- 关于使用DataGrid的ButtonColumn,动态添加DataGrid列,实现不定列n个文件的下载功能
- C#打开Excel文件的后缀名els与elsx的使用
- 在 Laravel 5 中使用 Laravel Excel 实现 Excel/CSV 文件导入导出功能
- Laravel 5使用Laravel Excel实现Excel/CSV文件导入导出的功能详解
- Iterator<FileItem>的使用,实现文件(图片)上传,限制图片格式,设置随机文件名
- C#项目中操作Excel文件——使用NPOI库
- C# 对Word文件添加水印图片
- C#使用文件监控对象FileSystemWatcher 实现数据同步
- iOS开发-使用NSURLSession实现文件断点下载,文件离线续传以及图片上传
- iOS开发-使用AFN实现GET/POST请求,以及文件下载/文件(图片)上传
- C#读取excel文件的内容(使用DataSet)
- C# 使用 ExcelLibrary 读写 Excel 文件