如何在矩阵/Excel电子表格中添加任意零行? (Matlab) [英] How to add arbitrary rows of zeroes in matrices/excel spreadsheet? (Matlab)
问题描述
我有一个包含3列和212行数据的XLS文件.有没有办法在中间插入3列零个零行和x个行,所以总共有365行?
I have a XLS file with 3 columns and 212 rows of data. Is there any way to insert 3 columns of zeroes with x number of rows in the middle, so there is a total of 365 rows?
推荐答案
另一种方法是通过excel activex控件.
Another way you can do this is via the excel activex control.
以下内容假设您的Excel传播范围为c:\ temp \ zeros.xlsx.
The following assume you have an excel spread c:\temp\zeros.xlsx.
它从第3行开始插入5行,每行有3列零.
It inserts 5 rows each with 3 columns of zeros starting at row 3.
很明显,您可以调整分配给insertRow和insertRowCount的值以满足您的特定要求.
Obviously, you can adjust the values assigned to insertRow and insertRowCount to meet your specific requirements.
或者,您也可以使用复制/插入,但这应该足够了.
Alternatively, you could use copy/insert instead but this should be good enough.
% Create Excel ActiveX control (and therefore application) and make it visible
excel = actxserver('Excel.Application');
excel.visible = 1;
% Open the the spreadsheet
workbook = excel.Workbooks.Open('c:\temp\zeros.xlsx');
sheet = workbook.ActiveSheet;
% Insert rows
insertRow = 3;
insertRowCount = 5;
range = get(sheet,'Range', sprintf('A%d:C%d', insertRow, insertRow ) );
for r=1:insertRowCount
range.Insert()
end
% Set cell values on inserted rows
cellValue = 0;
for r=1:insertRowCount
for c=1:3
set(get(sheet,'Cells',r+insertRow-1,c),'Value',cellValue);
end
end
这篇关于如何在矩阵/Excel电子表格中添加任意零行? (Matlab)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!