Opening Excel File with ClosedXML throws System.ArgumentNullException - openxml

Every time I try to open an Excel file with ClosedXML with the following code
XLWorkbook spreadsheetDocument = new XLWorkbook("Sample.xlsx");
I get a System.ArgumentNullException. However, I made a copy of that file in Excel, I renamed it and I have opened it succesfully. I compared both files using the Open XML Productivity Tool and I got the following output.
I wonder if those differences have something to do with the fact that I cannot open Sample.xlsx using ClosedXML. Thank you for your help.

Related

Open file local to function file in Octave

I'm trying to get a MATLAB project work under Octave. The project's layout looks like this:
/myproject/funcs/f1.m
/data.csv
/examples/test.m
I want to run the routine test.m, which makes use of the function f1. Inside f1.m, there's a call to open the data file:
[...]
data = dlmread('data.csv'); % Read the csv file
[...]
When I'm inside the examples directory, this fails:
error: dlmread: unable to open file 'data.csv'
So I guess what I'm looking for is some way to tell Octave that the file data.csv is in the same directory as the function definition trying to open it. This solution should be MATLAB-compatible, so I can contribute the changes necessary to make it work under Octave back to the project.
What I already tried:
data = dlmread('./data.csv'); -> the same error as before

SQL Developer won't attempt to import an .xlsx file because it's too large

I have two .xlsx files that total 1.6 million rows, and I'm trying to import these things into SQL Developer.
I right click the table name and select "Import Data..." and then select my file and nothing happens. It logs my attempt to open this file in the "File - Log" output
This is two separate attempts to import the same file logged here. When I click one of them, I get the following message:
However, I know that this warning is not true, because my attempts with importing a smaller .xlsx file are successful. So I figured the problem was just the file size is too large, and tried to change the memory available. I went into "C:\Users\User\Documents\sqldeveloper\sqldeveloper\bin" and changed sqldeveloper.conf to change one existing value to
AddVMOption -XX:MaxPermSize=2048M
and added another value
AddVMOption -Xmx2048M
Which helps the Java VM according to this source:
http://codechief.wordpress.com/2008/07/30/configuring-oracle-sql-developer-for-large-files-fix-out-of-memory-errors/
But this did nothing for me, and I still receive the same errors. I am using SQL Developer version 3.2.20.09 but I have also tried this on 4.0.1.14 to the same effect.
Many thanks!
I tried looking into SQL*Loader. Apparently you should be able to right click a table > Import Data > next and there should be an option to generate SQL*Loader files.
Unfortunately, not only did the import wizard not open with my large .xlsx files, the SQL*Loader option was not even present for smaller .xlsx files, or even .xls for that matter.
In the end, I decided to convert my .xlsx files into .csv and import those instead. This worked for all but 4 rows of my 1.6 million, and gave me the insert statement for those 4, of which 2 worked when run with no additional modifications.
In case of large volume of data, add a line "AddVMOption -Xmx4096M" to the sqldeveloper.conf file and even if it is not working change your file to *.csv format instead of *.xls. It will work fine.

Error using the User Defined function of Excel in Matlab [duplicate]

This question already exists:
Closed 10 years ago.
Possible Duplicate:
Excel Addin Error #NAME?
I think it is a follow-up query to my earlier reported issue concerning User defined function in Excel.
I am able to use the function in Excel when used Manually, but when I write to an excel file using Matlab using xlswrite, it gives an error #NAME?
I am attaching the screenshots of the both when entered manually and when using the function through Matlab.
Thanks
EDIT :
Thanks a lot. I have stored the VBA function as an Excel addin here :
C:\Users\Administrator\AppData\Roaming\Microsoft\Addins
Here is what I saw about Excel Add-ins not loaded when used in Automation :
http://www.excelforum.com/excel-programming/472145-calling-excel-macro-from-vb-6-app-problem.html
I am attaching the small snippet of the code from chi_squared() here :
Function Chi_Squared(act, exp, Optional df)
This is how I write to an excel file in Matlab :
Formula_chisqr={[ '=chi_squared(' 'O2:O22' ',' 'M2:22' ')']};
[status, message] = xlswrite1(ExcelFilename,Formula_chisqr,sheetname, Location_Agg);
I also tried giving the complete path as suggested. But it did not work.
Thanks
You have to specify the workbook where the UDF defined. Even if your function is it your PERSONAL.XLSB file. You don't have to do it only if UDF is defined in the same file where you use it.
For example,
='myFunctions.xlsb'!chi_squired(O2:O21,P2:P21)
If myFunctions.xlsb is not opened you may need to specify full path to the file.
If you want to be able to call UDF in any file without specifying the file name, you need to save the file as Add-in, and then enable it in Options - Add-ins - Manage Add-ins.
Another idea: When you use XLSWRITE provide file name with extension XLSX, like test.xlsx, not just test. By default MATLAB saves files with XLS extention in older format. It looks like when you open such file in newer version of Excel (2007/2010) the compatibility mode does not allow macros or UDFs to run.
When you open the Excel as a COM server, the UDF's and Add-ins are not loaded by default. So, you need to first load the add-ins if you would like to use the add-in your Excel file.
Here is the small code snippet in Matlab which loads the add-ins before opening the Excel file.
Excel = actxserver ('Excel.Application');
Excel.Workbooks.Open('C:\YourAddInFolder\AddInNameWithExtension');
Excel.Workbooks.Item('AddInNameWithExtension').RunAutoMacros(1);
File='C:\YourFileFolder\FileName';
if ~exist(File,'file')
ExcelWorkbook = Excel.Workbooks.Add;
ExcelWorkbook.SaveAs(File,1);
ExcelWorkbook.Close(false);
end
Excel.Workbooks.Open(File);
Source : Mathworks

How to open a file that is already open with fileshare permissions

I'm trying overwrite a csv file that already exists in Matlab on Windows. The problem is sometimes I have the file open in Excel. When this is the case, the write operation fails.
Is there anyway to overwrite the file in Matlab? I do NOT want to use ActiveX to connect to the Excel session and edit the file that way.
Seeing this post about FileShare settings made me think it might be possible, but none of the fopen parameters seem to be able to do the trick. Excel may be locking the file with exclusive write access, in which case there is no getting around it. Does anyone know how to check this?
Example of problem:
% make csv file
x = magic(4);
csvwrite('foo.csv', x);
% open foo.csv in EXCEL
% try writing again
csvwrite('foo.csv', x); % cannot write a new file
[fid, msg] = fopen('foo.csv' ,'w'); % cannot open handle for writing
As a side note, I used to be able to overwrite a file open in Excel when the file existed on a Linux box, and I had the file open over the network on a Windows box.
Those FileShare settings are for files opened from the Win32 API's CreateFile family of functions, not the C style fopen family, which Matlab exposes. The fopen options won't get you there. See http://support.microsoft.com/kb/99173 for a quick rundown of the differences. (If you really wanted to use CreateFile or other Win32 I/O, e.g. to check to see if Excel has the file locked or lock it yourself, you could call it from Matlab through .NET using System.IO.File.)
Regardless, by default, Excel opens the file in write mode, and gets an exclusive (write) lock. So you couldn't open it for writing anyway, those file sharing settings would only let you open the file for reading. If you want to be able to overwrite an Excel file while it's open in Excel, you need to have Excel open it in read-only mode like katrasnikj suggests. This causes Excel to read it in to memory once and then release the filehandle.
Try turning on the Read-Only file attribute on these files after you write them by shelling out to the attrib command. This will cause Excel to open them read-only by default. Then clear the read-only attribute right before opening it from Matlab for rewriting.
if exist(file, 'file')
[status,result] = system(sprintf('attrib -R "%s"', file));
end
[fid,msg] = fopen(file, 'w');
% ... write the file and close it ...
[status,result] = system(sprintf('attrib +R "%s"', file));
There's still a race condition between reader and writer, but if you write the file fast, it'll be a short window. Better would be to write out the csv to a temp file in the same directory, and then just turn off the read-only attribute long enough to swap the new file in to place with a movefile or java.io.File.renameTo. Still a race, but probably good enough to use in practice.
You could also change the permissions on the directory the files are in so your writer process has Modify permissions but the users running Excel only have Read access. Then the Excels will always open read-only and you don't have to fiddle with file attribs.

How to most effectively automate repetitive Excel task?

I want to automate Excel using Perl to do the following task(s):
For a list of Excel .xls files, do the following:
Open the file
Set Format to CSV
Save the file under the original filename and directory, but replace the extension "xls" with "csv"
Close the file
End
I found how to open files, even how to save them. I did not find how to change the fileformat/save as a different format. There shall be no user dialogs popping up, it should be fully automated. The Excel file list I can generate myself, a parameterized "find" or maybe "dir" should suffice.
If you are using Excel automation a great help is Excel itself. Use the VBA environment (Alt+F11) to get help for the Excel objects you want to use.
The objectbrowser (F2) is very valuable.
Workbook.SaveAs([Filename], [FileFormat], [Password], [WriteResPassword], [ReadOnlyRecommended], [CreateBackup], [AccessMode As XlSaveAsAccessMode = xlNoChange], [ConflictResolution], [AddToMru], [TextCodepage], [TextVisualLayout], [Local])
Searching for CSV in the object browser will show Excel constants with their values, since you probably cannot use these Excel constants in Perl.
See Spreadsheet::ParseExcel and xls2csv, they will help you.