C#控制台程序中使用剪贴板将Excel的单元格区域保存为图片 您所在的位置:网站首页 怎么剪切excel成图片 C#控制台程序中使用剪贴板将Excel的单元格区域保存为图片

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

2024-07-01 03:11| 来源: 网络整理| 查看: 265

代码来源: 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 VBA常用技巧第04章shape及Chart对象https://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

 


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有