gpt4 book ai didi

excel - 将 Excel 图表导出到 SVG 创建一个空文件

转载 作者:行者123 更新时间:2023-12-03 13:56:11 24 4
gpt4 key购买 nike

我正在尝试使用 VBA 以 SVG 格式导出 Excel 图表。

    Set objChrt = ActiveChart.Parent
objChrt.Activate
Set curChart = objChrt.Chart

curChart.Export fileName:=fileName, FilterName:="SVG"
如果我将“SVG”替换为“PNG”,则导出将完全按预期工作并生成有效的 PNG 文件。但是,“SVG”会生成一个空文件。 (手动,有一个选项可以在 Excel 365 中另存为 SVG,因此存在导出过滤器)。
根据文档,Filtername 是“图形过滤器在注册表中出现的与语言无关的名称。”,但我在注册表中找不到类似的东西,无论哪种方式,都很难想象 SVG 过滤器名称被命名为“SVG”以外的任何名称。
有没有办法使用 VBA 以 SVG 格式导出图表?

注意:还有一个关于 Chart.export 生成空文件的问题,修复方法是使用 ChartObject.Activate导出前。这个问题是不同的,因为代码在“PNG”下正常工作,但在“SVG”下失败(所以这不是与激活或可见性相关的问题)。推荐的修复也不起作用。

最佳答案

以矢量格式导出:
如果您的主要问题是以某种矢量格式导出图表,我建议只导出为 PDF,因为这非常简单:

Set curChart = objChrt.Chart
objChrt.ExportAsFixedFormat xlTypePDF, "YourChart"
PDF 现在将您的图表包含为矢量图形,而 PDF 是一种广泛支持的格式,可用于进一步处理。
如果您绝对需要将图表转换为 .svg,您可以使用开源软件 Inkscape 从命令行(因此很容易实现自动化)执行此操作。或者我想:/ .
转换为 SVG:
不幸的是,Inkscape 转换似乎对我不起作用,所以我使用开源 pdf 渲染工具包实现了它 Poppler . (安装说明在本文底部)
该库提供命令行实用程序 pdftocairo,它将在以下解决方案中使用:
Sub ExportChartToSVG()
Dim MyChart As ChartObject
Set MyChart = Tabelle1.ChartObjects("Chart 1")

Dim fileName As String
fileName = "TestExport"

Dim pathStr As String
pathStr = ThisWorkbook.Path

' Export chart as .pdf
MyChart.Chart.ExportAsFixedFormat Type:=xlTypePDF, FileName:=pathStr & "\" & fileName

' Convert .pdf file to .svg
Dim ret As Double
ret = Shell("cmd.exe /k cd /d """ & pathStr & """ & " & "pdftocairo -svg -f 1 -l 1 " & fileName & ".pdf", vbHide)
End Sub
请注意,生成的 .svg 文件中的文本不可选择,并且该文件大于手动导出生成的文件(在我的测试中为 241 KB 与 88 KB)。文件肯定是无限分辨率,所以 不是 嵌入在 .svg 文件中的奇怪位图偶尔会看到,但还有另一个小问题:
不幸的是, ExportAsFixedFormat方法创建一个 PDF“页面”,其中图形根据工作表上的位置定位在页面上。不幸的是,.svg 转换保留了这种“页面”格式。我不得不了解到摆脱这个问题并不像我最初想象的那么简单,因为 excel 不支持自定义页面大小,因此将图表导出为没有白色边框的 .pdf 似乎几乎不可能,请参阅此赏金但未解决 question (编辑:我在以下部分解决了它,并发布了我的方法作为该问题的答案)。我尝试了几种他们在这个链接问题中甚至没有想到的方法,但仍然没有设法仅使用 Excel 正确完成它,这可能取决于您的打印机驱动程序,但我不会那样做...
导出到没有白条的干净 SVG:
最简单的解决方法是使用 Word 将图表正确导出为 .pdf:
Sub ExportChartToSVG()
Dim MyWorksheet As Worksheet
Set MyWorksheet = Tabelle1

Dim MyChart As ChartObject
Set MyChart = MyWorksheet.ChartObjects(1)

Dim fileName As String
fileName = "TestExport"

Dim pathStr As String
pathStr = ThisWorkbook.Path

'Creating a new Word Document
'this is necessary because Excel doesn't support custom pagesizes
'when exporting as pdf and therefore unavoidably creates white borders around the
'chart when exporting
Dim wdApp As Object
Set wdApp = CreateObject("Word.Application")
wdApp.Visible = False

Dim wdDoc As Object
Set wdDoc = wdApp.Documents.Add

MyChart.Copy
wdDoc.Range.Paste

Dim shp As Object
Set shp = wdDoc.Shapes(1)

With wdDoc.PageSetup
.LeftMargin = 0
.RightMargin = 0
.TopMargin = 0
.BottomMargin = 0
.PageWidth = shp.Width
.PageHeight = shp.Height
End With
shp.Top = 0
shp.Left = 0

wdDoc.saveas2 fileName:=pathStr & "\" & fileName, FileFormat:=17 '(wdExportFormatPDF)
wdApp.Quit 0 '(wdDoNotSaveChanges)
Set wdApp = Nothing
Set wdDoc = Nothing
Set shp = Nothing

' Convert .pdf file to .svg
Dim ret As Double
ret = Shell("cmd.exe /k cd /d """ & pathStr & """ & " & "pdftocairo -svg -f 1 -l 1 " & fileName & ".pdf", vbHide)
End Sub
生成的 .pdf 和 .svg 看起来与手动导出的 .svg 完全相同,只有 .pdf 具有可选文本。 .pdf 文件保留在文件夹中。如有必要,稍后可以通过 VBA 代码轻松删除它...
如果此方法用于导出大量图表,我强烈建议将其移到一个类中并让该类保存 Word 应用程序的一个实例,这样它就不会不断地重新打开和关闭 Word。它具有使实际代码导出非常简洁和干净的额外好处。
用于导出到干净 SVG 的基于类的方法:
导出的代码变得非常简单:
Sub ExportChartToSVG()
Dim MyWorksheet As Worksheet
Set MyWorksheet = Tabelle1

Dim MyChart As ChartObject
Set MyChart = MyWorksheet.ChartObjects(1)

Dim fileName As String
fileName = "TestExport"

Dim filePath As String
filePath = ThisWorkbook.Path

Dim oShapeExporter As cShapeExporter
Set oShapeExporter = New cShapeExporter

' Export as many shapes as you want here, before destroying oShapeExporter
' cShapeExporter can export objets of types Shape, ChartObject or ChartArea
oShapeExporter.ExportShapeAsSVG MyChart, fileName, filePath

Set oShapeExporter = Nothing
End Sub
名为 cShapeExporter 的类模块的代码:
Option Explicit

Dim wdApp As Object
Dim wdDoc As Object
Dim wdWasOpen As Boolean

Private Sub Class_Initialize()
If WordIsRunning Then
Set wdApp = GetObject(, "Word.Application")
wdWasOpen = True
Else
Set wdApp = CreateObject("Word.Application")
wdApp.Visible = False
wdWasOpen = False
End If

Set wdDoc = wdApp.Documents.Add

With wdDoc.PageSetup
.LeftMargin = 0
.RightMargin = 0
.TopMargin = 0
.BottomMargin = 0
End With
End Sub

Private Sub Class_Terminate()
If Not wdWasOpen Then
wdApp.Quit 0 '(wdDoNotSaveChanges)
Else
wdDoc.Close 0
End If
Set wdApp = Nothing
Set wdDoc = Nothing
End Sub

Public Sub ExportShapeAsSVG(xlShp As Object, fileName As String, filePath As String)
If TypeName(xlShp) = "ChartObject" Or TypeName(xlShp) = "Shape" Or TypeName(xlShp) = "ChartArea" Then
'fine
Else
MsgBox "Exporting Objects of type " & TypeName(xlShp) & " not supported, sorry."
Exit Sub
End If

xlShp.Copy
wdDoc.Range.Paste

Dim wdShp As Object
Set wdShp = wdDoc.Shapes(1)

With wdDoc.PageSetup
.PageWidth = wdShp.Width
.PageHeight = wdShp.Height
End With

wdShp.Top = 0
wdShp.Left = 0

' Export as .pdf
wdDoc.saveas2 fileName:=filePath & "\" & fileName, FileFormat:=17 '(wdExportFormatPDF)

' Convert .pdf file to .svg
Dim ret As Double
ret = Shell("cmd.exe /k cd /d """ & filePath & """ & " & "pdftocairo -svg -f 1 -l 1 " & fileName & ".pdf", vbHide)

' Delete temporary shape in wdDoc
wdShp.Delete
End Sub

Private Function WordIsRunning() As Boolean
Dim wdApp As Object
On Error Resume Next
Err.Clear
Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
WordIsRunning = False
Else
WordIsRunning = True
End If
End Function
安装 Poppler 实用程序:
我假设你在这里使用的是 Windows,在 Linux 上获得 Poppler 无论如何都是微不足道的......
所以在 Windows 上,我建议使用 chocolatey 安装它Windows 的数据包管理器。要安装巧克力,您可以关注 these instructions (需要 <5 分钟)。
当你有巧克力时,你可以用简单的命令安装 Poppler
choco install poppler
您已准备好运行我建议的将 .pdf 转换为 .svg 的代码。
如果您更喜欢以不同的方式安装 Poppler,这里提供了多种选项 here ,但我想添加一些关于某些方法的注释:
  • 下载二进制文件对我不起作用,运行该实用程序总是会导致错误。
  • 通过 Anaconda ( conda install -c conda-forge poppler ) 安装也不适合我。刚刚安装失败。
  • 通过适用于 Linux 的 Windows 子系统安装确实有效,并且该实用程序也有效,但是如果您还没有安装包含发行版的 wsl,则必须下载并安装数百 MB ob 数据,这可能有点过头了。
  • 如果您安装了 MiKTeX,则应该包含该实用程序(就我而言)。我从 MiKTeX 安装中尝试了该实用程序,但不知何故它不起作用。
  • 关于excel - 将 Excel 图表导出到 SVG 创建一个空文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65131924/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com