Matlab:奇怪的文件创build时,我在Excel中的单元格

这个代码必须给给定的rgb上色J1单元格。

row_number_excel = 1; representative_red = 205; representative_green = 211; representative_blue = 201; headers = {'J'}; rgb = [representative_red representative_green representative_blue]; %# if you have 0 to 1 values multiply by 255 and round clr = rgb * [1 256 256^2]'; %# convert to long number Excel understands pwd = 'D:\grapes\main'; e = actxserver ('Excel.Application'); % open Activex server filename = fullfile(pwd,'example.xls'); %# full path required if exist(filename,'file') ewb = e.Workbooks.Open(filename); %# open the file else error('File does not exist.') %# or create a new file end esh = ewb.ActiveSheet; for c = 1:row_number_excel str = num2str(row_number_excel); esh.Range(strcat(headers{1},str)).Interior.Color = clr; end ewb.Save ewb.Close(false) e.Quit 

我试图运行这个代码,但是单元格的颜色与给定的rgb不同。 当我第二次运行代码时,我的目录中创build了一个名为“2E60F720”的文件。 它的types是'文件'。 然后程序运行并运行,直到我停止任务pipe理器的“EXCEL.EXE”。 之后,matlab写给我这个:

 "??? Error: The remote procedure call failed. Error in ==> test1 at 212 ewb. Close(false);" 

有人能帮助我吗? 我抓住了屏幕。 在左侧,程序仍在运行,在右侧,这是我的目录。 我标记了创build的文件。 在这里输入图像说明 谢谢!

解决我的问题的解决scheme是:

 row_number_excel = 1; representative_red = 205; representative_green = 211; representative_blue = 201; headers = {'J'}; rgb = [representative_red representative_green representative_blue]; %# if you have 0 to 1 values multiply by 255 and round clr = rgb * [1 256 256^2]'; %# convert to long number Excel understands pwd = 'D:\grapes\main'; e = actxserver ('Excel.Application'); % open Activex server filename = fullfile(pwd,'example.xls'); %# full path required if exist(filename,'file') ewb = e.Workbooks.Open(filename); %# open the file else error('File does not exist.') %# or create a new file end esh = ewb.ActiveSheet; for c = 1:row_number_excel str = num2str(row_number_excel); esh.Range(strcat(headers{1},str)).Interior.Color = clr; end xlWorkbookDefault = 51; %# it's the Excel constant, not sure how to pass it other way ewb.SaveAs(fullfile(pwd,'example2'), xlWorkbookDefault) ewb.Close(false) e.Quit 

感谢@yuk!

问题行是用ewb.Save保存文件。

你的文件实际上是旧格式(excel 2003),不知道它是否支持真正的RGB颜色,这可能是原因。

我build议使用新名称和最新格式保存文件。 把这个作为最后一行:

 xlWorkbookDefault = 51; %# it's the Excel constant, not sure how to pass it other way ewb.SaveAs(fullfile(pwd,'example2'), xlWorkbookDefault) ewb.Close(false) e.Quit