问题描述
我得到一张充满复杂图纸(形状)的电子表格。我正在尝试编写一个小实用程序来将它们提取到EMF。一切正常,除了结果是PNG图像而不是EMF矢量文件。当我尝试将它们导入其他
应用程序并调整大小时,我会失去解决方案。
Hi, I get t a spreadsheet full of complicated drawings (shapes). I am trying to write a small utility to extract them to EMF. Everything works except the result turn out to be PNG images rather than EMF vector files. When I try to import them into other applications and resize, I lose resolution.
任何人都可以看到错误吗?
Can anybody see what is wrong?
使用System;
使用System.Collections.Generic;
使用System.Drawing.Imaging;
使用System.Linq;
使用System.Runtime.InteropServices;
使用System.Text ;
使用System.Threading.Tasks;
使用Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Excel;
命名空间ShapeExtractor
{
课程计划
{
[DllImport(" user32.dll",EntryPoint =" OpenClipboard",SetLastError = true,ExactSpelling = true,CallingConvention = CallingConvention.StdCall)]¥b $ b    public static extern bool OpenClipboard(IntPtr hWnd);
namespace ShapeExtractor
{
class Program
{
[DllImport("user32.dll", EntryPoint = "OpenClipboard", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool OpenClipboard(IntPtr hWnd);
[DllImport(" user32.dll",EntryPoint =" EmptyClipboard",SetLastError = true,ExactSpelling = true,CallingConvention = CallingConvention.StdCall)]¥b $ b    public static extern bool EmptyClipboard();
[DllImport("user32.dll", EntryPoint = "EmptyClipboard", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool EmptyClipboard();
[DllImport(" user32.dll",EntryPoint =" SetClipboardData",SetLastError = true,ExactSpelling = true,CallingConvention = CallingConvention.StdCall)]¥b $ b    public static extern IntPtr SetClipboardData(int uFormat,IntPtr hWnd);
[DllImport("user32.dll", EntryPoint = "SetClipboardData", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr SetClipboardData(int uFormat, IntPtr hWnd);
[DllImport(" user32.dll",EntryPoint =" CloseClipboard",SetLastError = true,ExactSpelling = true,CallingConvention = CallingConvention.StdCall)]¥b $ b    public static extern bool CloseClipboard();
[DllImport("user32.dll", EntryPoint = "CloseClipboard", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool CloseClipboard();
[DllImport(" user32.dll",EntryPoint =" GetClipboardData",SetLastError = true,ExactSpelling = true,CallingConvention = CallingConvention.StdCall)]¥b $ b    public static extern IntPtr GetClipboardData(int uFormat);
[DllImport("user32.dll", EntryPoint = "GetClipboardData", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr GetClipboardData(int uFormat);
[DllImport(" user32.dll",EntryPoint =" IsClipboardFormatAvailable",SetLastError = true,ExactSpelling = true,CallingConvention = CallingConvention.StdCall)]¥b $ b    public static extern short IsClipboardFormatAvailable(int uFormat);
static void Main(string [] args)
{
const int CF_ETAFILE = 14;
[DllImport("user32.dll", EntryPoint = "IsClipboardFormatAvailable", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern short IsClipboardFormatAvailable(int uFormat);
static void Main(string[] args)
{
const int CF_ETAFILE = 14;
试试
{
VAR xlApp =新Microsoft.Office.Interop.Excel.Application();
                 xlApp.WindowState = XlWindowState.xlMinimized;
xlApp.Visible = FALSE;
               工作簿wkBook = xlApp.Workbooks.Open(" xyz.xlsx");
try
{
var xlApp = new Microsoft.Office.Interop.Excel.Application();
xlApp.WindowState = XlWindowState.xlMinimized;
xlApp.Visible = false;
Workbook wkBook = xlApp.Workbooks.Open("xyz.xlsx");
    工作表iconSheet = wkBook.Sheets ["图标与QUOT;];
              &NBSP ;如果(iconSheet!= NULL)
                {
                   形状的形状= iconSheet.Shapes;
                 &NBSP ; 的foreach(形状的形状的形状)
                 &NBSP ;  {
                    &NBSP ;   shape.Copy();
                        如果(OpenClipboard(IntPtr.Zero)及;&安培;
                IsClipboardFormatAvailable(CF_ETAFILE)!= 0)
                   {
                    &NBSP ;       IntPtr intPtr = GetClipboardData(CF_ETAFILE);
          元文件的元文件=新图元文件(IntPtr的,TRUE);
               &NBSP ;            CloseClipboard();
                             metaFile.Save(shape.Name + QUOT; .EMF" ;, ImageFormat.Emf);
Worksheet iconSheet = wkBook.Sheets["Icons"];
if (iconSheet != null)
{
Shapes shapes = iconSheet.Shapes;
foreach (Shape shape in shapes)
{
shape.Copy();
if (OpenClipboard(IntPtr.Zero) &&
IsClipboardFormatAvailable(CF_ETAFILE) != 0)
{
IntPtr intPtr = GetClipboardData(CF_ETAFILE);
Metafile metaFile = new Metafile(intPtr, true);
CloseClipboard();
metaFile.Save(shape.Name + ".emf", ImageFormat.Emf);
                             Console.WriteLine(shape.Name);
                &NBSP ;       }
                    }
}
wkBook.Close();
xlApp.Quit();
}
catch(例外e)
{
Console.WriteLine(e);
}
}
}
}
Console.WriteLine(shape.Name);
}
}
}
wkBook.Close();
xlApp.Quit();
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
}
推荐答案
我可以重现你的问题。据我所知,Excel不支持将形状导出为emf格式。而且,老实说,我不熟悉Windows API。
I could reproduce your issue. As far as I know, Excel does not support to export shape as emf format. And, to be honestly, I'm not familiar with windows API.
由于Power Point支持将形状导出为emf格式。作为一种解决方法,我们可以打开一个powerpoint文件,并将形状从excel复制到power point临时,然后通过Power Point将其导出。
Since Power Point support to export the shape as emf format. As a workaround, we could open a powerpoint file and copy shape from excel to power point temporary and then export it via Power Point.
这是简单的代码。
try
{
var pptApp = new PPT.Application();
//pptApp.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
var slide=pptApp.Presentations.Add(Microsoft.Office.Core.MsoTriState.msoFalse).Slides.Add(1, PPT.PpSlideLayout.ppLayoutBlank);
dynamic shapeRange;
string shapeName = "";
var xlApp = new Microsoft.Office.Interop.Excel.Application();
xlApp.WindowState = Excel.XlWindowState.xlMinimized;
xlApp.Visible = false;
Excel.Workbook wkBook = xlApp.Workbooks.Open(@"C:\Users\v-guaxu\Desktop\TestFolder\111.xlsx");
Excel.Worksheet iconSheet = wkBook.Sheets["Sheet1"];
if (iconSheet != null)
{
Excel.Shapes shapes = iconSheet.Shapes;
foreach (Excel.Shape shape in shapes)
{
shapeName = shape.Name;
shape.Copy();
shapeRange =slide.Shapes.PasteSpecial(PPT.PpPasteDataType.ppPasteDefault);
shapeRange.Export(@"C:\Users\v-guaxu\Desktop\TestFolder\" + shapeName + ".emf",
5,Type.Missing,Type.Missing,1);
shapeRange.Delete();
}
}
pptApp.Quit();
wkBook.Close();
xlApp.Quit();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
最好的问候,
Terry
这篇关于将Excel形状保存为EMF矢量格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!