I have a simple piece of code,that is not loading the excel file. Can't figure out what I'm doing wrong :
my $parser = Spreadsheet::ParseExcel->new();
my $workbook = $parser->parse("test.xlsx");
if ( !defined $workbook ) {
print("Unable to load workbook \n");
}
I also tried running with the full path of the file.
The test.xlsx is an Excel 2013 file
I am running in Windows with Perl 5.14.2 and the file is in the same folder as the .pl file.
From the documentation for Spreadsheet::ParseExcel:
The Spreadsheet::ParseExcel module can be used to read information from Excel 95-2003 binary files.
The module cannot read files in the Excel 2007 Open XML XLSX format. See the Spreadsheet::XLSX module instead.
Related
As many of you are aware, PowerShell can now be installed and used on Linux and it has worked surprisingly well for me so far. However, I wonder if the content of an Excel file can be read given that Excel cannot be installed on Linux. What I have found online is that the PS script to open an Excel file is:
# Script copied and pasted from https://stackoverflow.com/questions/48443536/how-to-read-excel-files-in-powershell
$excel = New-Object -com excel.application
$wb = $excel.workbooks.open("c:\users\administrator\my_test.xls")
But I get the error:
New-Object: A parameter cannot be found that matches parameter name 'com'.
When I remove the parameter and run the script, this is the error I get:
New-Object: Cannot find type [excel.application]: verify that the assembly containing this type is loaded.
Since I'm on Linux (ubuntu 22.04), I can use xls2csv on bash to convert the Excel file to csv and then use Get-Content on PowerShell. But I would like to know if there is a way to do it on PowerShell since I am currently learning it.
Thank you.
I am grateful to #Olaf who pointed out the ImportExcel module from Doug Finke.
After installing the module with:
Install-Module -Name ImportExcel
importing the file is as easy as:
$data = Import-Excel c:\users\administrator\my_test.xls
I'm using the below command which is intended to convert PDF files to XLSX format
soffice --infilter="writer_pdf_import" --convert-to xlsx:"Calc MS Excel 2007 XML" excel.pdf --outdir test.xlsx
But I'm getting an error message which states Application Error. I need to know what is wrong with my command and solution to convert PDF files to XLSX.
I wanted to write a code to convert automatically a huge number of .txt files into .xlsx files (Excel). I saw a code at this page Powershell Script to convert txt to xlsx
and I modified just the name and the link of the folder. But when trying to run the code I get this error message:
Error: unexpected '$' in "$"
This is the code:
$xlFixedFormat = Microsoft.Office.Interop.Excel.XlFileFormat]::xlOpenXMLWorkbook
$wb.SaveAs("C:\Users\wascal\Desktop\transfo\Test.xlsx”, $xlFixedFormat)
Could someone explain me the cause of the error help me to solve the issue?
Thank you in advance.
There is a missing [ in :
$xlFixedFormat = Microsoft.Office.Interop.Excel.XlFileFormat]::xlOpenXMLWorkbook
correct code should be
$xlFixedFormat = [Microsoft.Office.Interop.Excel.XlFileFormat]::xlOpenXMLWorkbook
Note that your
$wb.SaveAs("C:\Users\wascal\Desktop\transfo\Test.xlsx”, $xlFixedFormat)
Has another double quote at the end of the string.
I have a folder with many subfolders (1000+) and inside the subfolders are a few excel files, in .xlsx format. I want to run a batch script from the main folder, which goes into each sub folder and changes each .xlsx Excel file into a .csv Excel file.
The formatting of the file system:
MainFolder
---subfolder1
----->file1.xlsx
---subfolder2
----->file2.xlsx
etc.
From this thread: Convert XLS to CSV on command line, I figured out that I can't just rename each .xlsx to .csv, so I made the ExceltoCSV.vbs file (the modified ScottF answer, second one down). My understanding is that I can make a for loop in a batch file to run ExceltoCSV.vbs for each sub folder. The following line of code is something i attempted, but it didn't work. It usually says *.xlsx file is not found.
for /R %%F in ("C:\MainFolder") do cscript ExceltoCSV.vbs *.xlsx *.csv
Also tried this:
for /d /r "C:\MainFolder" %%F in (.) do cscript ExceltoCSV.vbs "%%F\*.xlsx" *.csv
Error message:
C:\MainFolder\ExceltoCSV.vbs(17,1) Microsoft Excel: 'C:\MainFolder\folder1\*.xlsx' could not be found. Check the spelling of the file name, and verify that the file location is correct.
The VBS script you got from SO only takes a string as an input. What you'll need to do is create a loop with a regex pattern that builds those file names dynamically and then run the script with an input string representing each file name. Pseudo code:
for loop (iterate over file names in dir){
set $file_name=$iterated_file_name_from_loop
ExceltoCSV.vbs "$file_name.xlsx" *.csv
}
I have a batch file with the statement below. The export works fine and results are in the file Corp.xls. However, when I try to open this file, I get a warning 'The file you are trying to open is in a different format that that specified by the file extenion ..........
When I open the file and try to 'Save As', I find that it is in Text-tab delimted format.
Is there any way to convert such a file to excel without having to open the file - i.e from the batch file ?
Note: The batch file is very comples. Given below is just a modified snippet.
BCP "exec DBname.dbo.sp_abc '201503' " queryout "\\ABC\3_MAR\Corp.xls" -T -c -S SCC-SLDB