ASP.net 将Excel转换成图片 并裁剪指定大小

一、将当前Excel文件装换为图片

1.需要引用Spire.Xls dll,可以去网上下载;

ASP.net 将Excel转换成图片 并裁剪指定大小_第1张图片

2.对应方法

public class XLS
    {
        /// 
        /// Excel转图片
        /// 
        /// 文件路径
        /// 保存文件名
        /// 保存文件格式
        /// 
        public static bool ExcelToPicture(string filePath, string saveFileName, ImageFormat imageFormat)
        {
            bool result = false;
            try
            {
                Workbook workbook = new Workbook();
                workbook.LoadFromFile(filePath);
                Worksheet sheet = workbook.Worksheets[0];
                //文件保存路径 C:\Program Files (x86)\IIS Express
                sheet.SaveToImage(saveFileName + "." + imageFormat.ToString());
                result = true;
            }
            catch (Exception ee)
            {
            }
            return result;
        }
        /// 
        /// Excel转图片
        /// 
        /// 首行
        /// 首列
        /// 尾行
        /// 尾列
        /// 文件路径
        /// 保存文件名
        /// 保存文件格式
        /// 
        public static bool ExcelToPicture(int firstRow, int firstColumn, int lastRow, int lastColumn, string filePath, string saveFileName, ImageFormat imageFormat)
        {
            bool result = false;
            try
            {
                Workbook workbook = new Workbook();
                workbook.LoadFromFile(filePath);
                Worksheet sheet = workbook.Worksheets[0];
                //文件保存路径 C:\Program Files (x86)\IIS Express
                sheet.SaveToImage(firstRow, firstColumn, lastRow, lastColumn).Save(saveFileName, ImageFormat.Jpeg);
                result = true;
            }
            catch (Exception ee)
            {
            }
            return result;
        }
        /// 
        /// 图片剪切
        /// 
        /// 源文件路径
        /// 需要裁剪上下边框高度
        /// 裁剪宽度
        /// 图片输出路径
        /// 图片输入格式
        /// 图片输出格式
        /// 
        public static bool CutImage(string filepath, int CutHight, int CutWidth, string thumbnailPath, ImageFormat imageFormatIn, ImageFormat imageFormatOut)
        {
            bool result = false;
            try
            {
                System.Drawing.Image originalImage = System.Drawing.Image.FromFile(filepath);
                int allW = originalImage.Width;
                int allH = originalImage.Height;
                // int oW = allW;
                //int oH = allH;
                int towidth = allW - CutHight;
                int toheight = allH - CutWidth;
                //新建一个bmp图片
                System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
                //新建一个画板
                Graphics g = System.Drawing.Graphics.FromImage(bitmap);
                //设置高质量插值法
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                //设置高质量,低速度呈现平滑程度
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                //清空画布并以透明背景色填充
                g.Clear(Color.Transparent);
                //在指定位置并且按指定大小绘制原图片的指定部分
                g.DrawImage(originalImage, new Rectangle(0, 0, allW, allH), new Rectangle(CutHight, CutHight, towidth, toheight), GraphicsUnit.Pixel);
                //以jpg格式保存缩略图
                string path = thumbnailPath + Path.GetFileNameWithoutExtension(filepath) + "." + imageFormatOut.ToString();
                bitmap.Save(thumbnailPath, imageFormatOut);
                g.Dispose();
                bitmap.Dispose();
                result = true;
            }
            catch (Exception ee)
            {
            }
            return result;
        }
    }

你可能感兴趣的:(ASP.NET)