C#控制台程序中使用剪贴板将Excel的单元格区域保存为图片

14 篇文章 0 订阅

代码来源: https://stackoverflow.com/questions/1287879/programmatically-c-convert-excel-to-an-image

using System;
using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Drawing.Imaging;
using Excel = Microsoft.Office.Interop.Excel;

public class Program
{
    [STAThread]//在控制台程序中需要在Main函数前加此句
    static void Main(string[] args)
    {
        Excel.Application excel = new Excel.Application();
        Excel.Workbook wkb = excel.Workbooks.Add(Type.Missing);
        Excel.Worksheet sheet = wkb.Worksheets[1] as Excel.Worksheet;
        Excel.Range range = sheet.Cells[1, 1] as Excel.Range;
        range.Formula = "Hello World";

        // copy as seen when printed
        range.CopyPicture(Excel.XlPictureAppearance.xlPrinter, Excel.XlCopyPictureFormat.xlPicture);

        // uncomment to copy as seen on screen
        //range.CopyPicture(Excel.XlPictureAppearance.xlScreen, Excel.XlCopyPictureFormat.xlBitmap);

        Console.WriteLine("Please enter a full file name to save the image from the Clipboard:");
        string fileName = Console.ReadLine();
        using (FileStream fileStream = new FileStream(fileName, FileMode.Create))
        {
            if (Clipboard.ContainsData(System.Windows.DataFormats.EnhancedMetafile))
            {
                Metafile metafile = Clipboard.GetData(System.Windows.DataFormats.EnhancedMetafile) as Metafile;
                metafile.Save(fileName);
            }
            else if (Clipboard.ContainsData(System.Windows.DataFormats.Bitmap))
            {
                BitmapSource bitmapSource = Clipboard.GetData(System.Windows.DataFormats.Bitmap) as BitmapSource;

                JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
                encoder.QualityLevel = 100;
                encoder.Save(fileStream);
            }
        }
        object objFalse = false;
        wkb.Close(objFalse, Type.Missing, Type.Missing);
        excel.Quit();
    }
}
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
public class ClipboardMetafileHelper
{
    [DllImport("user32.dll")]
    static extern bool OpenClipboard(IntPtr hWndNewOwner);
    [DllImport("user32.dll")]
    static extern bool EmptyClipboard();
    [DllImport("user32.dll")]
    static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem);
    [DllImport("user32.dll")]
    static extern bool CloseClipboard();
    [DllImport("gdi32.dll")]
    static extern IntPtr CopyEnhMetaFile(IntPtr hemfSrc, IntPtr hNULL);
    [DllImport("gdi32.dll")]
    static extern bool DeleteEnhMetaFile(IntPtr hemf);
    // Metafile mf is set to a state that is not valid inside this function.
    static public bool PutEnhMetafileOnClipboard(IntPtr hWnd, Metafile mf)
    {
        bool bResult = false;
        IntPtr hEMF, hEMF2;
        hEMF = mf.GetHenhmetafile(); // invalidates mf
        if (!hEMF.Equals(new IntPtr(0)))
        {
            hEMF2 = CopyEnhMetaFile(hEMF, new IntPtr(0));
            if (!hEMF2.Equals(new IntPtr(0)))
            {
                if (OpenClipboard(hWnd))
                {
                    if (EmptyClipboard())
                    {
                        IntPtr hRes = SetClipboardData(14 /*CF_ENHMETAFILE*/, hEMF2);
                        bResult = hRes.Equals(hEMF2);
                        CloseClipboard();
                    }
                }
            }
            DeleteEnhMetaFile(hEMF);
        }
        return bResult;
    }
}
//You can call this function with code that is similar to the following code:
//Metafile mf = new Metafile( "filename.emf" );
//ClipboardMetafileHelper.PutEnhMetafileOnClipboard(this.Handle, mf );


参考文档:

如何把Excel中的单元格等对象保存成图片http://www.cnblogs.com/powertoolsteam/archive/2011/01/24/1942952.html


VB代码:

shapes http://dmcritchie.mvps.org/excel/shapes.htm

shapes与shaperange区别 http://club.excelhome.net/thread-516957-1-1.html

excel中如何用vba导出对应图片文件 https://zhidao.baidu.com/question/1365918888688650419.html

Excel VBA04shapeCharthttps://wenku.baidu.com/view/fca0a98269dc5022aaea0089.html

What does the number in the AddChart2 VBA macro represents? https://stackoverflow.com/questions/27889579/what-does-the-number-in-the-addchart2-vba-macro-represents


使用第三方DLL:

http://bbs.csdn.net/topics/391821536


C# 对Excel文档打印时的页面设置

http://www.cnblogs.com/arxive/p/5794699.html



  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值