Excel转换成图片的操作方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EXCEL = Microsoft.Office.Interop.Excel;
using System.Windows.Forms;
using System.Drawing;
using System.IO;

namespace FormatApplication.CommonClass
{
    /// <summary>
    /// EXCEL文档JPG图片
    /// </summary>
    public class ConvertEXCEL
    {
        /// <summary>
        /// 图片保存路径。System.IO.Path.GetTempPath() + @"cache\{0}.jpg"
        /// </summary>
        private static string SAVEEXCELJPG = @"E:\cheng_Text\FormatApplication\测试文件\{0}.jpg";

        /// <summary>
        /// 暂时网页保存文件路径。
        /// Path.GetTempPath() + @"cache\{0}.htm"
        /// </summary>
        private static string TEMPHTMLPATH = @"E:\cheng_Text\FormatApplication\测试文件\Excel.htm";

        /// <summary>
        /// Excel文档转换成JPG图片文件。
        /// </summary>
        /// <param name="excelPath">Excel文件路径</param>
        /// <returns>JPG图片路径</returns>
        public static string EXCELConvertImage(string excelPath)
        {
            EXCEL.Application appExcel = new Microsoft.Office.Interop.Excel.Application();
            appExcel.Visible = false;
            object obj = Type.Missing;
            object fileHtml = TEMPHTMLPATH;
            object format = EXCEL.XlFileFormat.xlHtml;
            EXCEL.Workbook xls = appExcel.Workbooks.Open(excelPath, obj, obj, obj, obj, obj, obj, obj, obj, obj, obj, obj, obj, obj, obj);

            xls.SaveAs(fileHtml, format, obj, obj, obj, obj, EXCEL.XlSaveAsAccessMode.xlExclusive, obj, obj, obj, obj, obj);
            appExcel.Quit();
            string HTMLImage = ConvertHTML.HTMLConvertImage(fileHtml.ToString());
            DeleteTempFile(fileHtml.ToString());
            return HTMLImage;
        }

        public static string GetExcel(string excelFilePath)
        {
            EXCEL.Application app = new Microsoft.Office.Interop.Excel.Application();
            object objMis = Type.Missing;
            EXCEL.Workbook singleExcel = app.Workbooks.Open(excelFilePath, objMis, objMis, objMis, objMis, objMis, objMis, objMis, objMis, objMis, objMis, objMis, objMis, objMis, objMis);
            try
            {
                //wsheet.UsedRange.Select();
                for (int i = 1; i <= singleExcel.Worksheets.Count; i++)
                {
                    EXCEL.Worksheet wsheet = (EXCEL.Worksheet)singleExcel.Worksheets[i];

                    //Clipboard.Clear();

                    object ranobj = DBNull.Value;

                    //设置选择单元格,在复制出来。Excel转换成图片的操作方法。
                    wsheet.get_Range("A1", "D3").Copy(ranobj);

                    //全选单元格,全部复制出来。Excel转换成图片的操作方法。
                    //wsheet.UsedRange.Copy(objMis);
                    //Clipboard.SetDataObject(objMis);
                    IDataObject iData = Clipboard.GetDataObject();
                    Bitmap bits = (Bitmap)iData.GetData(DataFormats.Bitmap);
                    Bitmap myBitmap = new Bitmap(bits.Width, bits.Height);
                    Graphics g = Graphics.FromImage(myBitmap);
                    g.DrawImage(bits, 0, 0);
                    myBitmap.Save(string.Format(SAVEEXCELJPG, Guid.NewGuid()));

                    Clipboard.Clear();
                    myBitmap.Dispose();
                    bits.Dispose();
                }

            }
            catch (Exception Excel)
            {
                throw Excel;
            }
            finally
            {
                singleExcel.Close(objMis, objMis, objMis);
                app.Quit();
            }
            return string.Empty;
        }

        /// <summary>
        /// 删除指定的临时文件。
        /// </summary>
        /// <param name="filePath"></param>
        private static void DeleteTempFile(string filePath)
        {
            File.Delete(filePath);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值