Solved

Combining csv into an excel workbook

Posted on 2014-02-18
12
1,730 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
 
LVL 52

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 52

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:Bing CISM / CISSP
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
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.

 

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 68

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 52

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 68

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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

896 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

13 Experts available now in Live!

Get 1:1 Help Now