xlswrite - clear cells and write multiple values
이전 댓글 표시
Hi,
I have a Matlab code write on to an existing excel file. I need to do this in order to perform certain calculations and formatting for easy printing.
Each time I run the code, the size of the array may vary. I need to clear the cells before I write, in order to avoid values from previous run to stay in the sheet. How can I do this? Filling it with spaces [' '] is not an option as it messes with the print settings(will print blank pages).
Also, currently I have the code use xlswrite function multiple times. This is slow because it opens, writes and closes the file each time. Is there a way to write several variables on to specific ranges in the excel sheet faster?
Thanks!
댓글 수: 2
Andy
2011년 5월 18일
You said in a comment that your Excel sheet has formatting that needs to be preserved and calculations (cells with formulas in them?) that needs to be visible. If this is the case, I think you must have some constraint on the size of the data you're writing that you haven't shared with us. (E.g., how do you know you aren't overwriting the cells that contain your calculations? And how do the cells which contain your calculations know what range of data to look at?) I think we need a bit more information.
Prajwal Bhat
2011년 6월 1일
답변 (1개)
Oleg Komarov
2011년 5월 18일
The easiest would be to write to a new sheet (see xlswrite options)
Or using activex as explained in this post (clears all sheets for specified xlsx):
EDIT
% Name of the excel file
filename = 'C:\Users\<yourUser>\Documents\abc.xls';
% Open Excel as a COM Automation server
Excel = actxserver('Excel.Application');
% Open Excel workbook
Workbook = Excel.Workbooks.Open(filename);
% Clear the content of the sheet
Workbook.Worksheets.Item('Sheet1').Range('B2:C4').ClearContents
% Now save/close/quit/delete
Workbook.Save;
Excel.Workbook.Close;
invoke(Excel, 'Quit');
delete(Excel)
댓글 수: 7
Prajwal Bhat
2011년 5월 18일
Oleg Komarov
2011년 5월 18일
Then try the other option described in the link. First backup your file as I am not sure at the moment what happens to formatting. Gonna test in a min
Oleg Komarov
2011년 5월 18일
Use the following cellfun line to ClearContent only:
cellfun(@(x) Excel.ActiveWorkBook.Sheets.Item(x).Cells.ClearContents, sheetNames);
Prajwal Bhat
2011년 6월 1일
Oleg Komarov
2011년 6월 1일
See edit
Harry Commin
2012년 1월 12일
If the xls file is in the current directory, I found that xlsinfo() will locate it by filename alone, but Excel.Workbooks.Open() needs the full path:
Workbook = Excel.Workbooks.Open([pwd '\' filename]);
stephen brewis
2019년 1월 2일
try this simple and it works
delete 'filename.xlsx'
카테고리
도움말 센터 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!