How do you write an excel sheet, and add input to it everytime program is ran?

I'm having trouble trying to write an excel spreadsheet that starts with 3 rows of data, but every time the program is ran, new data is input by user and needs to be added to the spreadsheet. how do i do this?

 採用された回答

Matt Tearle
Matt Tearle 2011 年 3 月 12 日
Brute-force approach
[~,~,C] = xlsread('file.xls');
<do stuff to create new data>
<make cell array C2 out of new data>
C = [C;C2];
xlswrite('file.xls',C);
If this is likely to get unwieldy, you could either store information on the number of rows somewhere (like a MAT-file), then use xlswrite to write into a particular range, or you could dive into lower-level ActiveX commands to read/write to Excel.

6 件のコメント

Bob
Bob 2011 年 3 月 12 日
this would work, but the program has to make the excel sheet too, it cant be premade.
Oleg Komarov
Oleg Komarov 2011 年 3 月 12 日
Have you tried the code suggested by Matt?
xlswrite will create a standard excel file with Sheet1,Sheet2 and Sheet3 and will write on the first one if not specified any different one.
Or you can write on a differently named sheet as:
xlswrite('tryThisOut.xlsx',1,'myName')
Matt Tearle
Matt Tearle 2011 年 3 月 12 日
Ok, so add "if exist('file.xls','file')" before the xlsread command, with an "else C = {};"
Bob
Bob 2011 年 3 月 13 日
It works, thanks so much!
Deepanshu
Deepanshu 2011 年 3 月 14 日
Can you please add more to this answer to elaborate this program.
Suppose i generate some data and i wish to add it to a file
every time i run the code over the image.
then what i need to do.
Deepanshu
Deepanshu 2011 年 3 月 14 日
I mean to say that data must be appended to the same file not overwrite.

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

その他の回答 (0 件)

質問済み:

Bob
2011 年 3 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by