Converting .xls and .xlsx to .csv

조회 수: 73 (최근 30일)
mike
mike 2014년 12월 7일
댓글: Image Analyst 2020년 7월 22일
Hey guys,
I was wondering if there was a way I could ask a user if their file is .csv, and if not, I would sent it through a loop to have it converted to .csv. Is there any way I could do that?
Thanks!

채택된 답변

Image Analyst
Image Analyst 2014년 12월 7일
편집: Image Analyst 2014년 12월 7일
Why not assume the extension is correct and process it if necessary?
[folder, baseFileName, extension] = fileparts(filename);
if strcmpi(extension, '.xlsx')
numbers = xlsread(filename);
csvFileName = strrep(filename, '.xlsx', '.csv');
csvFileName = strrep(csvFileName, '.xls', '.csv');
csvwrite(csvFileName, numbers);
end
  댓글 수: 5
Vikas Saroha
Vikas Saroha 2020년 7월 22일
But it writes only numbers not including row and colomn headers. How these can be included in the .csv file?
Image Analyst
Image Analyst 2020년 7월 22일
You can use fprintf() to write it out exactly as you want.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Harish TV
Harish TV 2017년 3월 17일
clear; clear all; fileip='filename.csv'; g=char(fileip); g=g(1:end-4) fileop=horzcat(g,'.xlsx') g=fileip(1:end-4); [~,~,F]=xlsread(fileip); F=cellstr(F); %delete the header rows (4 rows) F([1:4],:)=[]; for a=1:size(F,1); b=F(a,1); c=char(b); D(a,:)=strsplit(c,','); end xlswrite(fileop,D); disp(['--------------Process complete---------------------']);

sapna kumar
sapna kumar 2019년 1월 11일
file = dir('*.xlsx'); % make sure your are navigated to the right folder on matlab
s= size(file,1);
for i= 1:s
Data = xlsread(file(i).name);
filename=file(i).name;
filename= filename(1:end-5); % to remove extension from filename
csvwrite([filename '.csv'], Data);
end

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by