博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Excel转换成图片的操作方法
阅读量:5900 次
发布时间:2019-06-19

本文共 2773 字,大约阅读时间需要 9 分钟。

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);
        }
    }
}

转载地址:http://gehsx.baihongyu.com/

你可能感兴趣的文章
Linux输入输出管理
查看>>
zabbix监控系统的配置
查看>>
SQL Server Management Studio(SSMS)修复来解决SSMS找不到的问题
查看>>
oracle中时间处理
查看>>
配置Java EE Eclipse+Tomcat开发环境
查看>>
SSH免密码登录的方法
查看>>
textkit
查看>>
Spring MVC中前后台数据传输小结
查看>>
Android详细的对话框AlertDialog.Builder使用方法
查看>>
2594 解药还是毒药
查看>>
Spring中使用@Profile指定不同的环境
查看>>
《精进:如何成为一个很厉害的人》读书笔记(转载)
查看>>
linux下修改/etc/profile文件
查看>>
cropper实现图片剪切上传
查看>>
谈谈java的BlockingQueue
查看>>
java 读excel xlsx
查看>>
20165313 我期望的师生关系
查看>>
CentOS7+CDH5.14.0安装CDH错误排查: HiveServer2 该角色的进程已退出。该角色的预期状态为已启动...
查看>>
GCC
查看>>
The Oregon Trail 俄勒冈之旅
查看>>