如何访问“保存缩略图”通过 Interop 实现 Excel 的功能?

发布于 2024-12-24 04:08:29 字数 305 浏览 5 评论 0原文

我正在使用 C# 和 .NET 4.0 开发 Excel 加载项。在 Excel 中,“另存为”对话框中有一项功能,用于将预览缩略图与文档一起保存。如何在代码中访问此功能?另外,保存后如何访问预览图像(我认为它是位图)?

目前,我的 Excel 加载项按如下方式复制文档:

Globals.ThisAddIn.Application.ActiveWorkbook.SaveCopyAs("tempwbcopy");

然后将文档复制到服务器并删除临时文件。基本上我还想制作缩略图,将其发布到服务器,并删除临时文件。

I'm working on an Excel add-in using C# and .NET 4.0. In Excel there's a feature in the Save-As dialog for saving a preview thumbnail along with the document. How can I access this feature in code? Also, how do I access the preview image (I think it's a bitmap) once it has been saved?

Currently my Excel add-in makes a copy of the document as follows:

Globals.ThisAddIn.Application.ActiveWorkbook.SaveCopyAs("tempwbcopy");

It then copies the document to a server and erases the temp file. Basically I'd like to also make the thumbail image, post it to the server, and erase the temp file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娜些时光,永不杰束 2024-12-31 04:08:29

我不知道如何以编程方式访问“保存缩略图”功能,但如果您有一个带有缩略图的 Excel 文件并且想要提取图像,您可以使用以下代码(使用 OpenXml 2.0 API):

Private Sub ExtractThumbnailAsPng(ByVal pathToExcelFile As String, ByVal outputPath As String)

    Dim thumbnailPart As DocumentFormat.OpenXml.Packaging.ThumbnailPart

    Using excelFile As SpreadsheetDocument = SpreadsheetDocument.Open(pathToExcelFile, True)
        thumbnailPart = excelFile.ThumbnailPart
        If thumbnailPart IsNot Nothing Then
            Using thumbnailStream As Stream = thumbnailPart.GetStream(FileMode.Open, FileAccess.ReadWrite)
                Dim thumbBitmap As New Bitmap(thumbnailStream)
                thumbBitmap.Save(outputPath, System.Drawing.Imaging.ImageFormat.Png)
            End Using
        End If
    End Using

End Sub

因为这不是 Excel自动化你也可以在服务器端进行。

I don't know how to access the Save Thumbnail feature programatically, but if you have a Excel file with a thumbnail and want to extract the image you could use the following code (using the OpenXml 2.0 API):

Private Sub ExtractThumbnailAsPng(ByVal pathToExcelFile As String, ByVal outputPath As String)

    Dim thumbnailPart As DocumentFormat.OpenXml.Packaging.ThumbnailPart

    Using excelFile As SpreadsheetDocument = SpreadsheetDocument.Open(pathToExcelFile, True)
        thumbnailPart = excelFile.ThumbnailPart
        If thumbnailPart IsNot Nothing Then
            Using thumbnailStream As Stream = thumbnailPart.GetStream(FileMode.Open, FileAccess.ReadWrite)
                Dim thumbBitmap As New Bitmap(thumbnailStream)
                thumbBitmap.Save(outputPath, System.Drawing.Imaging.ImageFormat.Png)
            End Using
        End If
    End Using

End Sub

Since this isn't Excel automation you could do this server side as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文