2016-02-02 80 views
0

我試圖格式化整個Excel工作表以使所有單元具有相同的寬度和高度。將電子表格中的所有單元格設置爲相同的指定高度和寬度

下面的代碼無法正常工作,但沒有給出錯誤之一:

 transposeSheet.Cells.UseStandardHeight = 15 
    transposeSheet.Cells.UseStandardWidth = 15 

有大量的代碼在那裏做各個範圍,但我找不到如何將整個表做指定的高度和寬度。

回答

1

注意我剛剛創建於以下內容的MSDN code sample

這裏是一個例子,確保所有的對象被正確處置,這就是爲什麼有相當數量的代碼,否則我們可以用少代碼來做到這一點,其中許多閃光燈離開網絡,將做的工作,但留下的殘餘電腦內存中未處理的對象有時如果幸運的話將在應用程序關閉時不處理。

enter image description here

呼叫例如用於

Dim FileName As String = IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "WorksheetsTest.xlsx") 
Dim SheetName As String = "Sheet1" 

SetWidthHeight(FileName, SheetName, 10, 50) 

代碼模塊上方

Option Strict On 
Imports Excel = Microsoft.Office.Interop.Excel 
Imports Microsoft.Office 
Imports System.Runtime.InteropServices 
Module OpenWorkSheets4 
    Public Sub SetWidthHeight(ByVal FileName As String, ByVal SheetName As String, ByVal RowHeight As Integer, ByVal ColumnHeight As Integer) 
     If IO.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 

       Runtime.InteropServices.Marshal.FinalReleaseComObject(xlWorkSheet) 
       xlWorkSheet = Nothing 

      Next 
      If Proceed Then 
       xlCells = xlWorkSheet.Cells 
       Dim EntireRow As Excel.Range = xlCells.EntireRow 
       EntireRow.RowHeight = RowHeight 
       EntireRow.ColumnWidth = ColumnHeight 

       ReleaseComObject(xlCells) 
       ReleaseComObject(EntireRow) 
      Else 
       MessageBox.Show(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 
      MessageBox.Show("'" & FileName & "' not located.") 
     End If 
    End Sub 
    Private Sub ReleaseComObject(ByVal obj As Object) 
     Try 
      If obj IsNot Nothing Then 
       System.Runtime.InteropServices.Marshal.ReleaseComObject(obj) 
       obj = Nothing 
      End If 
     Catch ex As Exception 
      obj = Nothing 
     End Try 
    End Sub 
End Module 
0

您需要使用下面的

Cells.Select // Selects all cells on active sheet 
Selection.RowHeight = Yournumberhere // Changes height of selected rows 
Selection.ColumnWidth = Yournumberhere // Changes width of selected columns 
相關問題