フィルターのクリア

Writing to Excel

2 ビュー (過去 30 日間)
Amit Davidi
Amit Davidi 2012 年 2 月 2 日
編集済み: faraz 2013 年 9 月 30 日
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

回答 (2 件)

Image Analyst
Image Analyst 2012 年 2 月 5 日
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 件のコメント
Image Analyst
Image Analyst 2012 年 2 月 5 日
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 2012 年 2 月 6 日
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.

サインインしてコメントする。


Friedrich
Friedrich 2012 年 2 月 2 日
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 件のコメント
Amit Davidi
Amit Davidi 2012 年 2 月 5 日
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

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by