I'm trying to convert an XLS file into CSV using Excel as a ComObject, yet maintaining the precision of the data concerned. I've tried changing the style to "#" or "Text" or similar for the formatting of each cell, but this still results in limited precision and doesn't seem to apply to the whole file.
For example: cell D4 in my source xls document has a value of 0.5124839309949 but is viewed as 0.51 due to Excel's Number formatting. Manually changing this to Text formatting resolves this but I need Powershell to run this for me, and then export (in CSV format) that same 0.5124839309949 rather than 0*.*51.
At the same time, I'm concerned about how this will treat date cells. As it stands, each row in my array has a date which Excel reads as DD/MM/YYYY except when put through to CSV this becomes the Excel serial date number (useless for my ultimate goal of bulk loading this into SQL Server.)
I'm aware that a File-SaveAs exercise in Excel produces a CSV but I need this to be run through Powershell due to business demands, and also, running this 'conversion' exercise manually also results in precision limits, so I am looking for a way around this please.
If you're able to amend the below Powershell script I'd be grateful!
Thanks for your help.
#
# Set the current folder path to temp
Set-Location \\xyz01\data\shared\data_warehouse\ntrs\temp
#Backup files
Copy-Item -path *.* -recurse \\xyz01\data\Shared\data_warehouse\test\performance
# Set variables
$file = "ABC Trust*"
$infile = "\\xyz01\data\shared\data_warehouse\ntrs\temp\"+$file
$outfile = "\\xyz01\data\shared\data_warehouse\test\temp\test_performance.csv"
# Set com object to Excel
$Excel = New-Object -ComObject Excel.Application
# Open the excel file and save as CSV
$workbook=$Excel.Workbooks.Open($infile)
$worksheet=$workbook.Worksheets.Item(1)
$worksheet.range.("D") = $worksheet.range.("D").Text
$workbook.SaveAs($outfile,6) # 6 is the code for .CSV
$workbook.Close($false)
$Excel.Quit()
The below may help? I have tried to replicate all suggested in here, to no avail.
Posh2Scripting - Automating Excel Spreadsheets with Powershell
Stackoverflow - Convert XLS to CSV without Data changes
OzGrid - Convert Excel To Text/CSV Without Losing Decimal Accuracy
Try changing this line...
$worksheet.range.("D") = $worksheet.range.("D").Text
to this...
$worksheet.range.("D") = $worksheet.range.("D").Value
Related
I am looking for a way to convert some excel workbooks into PDF files automaically using R.
I have seen people suggesting the RDCOMClient option, but it doesn't work from my company's PC. The problem I am encountering is that my spreadsheets contain tables, plots and images.
Is there a way I can print the entire worksheets and then save them into a pdf file?
I also tried using the loadWorkbook() function to import the files, but I cannot find a way to save them.
I thought about creating a pdf file with the pdf() function but I only managed to save the tables through the grid.table() function.
Does anyone have better ideas?
You can use the following approach
library(RDCOMClient)
xlApp <- COMCreate("Excel.Application")
path_Excel_File <- "C:\\...\\excel_File.xlsx"
xlWbk <- xlApp$Workbooks()$Open(path_Excel_File)
xlWbk$ExportAsFixedFormat(Type = 0, FileName = "C:\\...\\pdf_File.pdf")
Type = 0 is a PDF, Type = 1 is XPS, see https://learn.microsoft.com/en-us/office/vba/api/excel.xlfixedformattype
I have a small project where data from Oracle SQL Developer needs to be exported to Excel (using commands rather than tools in SLQ Developer), then create a graph.
Using "spool" I can export to csv fine (but cant make a graph in csv) but when I try to export to xlsx it corrupts the whole excel sheet saying
"Excel cannot open the file "ExcelFile.xlsx" because the file format or file extention
is not valid. Verify that the file has not been corrupted and that the
file extension mathces the format of the file."
Here is the code I used in SQL Developer.
spool FileLocation\ExcelFile.xlsm
SELECT * FROM Table;
spool off;
Is there any way I can stop the data from becoming corrupted or is there another way to export data to a .xlsx file?
Nooooooo.
set sqlformat csv
spool c:\file.sql
select * from table;
spool off;
then open the file in excel.
OR
Run your query interactively.
Right click on the grid, Export > XLSX. Open the file.
Spool only writes the query output to the file, it doesn't look at the file extension and figure out HOW to write the output at that point.
So you either have to code it yourself via the query, or use one of the format outputs we support
SET SQLFORMAT
CSV
JSON
DELIMITED
XML
HTML
INSERT
LOADER
Use 'help set sqlformat' for help.
Hi sql developer from what I know for exporting is using sqlplus(code is same) so perhabs there are other ways but this one should be good enough
I would try changing first line to look like this:
spool ExcelFile.xls
Probably you also need to turn on
SET MARKUP HTML ON
http://www.orahow.com/2015/09/spool-sqlplus-output-to-excel-format.html
Anyway there is workaround - you can just generate .CSV file and then open it in excel and save as .xlsx file
I was also facing the same problem then applied below code and it exported successfully..
import xlsxwriter
from xlsxwriter import Workbook
import cx_Oracle
import datetime
from datetime import date
dsn_tns = cx_Oracle.makedsn('HOST', 'PORTNO', sid='BGRDB')
db = cx_Oracle.connect(user=r'username', password='password', dsn=dsn_tns)
cursor = db.cursor()
workbook = xlsxwriter.Workbook('C:/Path/outfile.xlsx')
sheet = workbook.add_worksheet()
cursor.execute("select * from TABLENAME")
for r, row in enumerate(cursor.fetchall()):
for c, col in enumerate(row):
sheet.write(r, c, col)
workbook.close()
cursor.close()
I have several 20+ MB Excel files, and they need to be refreshed every week before business starts (Monday 8 AM).
These files contain one Data sheet, and data comes via external connection (ODC file), from an SQL Server view.
They also have one pivot sheet that also needs to be refreshed after the Data sheet is refreshed.
I am trying to find a solution (Windows PowerShell) to automatize the refreshing of Data and Pivot sheets without the need to touch the files.
"Refresh on opening" and other Excel options are not viable because it takes up to 20 minutes to refresh all the connections.
I also don't want to refresh ALL sheets because the file has custom coloring for charts and "Refresh" resets it to Excel default which cannot happen.
I tried this, but it doesn't seem to work with ODC connection? At least, it doesn't do anything.:
Windows PowerShell:
$ExcelApp = new-object -ComObject Excel.Application
$ExcelApp.Visible = $false
$ExcelApp.DisplayAlerts = $false
$Workbook = $ExcelApp.Workbooks.Open("c:\test\ref_test.xlsx", 3, $false, 5, $null, $null, $true)
Start-Sleep -s 30
$Workbook.RefreshAll()
$Workbook|Get-Member *Save*
$Workbook.Save()
$ExcelApp.Quit()
Any ideas?
Office version: 2010, on Windows 7
Possibly the answer on this question can help. The perl script is also available as a pre-compiled exe file.
I would approach this issue by using Excel VBA, and create your Excel file into a .xlsm.
Then update the file w/ Excel VBA commands and functions to refresh your odbc connection, and then save as a new file for distribution.
http://www.vbforums.com/showthread.php?675977-Auto-Open-Refresh-Pivots-Save-Close-Excel-files-using-VB
I'm currently presented with the following issue:
I need to open a CSV file with the Excel Interop Classes (15.0). This is done using the following code:
Dim app As New Excel.Application
Dim workbook As Excel.Workbook = app.Workbooks.Open(sFileNameCSV, Format:=4, Local:=True)
Unfortunately this converts some of the data into formulas (e.g. text starting with a hyphen [- this is an example] or phone numbers in international format [+41-555-123-45-67]) resulting in either a #NAME? error or a calculated result in case of the phone number.
After some searching in the web and on SO I tried the following things with no luck:
Saving the CSV File as a .txt file
Using the OpenText() Method instead of the Open() method
Combination of the two above
Is there any solution to this issue without having to change the CSV file data itself and still using the Interop classes, like disabling formulas altogether? Or am I just missing a param in the Open() / OpenText() functions?
I am working on populating a new application full of resident data. I have to export a list of residents for each property. Unfortunatly the new application has to have the residents imported 1 property at a time. This means im stuck loading 200+ properties by exporting an excel file, slightly modifing the data and then importing into the new application.
For each property that is exported I must remove a '-' from the first column and i have to remove all of the ',' throughout the entire document. I then change the formating on a date column to 'mm/dd/yyyy'. Then the document is saved as a CSV and can be imported.
I would like to write a script that can perform the updates to the excel file and save it to a csv. Please advise if this is worth my time. This is a one time load so it might be better to just power through.
Thank You
Possibly a little prematurely, as I'm not certain this is what you want to achieve, but you could try this (save first):
Sub replaceStuff()
ActiveSheet.Range("A:A").Replace "-", ""
ActiveSheet.Cells.Replace ",", ""
ThisWorkbook.SaveAs "doc", xlCSVWindows
End Sub