Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Combining csv into an excel workbook

Posted on 2014-02-18
12
Medium Priority
?
1,904 Views
Last Modified: 2014-03-10
Hi,

 I have two csvs, both with different column names and numbers in the first column. They also containg different data. I would like to combine them into a excel workbook say destination.xlsx that contains the first csv in the first tab and the second one in the secon tab.

How do I go about doing this?
0
Comment
Question by:LuckyLucks
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +3
12 Comments
 
LVL 36

Expert Comment

by:Kimputer
ID: 39867859
If this is a one time thing, the easiest way is to open the first csv file, then click underneath on the "insert worksheet" button (or press SHIFT+F11) the create a new sheet, then drag and drop the second csv file onto this blank sheet. Now your excel file has 2 sheets, from both csv files.

If you are more talking about automating it, because you have to do this often, let me know.
0
 

Author Comment

by:LuckyLucks
ID: 39867861
Yes, I was looking for automating it. Something that could go in a SSIS package as a script.
0
 
LVL 8

Expert Comment

by:Naresh Patel
ID: 39867869
sorry
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 57

Expert Comment

by:Bill Prew
ID: 39867914
I can work up a VBS script that can do this, will the files always have the same names and locations, or does that need to be taken in as command line parms?  Is the destination Excel file always stored in the same location, and what should it be named.  What should the sheet tab names be for the files?  If the excel file already exists should it be overwritten?  What version of excel do you want the output stored in (XLS versis XLSX)?

~bp
0
 
LVL 57

Expert Comment

by:Bill Prew
ID: 39867920
Can you provide a sample of the two CSV files for testing, sometimes Excel needs certain options depending on the exact format of the CSV files.

~bp
0
 
LVL 37

Expert Comment

by:bbao
ID: 39867951
Agree with Kimputer.

Another approach is to first import the CSV files into different XLS files then simply merge them at the end.

I think you must have known how to import CSV into XLS, just see below the steps to merge multiple XLS files in a single Excel Book file.

How to Merge XLS Files
http://www.ehow.com/m/how_7193236_merge-xls-files.html
0
 

Author Comment

by:LuckyLucks
ID: 39867983
So, in response this is what I have at the moment:

Files1.csv and File2.cav <-- always called this. Always stored in the same location.

They are always put into a excelworkbook that is called Final_yyyymmdd.xlsx. Always stored on the same location.

In the workbook the tabs can be called Final_yyyymmdd and Final_yyyymmdd

There is no reason why the Final would contain a version of the above unless it was stale from the last run. But if its does'nt, it should be overwritten.

You can use the sample files 1 and 2 below:

File 1

Column1      Column2      Column3
1      Apple      Apple Pie
2      Banana      Smoothie
3      Carrot      Cake
4      Cherry       Cake
5      Beans      Coffee
6      Leaves      Tea

File 2

FirstName      LastName
Mary       Jane
Bob       Smith
Drew      Barry
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 39868065
Storing two sheets with name of Final_yyyymmdd into a file called Final_yyyymmdd doesn't make much sense ;-).
I suggest to use File1 and File2 as sheet names.
0
 
LVL 57

Expert Comment

by:Bill Prew
ID: 39868088
For the sample files I actually wanted real files attached here, not just sample test data - I can create that easy enough.  But there are certain details in the files that might need to be accounted for like is the delimiter a ",", are double quotes allowed around fields, is there a header row, etc...

~bp
0
 
LVL 71

Assisted Solution

by:Qlemo
Qlemo earned 1000 total points
ID: 39868127
Here is a versatile PowerShell script to do that. It could be done more simple by using the import features of Excel, but this allows for pre-processing, applying formats and stuff, if needed.
cls

function export-xls ($sheet)
{
begin {
	$props = $null
	$row = 1
}
process {
	if (!$props) {
	  $col = 1
		$props = $_ | gm -MemberType NoteProperty
		$props | % {
			$sheet.Cells.Item($row, $col).Value2 = $_.Name
			$sheet.Cells.Item($row, $col++).Font.Bold = $true
		}
		$row++
	}
	$col = 1
	foreach ($prop in $props) {
		$sheet.Cells.Item($row, $col++).Value2 = $_.($prop.Name)
	}
	$row++
}
end {
	$sheet.usedRange.EntireColumn.AutoFit() | Out-Null
}
}

$excel = New-Object -ComObject excel.application
$excel.visible=$true
$wb = $excel.Workbooks.Add()            # empty, unnamed workbook
# Delete all but two work sheets
$excel.DisplayAlerts = $false
for ($i = $wb.Worksheets.Count; $i -gt 2; --$i) {$wb.Worksheets.Item($i).Delete()}
$excel.DisplayAlerts = $true

$ws = $wb.Worksheets.Item(1)
$ws.Name = 'file1'
import-csv 'C:\temp\EE\file1.csv' | export-xls $ws
$ws = $wb.Worksheets.Item(2)
$ws.Name = 'file2'
Import-Csv 'C:\temp\EE\file2.csv' | export-xls $ws

$wb.SaveAs('C:\temp\EE\final_'+(Get-Date -Format 'yyyyMMdd')+'.xlsx')
$excel.Quit()

Remove-Variable wb,ws,excel

Open in new window

0
 
LVL 57

Accepted Solution

by:
Bill Prew earned 1000 total points
ID: 39868270
Here is a simple VBS example that does the job, just update the paths to your files as needed.

' Some constants from Excel object model
Const cExcelXLS = 56
Const cExcelXLSX = 51

' Build date stamp in YYYMMDD format
sDate = Year(Now) & Right("0" & Month(Now), 2) & Right("0" & Day(Now), 2)

' Define paths to CSV and Excel file paths
sNewFile = "B:\EE\EE28367792\Final_" & sDate & ".xls"
sCsvFile1 = "B:\EE\EE28367792\file1.csv"
sCsvFile2 = "B:\EE\EE28367792\file2.csv"

' Instantiate the Excel application, but don't show it
Set oExcel = CreateObject("Excel.Application")
oExcel.Visible = False
oExcel.DisplayAlerts = False

' Open one CSV file
Set oNew = oExcel.Workbooks.Open(sCsvFile2)

' Open second CSV file and merge into first
Set oFile1 = oExcel.Workbooks.Open(sCsvFile1)
oFile1.Sheets("File1").Move oNew.Sheets("File2")

' Save merged result as an Excel file
oNew.SaveAs sNewFile, cExcelXLS
oNew.Close

' Shut down Excel
oExcel.Quit

Open in new window

~bp
0
 

Author Comment

by:LuckyLucks
ID: 39869403
These look promising ...let me check them out this week...thanks...points on the way :)
0

Featured Post

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question