Solved

Combining csv into an excel workbook

Posted on 2014-02-18
12
1,778 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 54

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 54

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 54

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 54

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

820 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