zoukankan      html  css  js  c++  java
  • 如何使用VSTO自动将Excel中的图表复制到Word

    如何使用VSTO自动将Excel中的图表复制到Word

    原文地址:https://code.msdn.microsoft.com/How-to-copy-Chart-in-Excel-a29f9029

    该项目说明如何使用VSTO自动复制Excel中的图表

    介绍

    有些客户经常在MSDN论坛上提出这个问题。 但在MSDN画廊中没有现有的样本。 所以如果有样品,可以帮助客户解决问题。

    客户证据:

    http://social.msdn.microsoft.com/Forums/en-US/isvvba/thread/21a2fd18-d850-40d9-9d87-122bbaf9369d

    http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/8d94da85-a95f-41c0-b7ec-6bf34fd0e151

    创建项目

    在Visual Studio 2013中打开项目(VBNETExportExcelChartToWord.vbproj)并构建它。

    VB代码:

     1 Module Module1
     2     Sub Main(args As String())
     3         Try
     4             Dim appPath As String = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
     5             Dim excelFile As String = appPath & Convert.ToString("TestExcel.xlsx")
     6             Dim wordFile As String = appPath & Convert.ToString("TestDoc.docx")
     7             ExportChartDataToWord(excelFile, wordFile)
     8             Console.WriteLine("Chart exported to word document successfully")
     9             Console.ReadLine()
    10         Catch ex As Exception
    11             Console.WriteLine(ex.Message)
    12         End Try
    13     End Sub
    14     Sub ExportChartDataToWord(excelFile As String, wordFile As String)
    15         Dim objExcelApp As Microsoft.Office.Interop.Excel._Application = New Microsoft.Office.Interop.Excel.Application()
    16         objExcelApp.Visible = False
    17         Dim workbook As Microsoft.Office.Interop.Excel._Workbook = objExcelApp.Workbooks.Open(excelFile)
    18         Dim worksheet As Microsoft.Office.Interop.Excel._Worksheet = DirectCast(workbook.Sheets(1), Microsoft.Office.Interop.Excel.Worksheet)
    19         Dim charObj As Microsoft.Office.Interop.Excel.ChartObject = worksheet.ChartObjects("Chart 1")
    20         Dim objWordApp As Microsoft.Office.Interop.Word._Application = New Microsoft.Office.Interop.Word.Application()
    21         If objWordApp Is Nothing Then
    22             Console.WriteLine("Word could not be started. Check that your office installation and project references are correct.")
    23             Return
    24         End If
    25         objWordApp.Visible = False
    26         Dim objDoc As Microsoft.Office.Interop.Word._Document = objWordApp.Documents.Open(wordFile)
    27         charObj.Chart.ChartArea.Copy()
    28         objDoc.Application.Selection.PasteSpecial()
    29         'Save the doc
    30         objDoc.Save()
    31         'Close the open docs and workbooks
    32         objWordApp.Documents.Close()
    33         objWordApp.Quit()
    34         objExcelApp.Workbooks.Close()
    35         objExcelApp.Quit()
    36     End Sub
    37 End Module

    1.此示例从Excel文件中复制图表对象内容,然后使用VSTO方式将其粘贴到Word文档中。
    2.在执行示例之前,将TestDoc.docx和TestExcel.xlsx文件复制到可执行位置。
    3.运行可执行文件VBNETExportExcelChartToWord.exe,这将会将图表内容复制到名为TestDoc.docx的单词文档中?
    4.打开word文档并验证其内容。

    Imports System.IO
    Imports Microsoft.Office.Interop.Excel
    Imports Microsoft.Office.Interop.Word
    Module Module1
    
        Sub Main()
            Try
                Dim appPath As String = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
                Dim excelFile As String = appPath & Convert.ToString("TestExcel.xlsx")
                Dim wordFile As String = appPath & Convert.ToString("TestDoc.Docx")
                ExportChartDataToWord(excelFile, wordFile)
                Console.WriteLine("Chart exported to word document successfully")
                Console.ReadLine()
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
            Console.Read()
        End Sub
        Sub ExportChartDataToWord(excelFile As String, wordFile As String)
            Dim objExcelApp As Microsoft.Office.Interop.Excel._Application = New Microsoft.Office.Interop.Excel.Application()
            objExcelApp.Visible = False
            Dim workbook As Microsoft.Office.Interop.Excel._Workbook = objExcelApp.Workbooks.Open(excelFile)
            Dim worksheet As Microsoft.Office.Interop.Excel._Worksheet = DirectCast(workbook.Sheets(1), Microsoft.Office.Interop.Excel.Worksheet) 'DirectCast类似于Ctype,不过要求必须前后的类型一致
            Dim charObj As Microsoft.Office.Interop.Excel.ChartObject = worksheet.ChartObjects("chart 3")
            Dim objWordApp As Microsoft.Office.Interop.Word._Application = New Microsoft.Office.Interop.Word.Application()
            If objWordApp Is Nothing Then
                Console.WriteLine("Word could not be started.Check that your office installation and project references are correct.")
                Return
            End If
            objWordApp.Visible = False
            Dim objDoc As Microsoft.Office.Interop.Word._Document = objWordApp.Documents.Open(wordFile)
            charObj.Chart.ChartArea.Copy()
            objDoc.Application.Selection.PasteSpecial()
            'Save the doc
            objDoc.Save()
            'Close the open docs and workbooks
            objWordApp.Documents.Close()
            objWordApp.Quit()
            objExcelApp.Workbooks.Close()
            objExcelApp.Quit()
    
        End Sub
    End Module
  • 相关阅读:
    Silverlight 调用GP服务第一篇之发布GP服务(Geoprocessing Service)
    SQLServer中char、varchar、nchar、nvarchar的区别:
    word中带圈符号如何手动输入
    js中循环for
    正则表达式总结
    SQL Server 2008 数据库 不允许保存修改。
    多线程三种锁Monitor,lock,Mutex
    Lambda和Linq的用法
    RowDeleting和RowDeleted区别
    ASP.NET中aspx.cs页面代码中的换行方法
  • 原文地址:https://www.cnblogs.com/xiehaofeng/p/7491394.html
Copyright © 2011-2022 走看看