如何将excel工作表导出为图像? [英] How can I export an excel worksheet as image?
本文介绍了如何将excel工作表导出为图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
using System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用Microsoft.Office.Interop.Excel;
命名空间ConsoleApplication1
{
class Prueba
{
[STAThread]
static void Main(string [] args)
{
var a = new Microsoft.Office.Interop.Excel.Application();
尝试
{
工作簿w = a.Workbooks.Open(@C:\SCRATCH\Libro2.xlsx);
工作表ws = w.Sheets [Report];
ws.Protect(Contents:false);
范围r = ws.Range [B2:H20];
r.CopyPicture(XlPictureAppearance.xlScreen,XlCopyPictureFormat.xlBitmap);
a.DisplayAlerts = false;
// System.Runtime.InteropServices.COMExceptionExcepciónde HRESULT:0x80010105(RPC_E_SERVERFAULT)
ChartObject chartObj = ws.ChartObjects()。Add(r.Left,r.Top,r。宽度,高度);
chartObj.Activate();
图表= chartObj.Chart;
chart.Paste();
chart.Export(@C:\SCRATCH\image.JPG,JPG);
chartObj.Delete();
w.Close(SaveChanges:false);
}
finally
{
a.Quit();
}
}
}
}
我正在使用Office 2013,64位,Windows 7 64和.Net 4.5。
解决方案
在WinForms项目中:
using System;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System.Drawing;
使用System.Linq;
使用System.Text;
使用System.Windows.Forms;
使用Excel = Microsoft.Office.Interop.Excel;
使用ios = System.Runtime.InteropServices;
命名空间ClipBoardTest
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent() ;
}
public void ExportRangeAsJpg()
{
Excel.Application xl;
xl =(Excel.Application)ios.Marshal.GetActiveObject(Excel.Application);
if(xl == null)
{
MessageBox.Show(No Excel !!);
返回;
}
Excel.Workbook wb = xl.ActiveWorkbook;
Excel.Range r = wb.ActiveSheet.Range [A1:E10];
r.CopyPicture(Excel.XlPictureAppearance.xlScreen,
Excel.XlCopyPictureFormat.xlBitmap);
if(Clipboard.GetDataObject()!= null)
{
IDataObject data = Clipboard.GetDataObject();
if(data.GetDataPresent(DataFormats.Bitmap))
{
Image image =(Image)data.GetData(DataFormats.Bitmap,true);
this.pict1.Image = image;
image.Save(@C:\_Stuff\test\sample.jpg,
System.Drawing.Imaging.ImageFormat.Jpeg);
}
else
{
MessageBox.Show(Clipboard !!中没有图像);
}
}
else
{
MessageBox.Show(Clipboard Empty !!);
}
}
private void button1_Click(object sender,EventArgs e)
{
ExportRangeAsJpg();
}
}
}
对于控制台应用程序,您还需要查看: http://blog.another-d-mention.ro/programming/c/use-clipboard-copypaste-in-c-console-application/
I'm triying to generate an image from an excel worksheet. After a lot of research, I'm using the following code, but at some point I get an exception:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;
namespace ConsoleApplication1
{
class Prueba
{
[STAThread]
static void Main(string[] args)
{
var a = new Microsoft.Office.Interop.Excel.Application();
try
{
Workbook w = a.Workbooks.Open(@"C:\SCRATCH\Libro2.xlsx");
Worksheet ws = w.Sheets["Report"];
ws.Protect(Contents: false);
Range r = ws.Range["B2:H20"];
r.CopyPicture(XlPictureAppearance.xlScreen, XlCopyPictureFormat.xlBitmap);
a.DisplayAlerts = false;
// System.Runtime.InteropServices.COMException Excepción de HRESULT: 0x80010105 (RPC_E_SERVERFAULT)
ChartObject chartObj = ws.ChartObjects().Add(r.Left, r.Top, r.Width, r.Height);
chartObj.Activate();
Chart chart = chartObj.Chart;
chart.Paste();
chart.Export(@"C:\SCRATCH\image.JPG", "JPG");
chartObj.Delete();
w.Close(SaveChanges: false);
}
finally
{
a.Quit();
}
}
}
}
I'm using Office 2013, 64 bits, Windows 7 64 and .Net 4.5.
解决方案
This worked for me in a WinForms project:
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 ios = System.Runtime.InteropServices;
namespace ClipBoardTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void ExportRangeAsJpg()
{
Excel.Application xl;
xl = (Excel.Application)ios.Marshal.GetActiveObject("Excel.Application");
if (xl == null)
{
MessageBox.Show("No Excel !!");
return;
}
Excel.Workbook wb = xl.ActiveWorkbook;
Excel.Range r = wb.ActiveSheet.Range["A1:E10"];
r.CopyPicture(Excel.XlPictureAppearance.xlScreen,
Excel.XlCopyPictureFormat.xlBitmap);
if (Clipboard.GetDataObject() != null)
{
IDataObject data = Clipboard.GetDataObject();
if (data.GetDataPresent(DataFormats.Bitmap))
{
Image image = (Image)data.GetData(DataFormats.Bitmap, true);
this.pict1.Image = image;
image.Save(@"C:\_Stuff\test\sample.jpg",
System.Drawing.Imaging.ImageFormat.Jpeg);
}
else
{
MessageBox.Show("No image in Clipboard !!");
}
}
else
{
MessageBox.Show("Clipboard Empty !!");
}
}
private void button1_Click(object sender, EventArgs e)
{
ExportRangeAsJpg();
}
}
}
For a console app you need to see also: http://blog.another-d-mention.ro/programming/c/use-clipboard-copypaste-in-c-console-application/
这篇关于如何将excel工作表导出为图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文