9

E.g. we have 3 csv files 1.csv, 2.csv,3.csv. I want the output as All.xls

containing

the 3 csv files 1.csv, 2.csv and 3.csv in their respective tab.

3
  • Do the files have headers, it would be mucheasier to merge 3 csv files into one big csv file, and import that into excel rather than generate an xls from a shell. Commented Mar 8, 2018 at 6:32
  • Just check my answer and let me know if it solves your issue Commented Mar 8, 2018 at 7:20
  • Stack Overflow is not a code writing service. Please show your code. Since Stack Overflow hides the Close reason from you: Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example. Commented Mar 8, 2018 at 18:45

5 Answers 5

2

Yes the are multiple ways to do what you want. Perl, Python and Ruby have the appropriate modules. Probably other scripting languages also. Depends on which scripting language you are comfortable with.

Here is a pointer to one way of doing what you want using Python: Python script to convert CSV files to Excel

Sign up to request clarification or add additional context in comments.

3 Comments

I do not want to convert csv to xls, if you completely understood my requirement then kindly comment, I want the script in unix
Hope this will help you link
@VaibhavGehlot: As you have said it is quite direct in python, or other scripting languages!
2

You can use this well maintained Perl script:

https://metacpan.org/release/Text-CSV_XS

which exists in most Linux distributions (although with different names):

https://repology.org/project/perl:text-csv-xs/versions

Here is the syntax

csv2xlsx -o ~/All.xlsx 1.csv 2.csv 3.csv

Comments

1

Just do the following:

1-open an empty excel file then go to file then options then choose customize Ribbon then select developer then press ok

2-Now you have developer tap showing in your ribbon so all that you need to do is to select it then press on VisualBasic icon

3-Microsoft Visual basic for applications window will pop, select insert then modules

4-paste the following code into the empty window:

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", ".xlsx", vbTextCompare), xlWorkbookDefault
        ActiveWorkbook.Close
        Windows(xWsheet).Activate
        xCSVFile = Dir
    Loop
    Application.StatusBar = False
    Application.DisplayAlerts = True
End Sub

5-press f5 to run, navigate to your CSV files then press ok and wait for the VBA script to do its magic = this will transform CSV files to xlsx

6-Now delete the previous VBA script in modules and add the following script

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", ".xlsx", vbTextCompare), xlWorkbookDefault
        ActiveWorkbook.Close
        Windows(xWsheet).Activate
        xCSVFile = Dir
    Loop
    Application.StatusBar = False
    Application.DisplayAlerts = True
End Sub

7-press f5 to run, navigate to your files then press ok and wait for the VBA script to do its magic = this will transform xlsx files to one excel file

Comments

0

You can try awk '!a[$0]++' ./*.csv > ./all.xls This command will combine all the csv files in current folder and create a new file: all.xls with single header row.

1 Comment

This solution doesn't answer the original need: all the CSV files are not put in multiple sheets, and all files needs to have the same structure. On top of it, the resulting file is not in Excel format, it's just a CSV with an .xls extension that will just barely might open into Excel with most probably some encoding issue if the original file are in UTF-8 for instance.
-1

The command to cat files together to produce a new file is cat. However, if you simply did a

 cat *csv >All.xls

you would also have header lines in the middle of the resulting files. There are two ways to work around this problem:

The first involves that you create temporary files out of each csv file, where the header line is missing, before putting together the pieces. This can be done using the tail command, for example

tail -n +2 2.csv >2_without_header.csv

The second possibility may or may not be applicable in your case. If - as it is often the case with CSV files - the order of the lines doesn't matter and duplicate lines can be ignored and - as it is likely in your case - the headers are identical, you could simply do a

sort -u *csv >All.xls

4 Comments

In my understanding he doesn't want to concatanete the 3 csv files but want to make an xls file with 3 sheets that will contain the data of the csv file!
I first also thought this, because he named the new file with .xls, but look at his comment above, where he says explicitly: I do not want to convert csv to xls! I think it's just poor naming. Actually, since Excel works just fine with CSV files, it indeed does make sense to stick with CSV.
I want 3 tabs in a single excel, first tab contains data for 1.csv, 2nd tab contains data for 2.csv and so on.
Ah, I see. In this case, my solution for sure is not appropriate ...... In this case, you want to convert the CSV files into Excel Format. Well, this has been answered in a good way by VaibhavGehlot already.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.