将Excel中的指定区域 复制到 剪贴板,然后根据剪贴板 生成图片。
执行这一句总是报参数无效的错误:System.Drawing.Imaging.Metafile metafile = new System.Drawing.Imaging.Metafile(alloc, true);
代码如下:
static void copyToJpg()
{
//初始化excel对象
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
//打开xls文件
excel.Workbooks.Open(@"d:\tt.xls", System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing); //excel.Workbooks.Open(@"C:\Tt.xlsx", System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
//选定到第2行第2列所在的单元格
//Excel.Range r = (Excel.Range)excel.Cells[10, 2];
Microsoft.Office.Interop.Excel.Range r = (Microsoft.Office.Interop.Excel.Range)excel.get_Range("A1", "C11");
r.Select();
//将单元格复制到剪贴板中
r.CopyPicture(Microsoft.Office.Interop.Excel.XlPictureAppearance.xlScreen, Microsoft.Office.Interop.Excel.XlCopyPictureFormat.xlBitmap);
//退出excel
excel.Quit();
OpenClipboard(IntPtr.Zero); uint format = 14/*CF_ENHMETAFILE*/;
IntPtr alloc = GetClipboardData(format);
System.Drawing.Imaging.Metafile metafile = new System.Drawing.Imaging.Metafile(alloc, true);
CloseClipboard(); Bitmap b = new Bitmap(metafile);
b.Save(@"d:\test.bmp");
} [DllImport("user32.dll")]
private static extern bool OpenClipboard(IntPtr hWndNewOwner); [DllImport("user32.dll")]
private static extern bool EmptyClipboard(); [DllImport("user32.dll")]
private static extern IntPtr GetClipboardData(uint uFormat); [DllImport("user32.dll")]
private static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem); [DllImport("user32.dll")]
private static extern bool CloseClipboard();
执行这一句总是报参数无效的错误:System.Drawing.Imaging.Metafile metafile = new System.Drawing.Imaging.Metafile(alloc, true);
代码如下:
static void copyToJpg()
{
//初始化excel对象
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
//打开xls文件
excel.Workbooks.Open(@"d:\tt.xls", System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing); //excel.Workbooks.Open(@"C:\Tt.xlsx", System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
//选定到第2行第2列所在的单元格
//Excel.Range r = (Excel.Range)excel.Cells[10, 2];
Microsoft.Office.Interop.Excel.Range r = (Microsoft.Office.Interop.Excel.Range)excel.get_Range("A1", "C11");
r.Select();
//将单元格复制到剪贴板中
r.CopyPicture(Microsoft.Office.Interop.Excel.XlPictureAppearance.xlScreen, Microsoft.Office.Interop.Excel.XlCopyPictureFormat.xlBitmap);
//退出excel
excel.Quit();
OpenClipboard(IntPtr.Zero); uint format = 14/*CF_ENHMETAFILE*/;
IntPtr alloc = GetClipboardData(format);
System.Drawing.Imaging.Metafile metafile = new System.Drawing.Imaging.Metafile(alloc, true);
CloseClipboard(); Bitmap b = new Bitmap(metafile);
b.Save(@"d:\test.bmp");
} [DllImport("user32.dll")]
private static extern bool OpenClipboard(IntPtr hWndNewOwner); [DllImport("user32.dll")]
private static extern bool EmptyClipboard(); [DllImport("user32.dll")]
private static extern IntPtr GetClipboardData(uint uFormat); [DllImport("user32.dll")]
private static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem); [DllImport("user32.dll")]
private static extern bool CloseClipboard();
解决方案 »
- copy文件夹怎么拷贝
- 请问从一个很大的文本逐步读出数据,怎样的方法效率最高,谢谢!
- 请教WPF中TabControl的问题!在线等!
- 获得 快捷方式参数 问题..在线等...分不多了,跪求!~
- C#有没有专门写日志的类?
- 十万火急!
- 查找数据库中的表OleDbConnection.GetOleDbSchemaTable函数报错
- Windows应用程序中的物理路径???
- 请问:form上有n个button(或txtbox),除了一个名字为a的以外,我想全部删除掉,而不是visible=false,请问怎么实现?
- PropertyGrid中枚举值显示的问题!
- 关于.net 3.5的属性简写的一些问题
- 正则小问题,求解!
{
//初始化excel对象
Excel.Application excel = new Excel.Application();
//打开xls文件
excel.Workbooks.Open(@"d:\tt.xls", System.Type.Missing,...); //选定到第2行第2列所在的单元格
Excel.Range r = (Microsoft.Office.Interop.Excel.Range)excel.get_Range("A1", "C11");
r.Select();
//将单元格复制到剪贴板中
r.CopyPicture(Microsoft.Office.Interop.Excel.XlPictureAppearance.xlScreen, Microsoft.Office.Interop.Excel.XlCopyPictureFormat.xlBitmap);
//退出excel
excel.Quit();
OpenClipboard(IntPtr.Zero); uint format = 14/*CF_ENHMETAFILE*/;
IntPtr alloc = GetClipboardData(format);
System.Drawing.Imaging.Metafile metafile = new System.Drawing.Imaging.Metafile(alloc, true);
CloseClipboard(); Bitmap b = new Bitmap(metafile);
b.Save(@"d:\test.bmp");
}本来是格式化后的,贴上来没有保留原格式。
好像.net直接有cliboard用法,自己去查下