我想问一下如何导出到excel与固定列宽度。因为列的宽度取决于字符的长度。
这是我的示例代码:Excel和宏
Excel过程:
DEFINE VARIABLE h-excel AS COM-HANDLE NO-UNDO.
DEFINE VARIABLE h-sheet AS COM-HANDLE.
DEFINE VAR w-invname AS CHAR INITIAL "file-pathexcel.xls".
CREATE "Excel.Application" h-excel.
h-sheet = h-excel:Workbooks:OPEN (w-invname,,FALSE,,,,,,,,,,FALSE) NO-ERROR.
h-excel:visible = true.
h-excel:Cells:Select.
h-excel:Run("loading").
/*h-excel:Range("A" + STRING(5)):VALUE = "Date Covered " + STRING(fifr) + " - " + STRING(fito).*/
h-excel:Range("A6"):Select.
RELEASE OBJECT h-sheet.
RELEASE OBJECT h-excel.
END PROCEDURE.
Excel宏:
Sub loading()
'
' loading Macro
'
With ActiveSheet.QueryTables.Add(Connection:="TEXT;file-pathtext.txt", _
Destination:=Range("A6"))
.Name = "CarSumm"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = xlWindows
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileOtherDelimiter = "|"
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.Refresh BackgroundQuery:=False
End With
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 6
ActiveWindow.ScrollColumn = 9
End Sub
U may Try below code before exporting, U can also increase or decrease 1.08 static value if not getting desire output
intRowsCount = ActiveSheet.UsedRange.Columns.count
Set objCells = ActiveSheet.Cells
For c = 1 To intRowsCount
objCells(1, c).ColumnWidth = 1.08 * Len(objCells(1, c).Value)
Next
'根据标题值len更改列宽度intRowsCount = ActiveSheet.UsedRange.Columns.Count设置objCells = ActiveSheet。细胞For c = 1 To intRowsCountobjCells(1, c).ColumnWidth = 1.08 * Len(objCells(1, c).Value)下一个导出活动表表(ActiveSheet.Name) .Copy作业本(2)。激活Set objWS = Workbooks(2).Worksheets(1)objWS。选择
'Save new worksheet with new workbookname
ActiveWorkbook.SaveAs Filename:="FilePath.xlsx" _
, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
Workbooks(2).Save
Workbooks(2).Close