Suggerimento: le altre lingue sono tradotte da Google. Puoi visitare il English versione di questo link.
Connetti
x
or
x
x
Registrati
x

or

Come convertire in batch più file CSV in file XLS (X) in Excel?

Per convertire un file CSV in file XlS o XLSX è molto facile per te applicando la funzione Salva come. Tuttavia, convertire più file CSV in file XLS o XLSX da una cartella richiede molto tempo salvandone uno alla volta manualmente. Qui presento un codice macro per convertire rapidamente tutti i file CSV in file XLS (x) da una cartella.

Batch converte i file CSV in file XlS (X) con codice macro

Scheda Office Abilita la modifica e la navigazione a schede in Office e semplifica notevolmente il tuo lavoro ...
Kutools per Excel risolve la maggior parte dei problemi e aumenta la produttività del 80%
  • Riutilizzare qualsiasi cosa: Aggiungi le formule più utilizzate o complesse, i grafici e qualsiasi altra cosa ai tuoi preferiti e riutilizzali rapidamente in futuro.
  • Più delle funzioni di testo di 20: Estrai numero da stringa di testo; Estrai o rimuovi parte di testi; Converti numeri e valute in parole inglesi ...
  • Unisci strumenti: Più cartelle di lavoro e fogli in uno; Unisci più celle / righe / colonne senza perdere dati; Unisci righe duplicate e somma ...
  • Strumenti di divisione: Suddivisione dei dati in più fogli in base al valore; Una cartella di lavoro per più file Excel, PDF o CSV; Una colonna a più colonne ...
  • Incolla Saltare Righe nascoste / filtrate; Conte E Somma per colore di sfondo; Crea una mailing list e Invia e-mail in base al valore della cella...
  • Super filtro: Crea schemi di filtro avanzati e applica a tutti i fogli; Ordinare per settimana, giorno, frequenza e altro; Filtro da grassetto, formule, commento ...
  • Oltre alle potenti funzionalità di 300; Funziona con Office 2007-2019 e 365; Supporta tutte le lingue; Facile implementazione nella tua azienda o organizzazione.

Batch converte i file CSV in file XlS (X) con codice macro


Per convertire più file CSV da una cartella in file XLS (X), puoi procedere come segue:

Kutools for Excel, con oltre 120 funzioni a portata di mano, facilita il lavoro.

1. Abilitare una nuova cartella di lavoro, premere Alt + F11 chiavi per aprire Microsoft Visual Basic, Applications Edition finestra e fare clic inserire > Modulo. Vedi screenshot:
doc batch converte cvs xls 1

Note: Assicurati che tutti i file CSV che desideri convertire siano chiusi.

2. Quindi incollare sotto il codice macro su Modulo sceneggiatura e stampa F5 chiave per eseguire il codice.

VBA: Converti CSV in XLS

Sub CSVtoXLS()
'UpdatebyExtendoffice20170814
    Dim xFd As FileDialog
    Dim xSPath As String
    Dim xCSVFile As String
    Dim xWsheet As String
    Application.DisplayAlerts = False
    Application.StatusBar = True
    xWsheet = ActiveWorkbook.Name
    Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
    xFd.Title = "Select a folder:"
    If xFd.Show = -1 Then
        xSPath = xFd.SelectedItems(1)
    Else
        Exit Sub
    End If
    If Right(xSPath, 1) <> "\" Then xSPath = xSPath + "\"
    xCSVFile = Dir(xSPath & "*.csv")
    Do While xCSVFile <> ""
        Application.StatusBar = "Converting: " & xCSVFile
        Workbooks.Open Filename:=xSPath & xCSVFile
        ActiveWorkbook.SaveAs Replace(xSPath & xCSVFile, ".csv", ".xls", vbTextCompare), xlNormal
        ActiveWorkbook.Close
        Windows(xWsheet).Activate
        xCSVFile = Dir
    Loop
    Application.StatusBar = False
    Application.DisplayAlerts = True
End Sub

3. Nella finestra di dialogo di chiusura, selezionare la cartella specificata contenente i file CSV che si desidera convertire. Vedi screenshot:
doc batch converte cvs xls 2

4. Clic OK, tutti i file CSV nella cartella selezionata sono stati convertiti in file XLS al suo interno.
doc batch converte cvs xls 3

Mancia: Se si desidera convertire i file CSV in file XLSX, si utilizza il codice VBA di seguito.

VBA: Converti file CSV in XLSX

Sub CSVtoXLS()
'UpdatebyExtendoffice20170814
    Dim xFd As FileDialog
    Dim xSPath As String
    Dim xCSVFile As String
    Dim xWsheet As String
    Application.DisplayAlerts = False
    Application.StatusBar = True
    xWsheet = ActiveWorkbook.Name
    Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
    xFd.Title = "Select a folder:"
    If xFd.Show = -1 Then
        xSPath = xFd.SelectedItems(1)
    Else
        Exit Sub
    End If
    If Right(xSPath, 1) <> "\" Then xSPath = xSPath + "\"
    xCSVFile = Dir(xSPath & "*.csv")
    Do While xCSVFile <> ""
        Application.StatusBar = "Converting: " & xCSVFile
        Workbooks.Open Filename:=xSPath & xCSVFile
        ActiveWorkbook.SaveAs Replace(xSPath & xCSVFile, ".csv", ".xls", vbTextCompare), xlWorkbookDefault
        ActiveWorkbook.Close
        Windows(xWsheet).Activate
        xCSVFile = Dir
    Loop
    Application.StatusBar = False
    Application.DisplayAlerts = True
End Sub


buonoEsporta intervallo su file

Kutools per Excel 's Esporta la funzione Intervallo su file può esportare o salvare un intervallo per separare il file come
cartella di lavoro, pdf, testo, csv o testo.
Clic Impresa > Import / Export > Esporta intervallo su file.
doc intervallo di celle di esportazione su file

Articoli relativi:


Kutools per Excel risolve la maggior parte dei problemi e aumenta la produttività del 80%

  • Riutilizzo: Inserire rapidamente formule complesse, grafici e tutto ciò che hai usato prima; Cifra le celle con password; Crea mailing list e invia email ...
  • Super Formula Bar (modifica facilmente più righe di testo e formula); Layout di lettura (leggere e modificare facilmente un gran numero di celle); Incolla su intervallo filtrato...
  • Unisci celle / righe / colonne senza perdere i dati; Contenuto delle celle divise; Combina righe / colonne duplicate... Previeni celle duplicate; Confronta le gamme...
  • Seleziona Duplica o Unica Righe; Seleziona righe vuote (tutte le celle sono vuote); Super Find e Fuzzy Find in molti libri di esercizi; Selezione casuale ...
  • Copia esatta Più celle senza modificare il riferimento alla formula; Creazione automatica di riferimenti a più fogli; Inserisci punti elenco, Caselle di controllo e altro ...
  • estrarre il testo, Aggiungi testo, Rimuovi per posizione, Rimuovi spazio; Creare e stampare subtotali di paging; Converti contenuto e commenti tra celle...
  • Super filtro (salva e applica schemi di filtro ad altri fogli); Ordinamento avanzato per mese / settimana / giorno, frequenza e altro; Filtro speciale di grassetto, corsivo ...
  • Combina cartelle di lavoro e fogli di lavoro; Unisci tabelle basate su colonne chiave; Dividi i dati in più fogli; Converti in batch xls, xlsx e PDF...
  • Oltre alle potenti funzionalità di 300. Supporta Office / Excel 2007-2019 e 365. Supporta tutte le lingue. Facile implementazione nella tua azienda o organizzazione. Funzioni complete di prova gratuita di 30-day.
kte tab 201905

Scheda Office Porta l'interfaccia a schede a Office e semplifica notevolmente il lavoro

  • Abilita la modifica e la lettura a schede in Word, Excel, PowerPoint, Editore, accesso, Visio e progetto.
  • Apri e crea più documenti in nuove schede della stessa finestra, piuttosto che in nuove finestre.
  • Aumenta la tua produttività di 50% e riduce centinaia di clic del mouse per te ogni giorno!
fondo officetab
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    viper · 1 months ago
    it changes my date column format from dd-mm-yyyy to dd/mm/yyyy, please can anyone help with how to prevent this script from changing column formats.
    Thanks a lot.
  • To post as a guest, your comment is unpublished.
    lisa6538 · 3 months ago
    Thanks for providing the code. I modified the code to do the XLSX to CSV batch conversion. The code is provided below.


    Sub XLSXtoCSV()
    'UpdatebyExtendoffice20170814
    Dim xFd As FileDialog
    Dim xSPath As String
    Dim xCSVFile As String
    Dim xWsheet As String
    Application.DisplayAlerts = False
    Application.StatusBar = True
    xWsheet = ActiveWorkbook.Name
    Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
    xFd.Title = "Select a folder:"
    If xFd.Show = -1 Then
    xSPath = xFd.SelectedItems(1)
    Else
    Exit Sub
    End If
    If Right(xSPath, 1) <> "\" Then xSPath = xSPath + "\"
    xXLSXFile = Dir(xSPath & "*.xlsx")
    Do While xXLSXFile <> ""
    Application.StatusBar = "Converting: " & xXLSXFile
    Workbooks.Open Filename:=xSPath & xXLSXFile
    ActiveWorkbook.SaveAs Replace(xSPath & xXLSXFile, ".xlsx", ".csv", vbTextCompare), xlCSV
    ActiveWorkbook.Close
    Windows(xWsheet).Activate
    xXLSXFile = Dir
    Loop
    Application.StatusBar = False
    Application.DisplayAlerts = True
    End Sub
    • To post as a guest, your comment is unpublished.
      Diego Jaime · 27 days ago
      Thank you for the modification, that´s what I was looking for!
  • To post as a guest, your comment is unpublished.
    Sam · 3 months ago
    One small issue with this code is that some accuracy (number of decimals) can be lost when opening a csv and saving it as xlsx.
    The way to prevent this is to select all cells right after opening, set the number of decimal places to the desired amount (15 for me personally), and *then* saving as xlsx

    Otherwise, the xlsx has less detail (less accuracy in terms of decimals) than the original csv, which can lead to problems in some applications
  • To post as a guest, your comment is unpublished.
    Rose · 4 months ago
    this macro does not show my csv file
  • To post as a guest, your comment is unpublished.
    stb · 4 months ago
    Cool bit of code. Very useful. If I'm not mistaken the code for .xlsx looks the same as the .xls code.

    This line:
    ActiveWorkbook.SaveAs Replace(xSPath & xCSVFile, ".csv", ".xls", vbTextCompare), xlWorkbookDefault

    I assume it should be: ".csv" , ".xlsx", vbTextCompare etc.
    • To post as a guest, your comment is unpublished.
      iBrezel · 4 months ago
      I've changed the code so, described above; now the script works as expected: it generates xlsx files
      • To post as a guest, your comment is unpublished.
        thy · 2 months ago
        no you haven't. it still says xls in both versions.
  • To post as a guest, your comment is unpublished.
    pramod koliar · 6 months ago
    Great Script after searching all other script available in google. Can anybody help how to modify this script by specifying a folder location in local drive instead of allowing it to open the dialog box as i am actually automating the system. Thanks in advance
  • To post as a guest, your comment is unpublished.
    Pramod Koliar · 6 months ago
    Hi, great script which worked for me after trying all other script available in google. I have added few more lines which performs the text to columns operation which i want. Can anybody help me how to modify the script by specifying the folder location in local drive instead of opening the dialog box as i am actually automating the system
    . Thanks in advance
  • To post as a guest, your comment is unpublished.
    HC · 8 months ago
    Hello,

    a great article - thank you! Almost works for me.

    Here's the problem: I have a pipe-delimited csv file. I have changed the default windows delimiter (via control panel/regional settings) to "|". So, when I open the csv file with Excel, it opens, directly reads and parses the fields to columns correctly. All I have to do is to save as xls or xlsx, done. When I look at your code, that should be what the code does: It opens the csv files in a folder, saves them as xls or xlsx, and loops over that.

    Here is the problem: When I open those Excel files, the pipe delimiters are still there, nothing is parsed to the columns. This was the behavior as before my change to the pipe delimited default setting in the Windows settings. So it seems like Excel doesn't use those Windows settings. I verified this by replacing the pipe delimiters with comma delimiters in a copy of the file - of the two files, the pipe-delimited remained unparsed, the comma delimited got converted just fine.

    Any idea where that Excel-inbuilt comma delimitation can be found or changed, or whether there is a way in the code to do that? I would prefer NOT to have to do a search and replace, then save...

    Thanks, HC
    • To post as a guest, your comment is unpublished.
      Sunny · 7 months ago
      If you want to split the data into column based on the pipe delimiter in Excel, just use Text to Columns function to split data by / after coverting the csv files to xls.
  • To post as a guest, your comment is unpublished.
    Fred Maye · 11 months ago
    WHen I gave the folder name, the program said, "No files match your search." But there are 2,609 .csv files in the folder
    • To post as a guest, your comment is unpublished.
      Sunny · 11 months ago
      The code not support the separated comma csv file, are your files in this case?
  • To post as a guest, your comment is unpublished.
    ptee · 1 years ago
    I used the file multiple csv to multiple xls but get wrong xls de csv files have ; seperated info but become together in cells. How can this been solved?
    • To post as a guest, your comment is unpublished.
      Sunny · 1 years ago
      The code cannot support the separated comma csv file.
  • To post as a guest, your comment is unpublished.
    Dragos · 1 years ago
    Hi,

    Your script is great but it doesn't convert the csv separated by semicolons. Can you please post a solution for those?

    Thank you
    • To post as a guest, your comment is unpublished.
      Younes · 1 years ago
      i might be late i just got the issue where i needed to convert the semicolons CSV files and i added this to the code and it worked hope this will help someone

      Change 'Workbooks.Open Filename:=xSPath & xCSVFile' By this 'Workbooks.Open Filename:=xSPath & xCSVFile, Delimiter:=";", Local:=True'
    • To post as a guest, your comment is unpublished.
      Sunny · 1 years ago
      Sorry,here I do not have any solution, maybe you can place your question to our forum, someone else may help you. https://www.extendoffice.com/forum.html
  • To post as a guest, your comment is unpublished.
    Sathish · 1 years ago
    Thankyou so much for your excellent script for CSV to XLS. Its much more useful to my work
  • To post as a guest, your comment is unpublished.
    Matt Leonard · 1 years ago
    "Tip: If you want to convert CSV files to XLXS files, you just need to change .xls to .xlsx in the macro ActiveWorkbook.SaveAs Replace(xSPath & xCSVFile, ".csv", ".xls", vbTextCompare)."

    I tried this, and Excel could not open the resulting files. I got the following message: "Excel cannot open the file 'filename.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file."

    However, the script was successful for converting to .xls.
    • To post as a guest, your comment is unpublished.
      Sunny · 1 years ago
      I did not find that problem before, thank you for your correct comment. I have corrected the tip.
  • To post as a guest, your comment is unpublished.
    Karen · 1 years ago
    I want to do the opposite - convert XLS to CSV. does this script work?
    Sub CSVtoXLS()
    'UpdatebyExtendoffice20170814
    Dim xFd As FileDialog
    Dim xSPath As String
    Dim xXLSFile As String
    Dim xWsheet As String
    Application.DisplayAlerts = False
    Application.StatusBar = True
    xWsheet = ActiveWorkbook.Name
    Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
    xFd.Title = "Select a folder:"
    If xFd.Show = -1 Then
    xSPath = xFd.SelectedItems(1)
    Else
    Exit Sub
    End If
    If Right(xSPath, 1) <> "\" Then xSPath = xSPath + "\"
    xXLSFile = Dir(xSPath & "*.xls")
    Do While xXLSFile <> ""
    Application.StatusBar = "Converting: " & xXLSFile
    Workbooks.Open Filename:=xSPath & xXLSFile
    ActiveWorkbook.SaveAs Replace(xSPath & xXLSFile, ".xls", ".csv", vbTextCompare), xlNormal
    ActiveWorkbook.Close
    Windows(xWsheet).Activate
    xXLSFile = Dir
    Loop
    Application.StatusBar = False
    Application.DisplayAlerts = True
    End Sub