Solved

Combining csv into an excel workbook

Posted on 2014-02-18
12
1,709 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
  • 4
  • 3
  • 2
  • +3
12 Comments
 
LVL 35

Expert Comment

by:Kimputer
Comment Utility
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
Comment Utility
Yes, I was looking for automating it. Something that could go in a SSIS package as a script.
0
 
LVL 8

Expert Comment

by:itjockey
Comment Utility
sorry
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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 51

Expert Comment

by:Bill Prew
Comment Utility
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:Bing CISM / CISSP
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:LuckyLucks
Comment Utility
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 68

Expert Comment

by:Qlemo
Comment Utility
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 51

Expert Comment

by:Bill Prew
Comment Utility
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 68

Assisted Solution

by:Qlemo
Qlemo earned 250 total points
Comment Utility
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 51

Accepted Solution

by:
Bill Prew earned 250 total points
Comment Utility
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
Comment Utility
These look promising ...let me check them out this week...thanks...points on the way :)
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…

762 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

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now