Solved

Combining csv into an excel workbook

Posted on 2014-02-18
12
1,746 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
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:itjockey
ID: 39867869
sorry
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 53

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 53

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 69

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 53

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 69

Assisted Solution

by:Qlemo
Qlemo earned 250 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 53

Accepted Solution

by:
Bill Prew earned 250 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

User Beware!  This is a rather permanent solution to removing your email from an exchange server.  The only way to truly go back is to have your exchange administrator restore your mailbox from backups.  This is usually the option of last resort.  A…
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

831 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