Can't call method "worksheet" on an undefined value - excel

I have a problem parsing excel file in the same perl code:
I get this error:
"Can't call method "worksheet" on an undefined value at
./parse_pathsim_results.pl line 223"
Interestingly in the perl code I have if I parse another file (delay xls) before the intended slope xls file it works.
Here is the code:
use Spreadsheet::ParseExcel::SaveParser;
$input_delay_csv_file = "./presto/prs/c2x_delay.xls";
$input_slope_excel_file = "./presto/prs/c2x_slope.xls";
$slope_parser = Spreadsheet::ParseExcel::SaveParser->new();
$delay_parser = Spreadsheet::ParseExcel::SaveParser->new();
The code works if I use the following two lines, but I don't want to.
$workbook = $delay_parser->Parse("$input_delay_csv_file");
$worksheet = $workbook->worksheet("Sheet1");
This is where it creates a problem if the above two lines are commented.
$new_workbook = $slope_parser->Parse("$input_slope_excel_file");
$worksheet = $new_workbook->worksheet("Sheet1");

Without you giving much information and only snippets of your code it is hard to say.
The great suspect is the file format itself, as the Parse method does not return a $workbook, the parser returned undef
I recently had a problem like this where the excel file was a quite recent format version. (ending on .xlsx and not .xls) Opening the file with MS Excel and saving as an older excel format did the trick.
Your first file looks as if it was a plain CSV file, and therefore had no problem parsing.

Related

Close an Excel file was opened in Matlab

My code needs to open an Excel file (with formulas) to search for a given value and close this file, will do some calculations and will write the answer in this file.
I can not get Matlab to close the file before it writes the response and this error appears:
Error using xlswrite (line 226)
The file C: \Temp\Data_Home.xlsx is not writable. It might be locked by another process.
File_123 = 'C: \Temp\Data_Home.xlsx';
xlObj = actxserver('Excel.Application'); %Start Excel
wsObj = xlObj.Workbooks.Open(File_123); %Open workbook
Sheet = wsObj.Sheets.Item(3); %Assume we're using the first sheet
Use Quit and delete as mentioned in the example in the documentation of actxserver.
Quit(xlObj);
delete(xlObj);

Writing from Powershell to Excel: How to set the cell format for the value?

I am reading values from different Excel files, and composing a new one containing information from all the others. While doing that, Excel seems to automatically change '.' to a comma ','. How do I prevent that?
I am using Powershell ISE on Win10 and Office365. I tried reading and writing 'value2' and 'text' and writing those. I tried casting the value2 to string when I write it. This did not work. The variables in Powershell hold the correct values as strings. The moment I save the new Excel file, the correct format is gone.
Example: Value is "123.456". I can read it, the Powershell variable shows "123.456". I write it to Excel and open the Excel afterwards, it reads:
123,456 and interprets it as number instead of a text.
How I read the value
[...]
$tmp += ($worksheet.cells.item($intRow,$col).value2)
How I write the value (I tried "value", and "text" for both)
[...]
elseif($value -eq 6){
$sheet.Cells.item($intRow,$columncounter).value2 = ($tmp[$value]).ToString()
}
[...]
This is how I open the excel file for writing:
$objExcel=New-Object -ComObject Excel.Application
$objExcel.Visible=$false
$resultbook = $objExcel.Workbooks.Add()
$sheet = $resultbook.ActiveSheet
$sheet.Name = "Data"
This is how I save the excel file
$resultbook.SaveAs($name)
$resultbook.close()
Expected: Input == Output, example: 1234.5678 --> 1234.5678
Actual Result: Input != Output, example 1234.5678 --> 1234,5678
It works fine for all other strings, texts, numbers except those containing dots.
I presume there must be a way to specify the cell format in the target file, however I did not find any documentation on that.

NPOI produces unreadable content

I'm using NPOI to open an existing Excel file, make modifications, and write it back to disk.
However, when I open the file with NPOI and write it back, the file becomes damaged. Excel complains that the file "contains unreadable content", and asks whether I want to "recover the contents" of the file. When I select OK, it says:
Excel cannot open the file 'test.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.
Here is my code:
var excelFilename = "c:\\temp\\simplefile.xlsx";
IWorkbook wb;
using (var fs = File.OpenRead(excelFilename))
{
wb = new XSSFWorkbook(fs);
}
var sheet = wb.GetSheetAt(0);
// For this sample, we don't make any modifications to the file.
// Just opening and writing it back is enough to produce this error.
using (var str = new FileStream(excelFilename, FileMode.Open, FileAccess.ReadWrite))
{
wb.Write(str);
}
"simplefile.xlsx" is an empty excel workbook created by Excel 2010.
What's happening here?

Using Perl to SaveAsXMLData on Excel 2010 Worksheet

I've an Excel 2010 spreadsheet with an XML map defined within it. Using Perl I want to save the worksheet as XML Data. I do not need to export the XML map file. From within Excel I can select "File > Save As > Save as type : XML Data". This is the output I want to create, but from my Perl script.
I can output the worksheet in CSV format using the SaveAs command with enum 6. I can also output the spreadsheet in XML format using SaveAs with enum 46, but this is not what I want. I want just the XML Data..
There appears to be a SaveAsXMLData function but I'm unable to get it working. Any help appreciated.
use strict;
use warnings;
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';
use Win32::OLE::Variant;
use Win32::OLE::NLS qw(:LOCALE :DATE);
$Win32::OLE::Warn = 3; # Die on Errors.
my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
|| Win32::OLE->new('Excel.Application', 'Quit');
$Excel->{DisplayAlerts}=0;
my $excel_file = 'c:\\temp\\master.xlsx';
my $csv_file = 'c:\\temp\\master.csv';
my $xml_file = 'c:\\temp\\master.xml';
my $workbook = $Excel->Workbooks->Open($excel_file);
# Alt+F11 in Excel to start VBA and after that F2 to start Object browser.
# 6 is CSV format
# 46 is XML spreadsheet
$workbook->SaveAs( $csv_file, 6 );
# Now just the XML Data
# The map is called MDBAC_Map
my $objMapToExport = $Excel->Workbooks->XmlMaps("MDBAC_Map");
$workbook->SaveAsXMLData( $xml_file, $objMapToExport );
$workbook->Close();
$Excel->Quit();
Fixed this myself (I was 99% there!). Using the macro recorder within Excel confirmed the required function calls as follows:
ChDir "C:\temp"
ActiveWorkbook.SaveAsXMLData Filename:="C:\temp\master.xml", Map:= _
ActiveWorkbook.XmlMaps("MDBAC_Map")
The line of code for exporting the XML map is wrong. Changed the above code as follows and the script works fine:
my $objMapToExport = $workbook->XmlMaps("MDBAC_Map");

How to save an Excel sheet as PDF through MATLAB?

I am using the following code:
hExcel = actxserver('Excel.Application');
hWorkbook = hExcel.Workbooks.Open(sprintf('%s','C:\test.xlsx'));
hWorksheet = hWorkbook.Sheets.Item(1);
hRange = hWorksheet.Range('A1:O10');
hRange.ExportAsFixedFormat('xlTypePDF','test_out.pdf');
The Excel ActiveX server allows me to do the usual stuff, but the last line doesn't do anything. Neither does it throw any error.
Does anyone know how to do this?
I think you need to use hWorkbook.ExportAsFixedFormat, not hRange.ExportAsFixedFormat.
Also, you'll need to specify the full path to the output file as C:\test_out.pdf.

Resources