在Excel工作表中设置最小单元格高度的任何方法?

时间:2016-05-27 08:20:08

标签: c# excel excel-interop

您好我的问题是如何在Excel工作表中为单元格设置最小高度?现在它看起来像:Excel Sheet

但它看起来应该是这样的:How it should look我到目前为止尝试使用:D:\Source\Test\Pizza λ \Tools\spring-roo-2.0.0.M2\bin\roo.bat ____ ____ ____ / __ \/ __ \/ __ \ / /_/ / / / / / / / / _, _/ /_/ / /_/ / /_/ |_|\____/\____/ 2.0.0.M2 [rev 003b42c] Welcome to Spring Roo. For assistance press TAB or type "hint" then hit ENTER. Spring Roo Eastern Grey UI started at 'http://localhost:9191/' roo> script --file PizzaProject.roo tailor activate --name web-simple Command 'tailor activate --name web-simple' not found (for assistance press TAB or type "hint" then hit ENTER) Searching 'tailor activate' on installed repositories 0 matches found with 'tailor activate' on installed repositories Script required 0.031 seconds to execute Script execution aborted roo> 设置单元格高度,然后设置excelSheet.Columns[1].ColumnWidth = 16.14;,但它会被覆盖,所以我希望找到一种方法为自动调整设置最小高度。然后我试着谷歌,但没有任何帮助出现。对于他们与

合并的单元格
excelSheet.Columns[1].Autofit()

然后使用// Merge the Cells for the summary Box for (int i = 2; i <= 10; i++) { excelSheet.Range[excelSheet.Cells[startColumn + i, 1], excelSheet.Cells[startColumn + i, 10]].Merge(Missing.Value); } Mabey进行换行,这与它有关。

所以任何帮助或建议都会非常感谢您的时间。对不起我的英语。

1 个答案:

答案 0 :(得分:1)

在Excel中模拟最小高度

为了模拟excel行中的Min Height设置,我编写了以下简单代码,并通过我的excel文件中的bellow指令使用它:

  1. 打开您的Excel文件,然后在Excel
  2. 中按 Alt + F11
  3. 点击打开的窗口中的View Code按钮,打开Editor
  4. enter image description here

    1. Copy-Paste Editor
    2. 中的下方代码

      <强>码

      Private Sub Worksheet_Change(ByVal Target As Range)
          'Cells.Rows.AutoFit
          For rowCounter = 1 To 500
              If Rows(rowCounter & ":" & rowCounter).EntireRow.RowHeight < 15 Then
                    Rows(rowCounter).EntireRow.RowHeight = 15
              End If
          Next
      End Sub
      

      enter image description here

        

      注1:如果您想强制工作表也对更改应用Rows AutoFit function,那么您可以通过从代码开头删除'符号来取消注释在上面的代码中。

           

      注2:如果您的Excel文件中的数据行数超过500,请将上述代码中的此数字增加到适当的值。

      1. Editor
      2. 中按 Ctrl + S 保存
      3. 返回工作表并更改任何单元格值并离开单元格,现在您应该看到结果;)