zoukankan      html  css  js  c++  java
  • excel转图片(通过单元格截取图片)

    View Code
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using EXCEL= Microsoft.Office.Interop.Excel;
    using System.Diagnostics;
    
    namespace excel转图片
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                KillProgram();
                GetExcel(@"D:\test\1.xls");
            }
    
            public static void KillProgram()
            {
                foreach (Process process in Process.GetProcesses())
                {
                    if (process.ProcessName == "Excel") //要结束程序的名称
                    {
                        process.Kill();
                    }
                } 
                
            }
    
    
            public static string GetExcel(string excelFilePath)
            {
                string SaveExcelJPG = @"D:\test\1.jpg";
                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(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;
            }
    
    
        }
    }
  • 相关阅读:
    B站14天数据分析笔记6次课笔记
    B站14天数据分析笔记5次课作业
    B站14天数据分析笔记5次课Pandas
    1037 在霍格沃茨找零钱 (20 point(s))
    第一章:第三节探索性数据分析
    Java没有运行选项
    Eclipse截图的时候错误提示消失/复制粘贴错误信息
    错误记录_css属性的值一定不需要引号么?
    错误记录_语法哪里错了?
    微软输入法使用
  • 原文地址:https://www.cnblogs.com/anbylau2130/p/2744948.html
Copyright © 2011-2022 走看看