c# Aspose 图片处理(将Excel单元格中的图片拷贝到另一个Excel文件中)

来源:互联网 发布:pe我的世界矿物追踪js 编辑:程序博客网 时间:2024/05/16 07:41

Aspose是一个很强大的控件,可以用来操作word,excel,ppt等文件,用这个控件来导入、导出数据非常方便。其中Aspose.Cells就是用来操作Excel的,功能有很多;我们接下来用来实现一个“将指定的XLS文件中、指定页签下的、指定单元格内的、图片拷贝到另一个XLS中的相应单元格内”的功能;

首先,需要添加引用Aspose.Cells.dll,官网下载地址:http://downloads.aspose.com/cells/net

实现代码如下,具体含义请看代码注释:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using Aspose.Cells;        //新增的用于处理Excel表格的引用using Aspose.Cells.Drawing;using Aspose.Cells.Rendering;using System.IO;namespace Demo{    class Program    {        // 将指定的XLS文件中、指定页签下的、指定单元格内的、图片拷贝到另一个XLS中的相应单元格内        private void GetInfoBySheetName(string fileName1, string fileName2, string XlsSheetName)        {            Workbook m_XlsFileReader1 = new Workbook(FileName1);    // xls文件1            Worksheet CurSheet1 = m_XlsFileReader1.Worksheets[XlsSheetName];   // xls文件中指定的页签            Workbook m_XlsFileReader2 = new Workbook(FileName2);    // xls文件1            Worksheet CurSheet2 = m_XlsFileReader1.Worksheets[XlsSheetName];   // xls文件中指定的页签            int row = 5;       // 行、列            int column = 5;            MemoryStream mstream = null;  // 内存流            string name = "图标";  // 图片名            string path = "";     //保存后的文件路径            double rowHeight = 13.5;  //单元格行高            // 遍历文件1当前页中的所有图片,并找到指定图片进行保存            for (int i = 0; i < CurSheet1.Pictures.Count; i++)             {                // 找到指定单元格的图片                if (CurSheet1.Pictures[i].UpperLeftRow == row && CurSheet1.Pictures[i].UpperLeftColumn == column)                {                    rowHeight = CurSheet1.Cells.GetRowHeight(row);     // 获取单元格行高                    SaveToStream(CurSheet1.Pictures[i], ref mstream);  // 保存成内存流                     SaveImagePath(CurSheet1.Pictures[i], name);       // 保存成图片                    continue;                }            }            // 将图片拷贝到文件2指定页签下的指定位置            CurSheet2.Cells.SetRowHeight(row, rowHeight);   // 设置单元格行高            CurSheet2.Pictures.Add(row, column, mstream);  // 以流的方式进行拷贝            CurSheet2.Pictures.Add(row, column, path);    // 以文件路径的方式进行拷贝        }        // 保存成图片        private void SaveImagePath(Picture pic, string name, ref string path)        {            string picFormat = pic.ImageFormat.ToString();            picFormat = picFormat.ToLower();            ImageOrPrintOptions printOption = new ImageOrPrintOptions();  //图片格式            printOption.ImageFormat = pic.ImageFormat;            string filePath = "D:\\新增资源表图标\\" + name + "." + picFormat;            pic.ToImage(filePath, printOption);   // 保存(参数为:文件路径名和图片格式)            path = filePath;        }        // 保存成内存流         private void SaveToStream(Picture pic, ref MemoryStream mstream)        {            ImageOrPrintOptions printOption = new ImageOrPrintOptions(); //图片格式            printOption.ImageFormat = pic.ImageFormat;            mstream = new MemoryStream();            pic.ToImage(mstream, printOption);  // 保存(参数为:内存流和图片格式)        }        public static void Main()        {            Program program = new Program();            string file1 = "D:\\1.xlsx";            string file2 = "D:\\2.xlsx";            string sheetName = "图标";            program.GetInfoBySheetName(file1, file2, sheetName);            Console.ReadKey();        }    }}



阅读全文
0 0