seashellingolds 发表于 2009-10-23 08:49

怎么删除excel旧的已有sheet

各位大大
我想把数据输出到xls文件的sheet中,自己定义名字
xlswrite可以将输出写到当前已有sheet,怎么有办法改名呢?
或是添加新sheet用我定义的名字,怎么删除旧的已有的sheet呢?

非常感谢

[ 本帖最后由 ChaChing 于 2009-10-24 10:41 编辑 ]

friendchj 发表于 2009-10-23 11:43

xlswrite(filename, M, sheet) writes matrix M to the specified worksheet sheet in the file filename. The sheet argument can be either a positive, double scalar value representing the worksheet index, or a quoted string containing the sheet name. The sheet argument cannot contain a colon.

seashellingolds 发表于 2009-10-23 13:20

帮助文件我看过了,主任引用的第一句话说写入到指定工作表中,意味着无法修改工作表名称? 要想让工作表名称符合自己要求必须创建新表而保留着旧表无法从matlab里删去?

ChaChing 发表于 2009-10-23 14:06

From help
If sheet does not exist, a new sheet is added at the end of the worksheet collection.

seashellingolds 发表于 2009-10-23 14:11

主任们所言都是事实,我的问题还是没有解决
目前我认为
已存在sheet无法改名
只能靠添加sheet来进行命名,但默认生成sheet也无法删除
总而言之,matlab不能解决我提的问题?
手动进excel解决
就此封贴吧

ChaChing 发表于 2009-10-23 14:52

"当前已有sheet改名, 删除旧的已有的sheet?"
个人好奇又不才, 可问为何有此需求?

seashellingolds 发表于 2009-10-23 15:48

其实我就想1.把数据导入一个sheet,2. 让这个sheet有我起得名字, 3.多余的sheet不要
1. 主要目的
2. 就像书的每个章节都有个题目,不能只叫Chapter 1, Chapter 2.....
3. 同上,不想书Chapter 1,2,3都是空的,从Chapter 4 开始写,前面占用的资源浪费,且不方便阅读

ChaChing 发表于 2009-10-23 18:42

数据导入一个sheet并依LZ意思进行命名, 进入Excel删除不必要的sheet, 之后新sheet亦可依LZ意思进行命名!
好像仅能这样了!:loveliness:

friendchj 发表于 2009-10-24 05:07

example:
clc
clear
a=1:5;
xlswrite('my.xls',a,'myown');
DeleteEmptyExcelSheets('my.xls')

Reference:http://www.mathworks.com/matlabcentral/newsreader/view_thread/257376

% DeleteEmptyExcelSheets.m
% DeleteEmptyExcelSheets: deletes all empty sheets in an xls-file
%
%==========================================================================
% Version : 1.0
% Author : hnagel
% Date : 27/04/2007
% Tested : 02/05/2007 (DR)
%==========================================================================
%
% This function looped through all sheets and deletes those sheets
%that are
% empty. Can be used to clean a newly created xls-file after all
%results
% have been saved in it.
%
% References: Torsten Jacobsen, "delete standard excel sheet"
%---------------------------------------------------------------------
%
% Input:
%
% fileName: name of xls file
%
%---------------------------------------------------------------------
%
% Output:
%
% none
%
%---------------------------------------------------------------------
%
% See also XLSWRITE
%---------------------------------------------------------------------
% Changes
%---------------------------------------------------------------------
%
% Name :
% Date :
% Description:
% Indicated :
function DeleteEmptyExcelSheets(fileName)
% Check whether the file exists
if ~exist(fileName,'file')
error();
else
% Check whether it is an Excel file
typ = xlsfinfo(fileName);
if ~strcmp(typ,'Microsoft Excel Spreadsheet')
error();
end
end
% If fileName does not contain a "\" the name of the current path is
% added to fileName. The reason for this is that the full path is required
% for the command "excelObj.workbooks.Open(fileName)" to work properly
if isempty(strfind(fileName,'\'))
fileName = ;
end
excelObj = actxserver('Excel.Application');
excelWorkbook = excelObj.workbooks.Open(fileName);
worksheets = excelObj.sheets;
sheetIdx = 1;
sheetIdx2 = 1;
numSheets = worksheets.Count;
% Prevent beeps from sounding if we try to delete a non-empty
% worksheet.
excelObj.EnableSound = false;
% Loop over all sheets
while sheetIdx2 <= numSheets
% Saves the current number of sheets in the workbook
temp = worksheets.count;
% Check whether the current worksheet is the last one. As there
% always
% need to be at least one worksheet in an xls-file the last sheet
% must
% not be deleted.
if or(sheetIdx>1,numSheets-sheetIdx2>0)
% worksheets.Item(sheetIdx).UsedRange.Count is the number of used
% cells.
% This will be 1 for an empty sheet. It may also be one for
%vcertain other
% cases but in those cases, it will beep and not actually delete
% the sheet.
if worksheets.Item(sheetIdx).UsedRange.Count == 1
worksheets.Item(sheetIdx).Delete;
end
end
% Check whether the number of sheets has changed. If this is not
% the case the counter "sheetIdx" is increased by one.
if temp == worksheets.count;
sheetIdx = sheetIdx + 1;
end
sheetIdx2 = sheetIdx2 + 1; % prevent endless loop...
end
excelObj.EnableSound = true;
excelWorkbook.Save;
excelWorkbook.Close(false);
excelObj.Quit;
delete(excelObj);
return;

seashellingolds 发表于 2009-10-24 15:29

这都让你给找到了,赞一个~~~ 多谢多谢,Matlab依旧还是强大滴
worksheets.Item(sheetIdx).Delete; 这句将空表删掉,如果改成worksheets.Item(sheetIdx).Move啥的或许会实现改名的效果
我顶一下,看有没牛闲人试试写一个改名的脚本,那真是感激不尽了呀~~

送给ls
小红花+1 理由:回复具有直接解决问题性

[ 本帖最后由 seashellingolds 于 2009-10-24 15:32 编辑 ]

ChaChing 发表于 2009-10-24 21:33

原帖由 seashellingolds 于 2009-10-24 15:29 发表 http://www.chinavib.com/forum/images/common/back.gif
...如果改成worksheets.Item(sheetIdx).Move啥的或许会实现改名的效果 ...

我想楼主也非等闲之辈, 或许楼主有兴趣的话, 不是就可以直接试试看!?
个人以为试试看是最好的方式

friendchj 发表于 2009-10-25 12:20

回复 10楼 seashellingolds 的帖子

一个笨的方法是读入你要修改名字sheet的数据并清空,存到你要重新命名的sheet中,最后删除那个空sheet。

oquicko 发表于 2010-10-11 21:31

好帖,我顶

zhs_163com 发表于 2012-6-24 16:10

friendchj 发表于 2009-10-24 05:07 static/image/common/back.gif
example:
clc
clear


太犀利了,哥们,佩服啊

twm0226 发表于 2012-8-15 22:26

绝对好贴啊!
学习学习!
页: [1]
查看完整版本: 怎么删除excel旧的已有sheet