将电子表格中的所有单元格格式化为相同的指定高度和宽度

我正在尝试格式化整个Excel工作表,以使所有单元具有相同的宽度和高度。

以下代码不起作用,但也不会给出错误:

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

有很多代码用于做个别的范围,但我找不到如何做到整个工作表的指定的高度和宽度。

注意我刚刚在下面创build了一个MSDN代码示例 。

这是一个例子,确保所有的对象都妥善处理,这就是为什么有相当数量的代码,否则我们可以用less代码做到这一点,其中许多闪光灯离开networking,将做的工作,但留下未处置的对象的残余在电脑内存中有时候如果幸运的话会在应用closures的时候处理掉而不是。

在这里输入图像说明

调用示例

 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 

你会想使用下面的

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