Delete specific rows in excel
27 次查看(过去 30 天)
显示 更早的评论
Hi,
I want to delete rows from an excel file before writte in to it, starting row no 9 till end. I want this for initialize a new file but to keep the first 8 rows for labels and other stuff. The file model is attached.
I tried many ways with no succes, including the one below:
data = xlsread(test.xlsx);
data(9, :) = [];
delete(test.xlsx); % So extra rows on the end of old worksheet don't stay there after writing new data.
xlswrite(test.xlsx);
Thanks!
0 个评论
采纳的回答
dpb
2022-5-30
Use read/writematrix instead
data=readmatrix('text.xlsx');
writematrix(nan(size(data)),'test.xlsx',"WriteMode",'inplace','Range','A9','UseExcel',1,"PreserveFormat",1)
There are no builtin MATLAB functions that will read/return the textbox you've got stuck in that header region so you've got to blank out the other data that is in the file to do what you want with prepackaged functions.
Be a lot simpler to just create a template workbook and use it to create the new file from...
There are user-written functions on FEX and some have been posted here that use the ActiveX interface and package functions for some low-level operations such as this, but I don't have a specific link at hand...well, let's see -- I do have a utility package I did download some time back I've fiddled with some...let's see what's in it, I forget.
Excel = actxserver('Excel.Application')
d=dir('test.xlsx')
Workbook = Excel.Workbooks.Open(fullfile(d.folder,d.name))
Excel.Range('A9:E34').Select
Excel.Selection.Clear
Excel.ActiveWorkbook.Save
Excel.ActiveWorkbook.Close(false)
delete(Excel); clear Excel
is the basics -- I just used a hardcoded range; you'd have to figure that range out dynamically if not known a priori. There's a function that will return the used range in a sheet that could be used. To use ActiveX, one has to just pore through the VBA help files and find the functions of use and figure out what Excel syntax can be used w/ ActiveX; much that works in VBA doesn't translate over owing to there not being the VBA compiler to translate syntax.
2 个评论
dpb
2022-5-30
The app has got to be able to know/find whatever file it's going to be writing to, so you'll have to have some way to determine that.
As far as the GUI, if you package an app, files that it needs are packaged with it so the template will be there.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!