我在excel中创建了一个提取。我从SQL Server中提取数据 - 然后将其写入excel。首先,我通过指定行来创建标题,见下文....
With xlSheetInfo
.Cells(5, 1).ColumnWidth = 50
.Cells(5, 1).Value = "School"
.Cells(5, 2).ColumnWidth = 25
.Cells(5, 2).Value = "Name"
.Cells(5, 3).Value = "Q1"
.Cells(5, 4).Value = "Comments"
end with
然后我使用记录集
写入数据While Not g_RS3.EOF
For i = xlCol To rCount
Cells(xlRow, xlCol).Value = g_RS3("Location")
Cells(xlRow, xlCol).Font.Bold = True
xlCol = xlCol + 1
Cells(xlRow, xlCol).Value = g_RS3("LastName") & " ," & g_RS3("FirstName")
xlCol = xlCol + 1
Cells(xlRow, xlCol).Value = g_RS3("Q01")
xlCol = xlCol + 1
Cells(xlRow, xlCol).Value = g_RS3("Comments")
next i
wend
我遇到的问题是“评论”,因为此字段最多可包含500个字符。由于这是我的摘录中的最后一列,我希望它是AUTOFIT。实际上因为有很多记录,我希望根据最长评论的长度进行自动调整或自动调整。我不确定在哪里应该包含它以使其自动调整。我该如何解决这个问题?
答案 0 :(得分:2)
使用AutoFit
方法:
Cells(xlRow, xlCol).Value = g_RS3("Comments")
Cells(xlRow, xlCol).EntireColumn.AutoFit