【问题标题】:Visual basic with excel automation带有 excel 自动化的 Visual Basic
【发布时间】:2017-01-27 18:28:57
【问题描述】:

我未能在 Visual Basic 应用程序中设置 Excel 电子表格的行高和列宽。

我有一个 Visual Basic 应用程序,我的剪贴板中有数据。我将该代码复制到excel实例中,然后让excel保存生成的电子表格,然后excel关闭。我试图在保存电子表格之前以编程方式设置行高和单元格宽度,但一直无法这样做。这是我正在执行的代码:

If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
    Dim oXL As Excel.Application
    Dim oWB As Excel.Workbook
    Dim oSheet As Excel.Worksheet
    oXL = CreateObject("Excel.Application")
    oXL.Visible = True
    oWB = oXL.Workbooks.Add
    oSheet = oWB.ActiveSheet

    oSheet.Paste()
    oSheet.Cells.Select()
    oSheet.Selection.RowHeight = 11.4
    oSheet.Cells.EntireColumn.AutoFit()
    oSheet = Nothing
    oWB.Close(True, SaveFileDialog1.FileName)
    oWB = Nothing
    oXL.Quit()
    oXL = Nothing
    MsgBox("Finished!")
End If

应用程序在没有 oSheet.Cells.Select()、oSheet.Selection.RowHeight = 11.4 和 oSheet.Cells.EntireColumn.AutoFit() 的情况下运行 线。使用这些行,我会收到以下错误对话框消息:

未找到类型“工作表”上的公共成员“选择”。

当我在 Visual Studio 中跟踪程序时,执行 oSheet.Paste() 命令并执行 oSheet.Cells.Select() 命令。当我尝试执行 oSheet.Selection.RowHeight = 11.4 命令时会产生异常。

任何帮助将不胜感激。

乔纳森

【问题讨论】:

  • 试试这个oSheet.Rows("1:1").RowHeight = 11.4 并删除oSheet.Cells.Select()
  • 如果你要使用.Select,它是highly recommended to avoid doing,它只是.Select,而不是.Select()
  • @Zaggler - 我尝试了你的建议。我不再收到错误消息,但生成的电子表格没有任何高度为 11.4 的行,并且列也没有自动调整。还有其他建议吗?
  • @BruceWayne - 当我尝试将 .Select() 更改为 .Select 时,Visual Studio 会自动将 () 放到属性上。我是在 Visual Basic 应用程序中执行此操作的,也许这是 excel vba 中的格式?
  • @Zaggler - 我从您的建议中删除了 ("1:1") 并且有效!我现在使用的命令是 oSheet.Rows.RowHeight = 11.4 没有 .select 命令。

标签: excel vb.net office-automation


【解决方案1】:

以下不是后期绑定,而是早期绑定,但该模式应该可以在少量代码更改的情况下使用。

Option Strict On
Imports Excel = Microsoft.Office.Interop.Excel
Imports System.Runtime.InteropServices
Imports System.IO

Public Class Operations
    Public HasError As Boolean
    Public ErrorMessage As String
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="FileName">Path and file name for Excel file</param>
    ''' <param name="SheetName">Sheet name to work on</param>
    ''' <param name="RowHeight">Used to set row height in SheetName</param>
    ''' <returns></returns>
    Public Function SetWidthHeight(
        ByVal FileName As String,
        ByVal SheetName As String,
        ByVal RowHeight As Integer) As Boolean

        If File.Exists(FileName) Then

            Dim Proceed As Boolean = False

            Dim xlApp As Excel.Application = Nothing
            Dim xlWorkBooks As Excel.Workbooks = Nothing
            Dim xlWorkBook As Excel.Workbook = Nothing
            Dim xlWorkSheet As Excel.Worksheet = Nothing
            Dim xlWorkSheets As Excel.Sheets = Nothing
            Dim xlCells As Excel.Range = Nothing

            xlApp = New Excel.Application
            xlApp.DisplayAlerts = False


            xlWorkBooks = xlApp.Workbooks
            xlWorkBook = xlWorkBooks.Open(FileName)

            xlApp.Visible = False

            xlWorkSheets = xlWorkBook.Sheets

            For x As Integer = 1 To xlWorkSheets.Count
                xlWorkSheet = CType(xlWorkSheets(x), Excel.Worksheet)

                If xlWorkSheet.Name = SheetName Then
                    Proceed = True
                    Exit For
                End If

                Marshal.FinalReleaseComObject(xlWorkSheet)
                xlWorkSheet = Nothing

            Next
            If Proceed Then

                xlCells = xlWorkSheet.Cells
                xlCells.PasteSpecial()

                Dim EntireRow As Excel.Range = xlCells.EntireRow
                '
                ' Set row height 
                '
                EntireRow.RowHeight = RowHeight
                Dim ColRange = xlCells.EntireColumn
                '
                ' Auto fit all columns
                '
                ColRange.AutoFit()
                ReleaseComObject(ColRange)

                ReleaseComObject(xlCells)
                ReleaseComObject(EntireRow)

            Else
                HasError = True
                ErrorMessage = SheetName & " not found."
            End If

            xlWorkSheet.SaveAs(FileName)

            xlWorkBook.Close()
            xlApp.UserControl = True
            xlApp.Quit()

            ReleaseComObject(xlWorkSheets)
            ReleaseComObject(xlWorkSheet)
            ReleaseComObject(xlWorkBook)
            ReleaseComObject(xlWorkBooks)
            ReleaseComObject(xlApp)

        Else
            HasError = True
            ErrorMessage = "'" & FileName &
                "' not located. Try one of the write examples first."
        End If

        Return HasError
    End Function
    Private Sub ReleaseComObject(ByVal excelObject As Object)
        Try
            If excelObject IsNot Nothing Then
                Marshal.ReleaseComObject(excelObject)
                excelObject = Nothing
            End If
        Catch ex As Exception
            excelObject = Nothing
        End Try
    End Sub
End Class

【讨论】:

    猜你喜欢
    • 2017-10-28
    • 1970-01-01
    • 2019-07-23
    • 2021-04-24
    • 1970-01-01
    • 1970-01-01
    • 2016-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多