2016-08-10 1 views
0

Je voudrais demander comment exporter vers Excel avec une largeur de colonne fixe. Puisque la largeur d'une colonne dépend de la longueur du caractère.Excel Colonne-Largeur

Voici mon exemple de code: pour Excel et Macro

Excel Procédure:

DEFINE VARIABLE h-excel AS COM-HANDLE NO-UNDO. 
DEFINE VARIABLE h-sheet AS COM-HANDLE. 
DEFINE VAR w-invname AS CHAR INITIAL "file-path\excel.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 Macro:

Sub loading() 
' 
' loading Macro 

' 
    With ActiveSheet.QueryTables.Add(Connection:="TEXT;file-path\text.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 

Répondre

0
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 
+0

merci pour la réponse. BTW où dans la macro devrais-je ajouter cela? nouveau à la chose macro. Je viens de copier et coller ce qui était utilisé dans notre code source. – noob

0

« Modifier la largeur de colonne basée sur la valeur d'en-tête len intRowsCount = ActiveSheet.UsedRange.Columns.Count Set objCells = ActiveSheet.Cells Pour c = 1 Pour intRowsCount objCells (1, c) .ColumnWidth = 1,08 * Len (objCells (1, c) .Value) suivant « Export feuille active feuilles (ActiveSheet.Name) .copy Workbooks (2) .Activate Set objWS = Workbooks (2) .Worksheets (1) objWS.Select

'Save new worksheet with new workbookname 
ActiveWorkbook.SaveAs Filename:="FilePath.xlsx" _ 
    , FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False 
Workbooks(2).Save 
Workbooks(2).Close