Reading from many columns of an excel sheet
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I am using the following code to read from multiple columns of an excel sheet. Can someone help me to reduce this code?
ex = actxserver('excel.application');
ex.visible = 1;
exwb = ex.Workbooks.Open('C:\Sgpa.xls');
exSheet1=exwb.Sheets.Item('Marks');
exSheet2=exwb.Sheets.Item('Points');
%ex.Sheet1.Range.invoke;
for i=5:324
val = exSheet1.Range(['J',num2str(i)]).get('Value');
if isnumeric(val) && isfinite(val)
exSheet2.Range(['J',num2str(i)]).set('Value',Points(val));
end
val = exSheet1.Range(['K',num2str(i)]).get('Value');
if isnumeric(val) && isfinite(val)
exSheet2.Range(['K',num2str(i)]).set('Value',Points(val));
end
val = exSheet1.Range(['L',num2str(i)]).get('Value');
if isnumeric(val) && isfinite(val)
exSheet2.Range(['L',num2str(i)]).set('Value',Points(val));
end
val = exSheet1.Range(['M',num2str(i)]).get('Value');
if isnumeric(val) && isfinite(val)
exSheet2.Range(['M',num2str(i)]).set('Value',Points(val));
end
val = exSheet1.Range(['N',num2str(i)]).get('Value');
if isnumeric(val) && isfinite(val)
exSheet2.Range(['N',num2str(i)]).set('Value',Points(val));
end
val = exSheet1.Range(['O',num2str(i)]).get('Value');
if isnumeric(val) && isfinite(val)
exSheet2.Range(['O',num2str(i)]).set('Value',Points(val));
end
end
exwb.Save;
ex.Quit;
ex.delete;
0 comentarios
Respuestas (1)
Fangjun Jiang
el 25 de Nov. de 2011
Can you explain why you don't use xlsread() directly?
A=rand(10); xlswrite('test.xls',A,'FirstSheet','B2');
B=xlsread('test.xls','FirstSheet','C3:E5');
5 comentarios
Fangjun Jiang
el 26 de Nov. de 2011
"does not work" really doesn't describe anything. See update. You may need to use [Num, Txt, Raw]=xlsread() for different types of data in the Excel file. All the rest should be straightforward. See doc for help.
Ver también
Categorías
Más información sobre Spreadsheets en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!