• Excel转Jpg(Microsoft.Office.Interop.Excel)


    View Code
    /*
     *Made By Anby 
     * 2012-10.31
     * Good Luck!~
     * Excel转换jpg
     * 记得引用Microsoft.Office.Interop.Excel;
     */
    using System.Drawing;
    using System.Linq;
    using System.Windows.Forms;
    using EXCEL = Microsoft.Office.Interop.Excel;
    using System;
    using System.Diagnostics;
    using System.IO;
    
    namespace ExcelConvertHelper
    { 
        class ExcelToImage
        {
    
            /// <summary>
            /// 打开Excel文件
            /// </summary>
            /// <returns></returns>
            public static string  OpenExcelFile()
            {
                OpenFileDialog opf = new OpenFileDialog();
                string excelFilePath = "";
                opf.Filter = "Excel文件(*.xls)|*.xls";
                opf.FilterIndex = 1;
                if (opf.ShowDialog() == DialogResult.OK)
                {
                    excelFilePath = opf.FileName;
                }
                else
                {
                    excelFilePath = "";
                }
                return excelFilePath;
            }
    
            public static string SaveExcelFile()
            {
                FolderBrowserDialog fbd = new FolderBrowserDialog();
                string excelSavePath = "";
                if (fbd.ShowDialog() == DialogResult.OK)
                {
                    excelSavePath = fbd.SelectedPath;
                }
                else
                {
                    excelSavePath = "";
                }
                return excelSavePath;
            }
    
    
    
            /// <summary>
            /// 结束打开的Excel进程
            /// </summary>
            public static  void KillProgram()
            {
                foreach (Process process in Process.GetProcesses())
                {
                    if (process.ProcessName == "EXCEL") //要结束程序的名称
                    {
                        process.Kill();
                    }
                }
            }
    
            public static void FileCreate(string path)
            {
                DirectoryInfo dirInfo=new DirectoryInfo(path);
                if (dirInfo.Exists == false)
                {
                    dirInfo.Create();
                }
                else
                {
                    dirInfo.Delete();
                    dirInfo.Create();
                }
            }
    
    
            /// <summary>
            /// 将制定的范围单元格excel转换Jpg
            /// </summary>
            /// <param name="excelFilePath">excel文件路径,EX:@"c:\1.xls"</param>
            /// <param name="SaveExcelJPG">图片保存路径,EX:@"c:\test"</param>
            /// <param name="cell1"></param>
            /// <param name="cell2"></param>
    
            public static void ExcelToImages(string excelFilePath, string SaveExcelJPG,string cell1,string cell2)
            {
                KillProgram();
                FileCreate(SaveExcelJPG);
                EXCEL.Application app = new Microsoft.Office.Interop.Excel.Application();
                object objMis = System.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++)
                    {
                        Application.DoEvents();
                        EXCEL.Worksheet wsheet = (EXCEL.Worksheet)singleExcel.Worksheets[i];
                        object ranobj = DBNull.Value;
    
                        //设置选择单元格,在复制出来。
                        wsheet.get_Range(cell1, cell2).Copy(ranobj);
                       
                        //全选单元格,全部复制出来。
                        //wsheet.UsedRange.Copy(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);
                        string savepath = SaveExcelJPG + wsheet.Name + ".jpg";
                        myBitmap.Save(savepath, System.Drawing.Imaging.ImageFormat.Jpeg);
                        Clipboard.Clear();
                        myBitmap.Dispose();
                        bits.Dispose();
                    }
                    MessageBox.Show("转换成功!");
                }
                catch (Exception e)
                {
                    MessageBox.Show("转换失败!");
                }
                finally
                {
                    KillProgram();
                    //singleExcel.Close(objMis, objMis, objMis);
                    //app.Quit();
                }
            }
    
    
    
    
            /// <summary>
            /// 将用户使用过的excel单元格截图保存为jpg
            /// </summary>
            /// <param name="excelFilePath">源excel路径</param>
            /// <param name="SaveExcelJPG">目标图片的路径(不包含图片名)</param>
            /// <returns></returns>
            public static void ExcelToJpg(string excelFilePath, string SaveExcelJPG)
            {
                KillProgram();
                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];
    
    
                        object ranobj = DBNull.Value;
    
                        //设置选择单元格,在复制出来。
                        //wsheet.get_Range("A1", "AA22").Copy(ranobj);
                        wsheet.UsedRange.Copy(ranobj);
    
                        //全选单元格,全部复制出来。
                        //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(SaveExcelJPG, System.Drawing.Imaging.ImageFormat.Jpeg);
                        Clipboard.Clear();
                        myBitmap.Dispose();
                        bits.Dispose();
                    }
                    MessageBox.Show("转换成功!");
                }
                catch (Exception Excel)
                {
                     MessageBox.Show("转换失败!");
                }
                finally
                {
                    KillProgram();
                    //singleExcel.Close(objMis, objMis, objMis);
                    //app.Quit();
                }
            }
        }
    }
  • 相关阅读:
    十三、Sleuth分布式请求链路追踪
    十二、SpringCloud Stream消息驱动
    十一、SpringCloud Bus 消息总线
    Linux命令(权限管理)
    Linux命令(文件管理)
    Linux的文件和目录
    Linux介绍及安装
    Docker
    Nginx
    13、SpringBoot整合Mybatis-Plus
  • 原文地址:https://www.cnblogs.com/anbylau2130/p/2748244.html
Copyright © 2020-2023  润新知