Writing to Excel

1 visualización (últimos 30 días)
Amit Davidi
Amit Davidi el 2 de Feb. de 2012
Editada: faraz el 30 de Sept. de 2013
Hello
I'm trying to write data to an Excel file using xlswrite. If the file does not exist, it should create it. However, when I type the following command:
>> xlswrite('testFile.xls',magic(5))
I get this error:
??? Error using ==> xlswrite at 213
Invoke Error, Dispatch Exception:
Source: Microsoft Excel
Description: 'D:\Public\AmitDavidi\M files\testFile.xls' could not be found. Check the spelling of the file name, and verify that the file location is correct.
If you are trying to open the file from your list of most recently used files, make sure that the file has not been renamed, moved, or deleted.
Help File: xlmain11.chm
Help Context ID: 0
I'm using Matlab 2010b, Office 2010, Windows XP (on another machine with Windows 7 I get the same error). Could it be that xlswrite doesn't work with Office 2010?
Could anyone help please?
Thanks, Amit

Respuestas (2)

Image Analyst
Image Analyst el 5 de Feb. de 2012
I think you built up the filename incorrectly. It looks like you're using WIndows 7 and you can't just write files anywhere you want to - Windows 7 has strict rules for that. I think you forgot "Users" since "Public" is a folder underneath Users, at least it is on the C drive. If D is not a "special" folder, say it's just some ordinary secondary drive, then that's not the problem.
One possibility is that you already have the file open in Excel. Could that be the case? If so, you must close it from Excel before you can write to it from MATLAB.
The only other thing I could think of is to check, with exist(folder, 'dir'), whether the folder exists and to use mkdir() to create the folder in advance of calling xlswrite(). Make sure you check just the folder name, not the whole filename.
  3 comentarios
Image Analyst
Image Analyst el 5 de Feb. de 2012
Well I just tried it and it worked fine for me. I suggest you call the Mathworks. It's weird that the error message sounds like you're trying to read an existing file instead of create one. If you have Excel 2007 or 2010, you might try using .xlsx as the extension.
Friedrich
Friedrich el 6 de Feb. de 2012
This is clearly an EXCEL issue rather than MATLAB. Amit stated that even the VBS code doesnt create the file. So, something prevent EXCEL for writing the file.

Iniciar sesión para comentar.


Friedrich
Friedrich el 2 de Feb. de 2012
Hi,
seems more like an EXCEL issue rather than ML. Did you try to write an excel file to that folder via COM outside of MATLAB? easiest way would be some VBS code like this:
Set Excel = CreateObject("Excel.Application")
Excel.visible = 1
Set wb = Excel.Workbooks.Add
Set add_sheet = wb.Sheets.Add
wb.Sheets.Item(1).Activate
wb.Sheets.Item(1).Name = "test text"
Excel.Range("A1").Select
Excel.Selection.Value = 10
wb.SaveAs("D:\Public\AmitDavidi\M files\testFile.xls")
MsgBox "Done"
wb.close(0)
Excel.Quit
Copy that file in a something.vbs file and double click to run it. Does this work?
  1 comentario
Amit Davidi
Amit Davidi el 5 de Feb. de 2012
Hi Friedrich,
Thanks a lot for your reply!
I've tried your VBS code. It gederated an Excel file and wrote in it, but didn't save the file.
However, I've tried to use again xlswrite on a different machine, which has office 2007 (instead of 2010), and it worked perfectly.
Thanks again,
Amit

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by