声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 8587|回复: 14

[编程技巧] 怎么删除excel旧的已有sheet

[复制链接]
发表于 2009-10-23 08:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

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

非常感谢

[ 本帖最后由 ChaChing 于 2009-10-24 10:41 编辑 ]
回复
分享到:

使用道具 举报

发表于 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.
 楼主| 发表于 2009-10-23 13:20 | 显示全部楼层
帮助文件我看过了,主任引用的第一句话说写入到指定工作表中,意味着无法修改工作表名称? 要想让工作表名称符合自己要求必须创建新表而保留着旧表无法从matlab里删去?
发表于 2009-10-23 14:06 | 显示全部楼层
From help
If sheet does not exist, a new sheet is added at the end of the worksheet collection.
 楼主| 发表于 2009-10-23 14:11 | 显示全部楼层
主任们所言都是事实,我的问题还是没有解决
目前我认为
已存在sheet无法改名
只能靠添加sheet来进行命名,但默认生成sheet也无法删除
总而言之,matlab不能解决我提的问题?
手动进excel解决
就此封贴吧
发表于 2009-10-23 14:52 | 显示全部楼层
"当前已有sheet改名, 删除旧的已有的sheet?"
个人好奇又不才, 可问为何有此需求?
 楼主| 发表于 2009-10-23 15:48 | 显示全部楼层
其实我就想1.把数据导入一个sheet,2. 让这个sheet有我起得名字, 3.多余的sheet不要
1. 主要目的
2. 就像书的每个章节都有个题目,不能只叫Chapter 1, Chapter 2.....
3. 同上,不想书Chapter 1,2,3都是空的,从Chapter 4 开始写,前面占用的资源浪费,且不方便阅读
发表于 2009-10-23 18:42 | 显示全部楼层
数据导入一个sheet并依LZ意思进行命名, 进入Excel删除不必要的sheet, 之后新sheet亦可依LZ意思进行命名!
好像仅能这样了!:loveliness:
发表于 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

  1. % DeleteEmptyExcelSheets.m
  2. % DeleteEmptyExcelSheets: deletes all empty sheets in an xls-file
  3. %
  4. %==========================================================================
  5. % Version : 1.0
  6. % Author : hnagel
  7. % Date : 27/04/2007
  8. % Tested : 02/05/2007 (DR)
  9. %==========================================================================
  10. %
  11. % This function looped through all sheets and deletes those sheets
  12. %that are
  13. % empty. Can be used to clean a newly created xls-file after all
  14. %results
  15. % have been saved in it.
  16. %
  17. % References: Torsten Jacobsen, "delete standard excel sheet"
  18. %---------------------------------------------------------------------
  19. %
  20. % Input:
  21. %
  22. % fileName: name of xls file
  23. %
  24. %---------------------------------------------------------------------
  25. %
  26. % Output:
  27. %
  28. % none
  29. %
  30. %---------------------------------------------------------------------
  31. %
  32. % See also XLSWRITE
  33. %---------------------------------------------------------------------
  34. % Changes
  35. %---------------------------------------------------------------------
  36. %
  37. % Name :
  38. % Date :
  39. % Description:
  40. % Indicated :
  41. function DeleteEmptyExcelSheets(fileName)
  42. % Check whether the file exists
  43. if ~exist(fileName,'file')
  44. error([fileName ' does not exist !']);
  45. else
  46. % Check whether it is an Excel file
  47. typ = xlsfinfo(fileName);
  48. if ~strcmp(typ,'Microsoft Excel Spreadsheet')
  49. error([fileName ' not an Excel sheet !']);
  50. end
  51. end
  52. % If fileName does not contain a "" the name of the current path is
  53. % added to fileName. The reason for this is that the full path is required
  54. % for the command "excelObj.workbooks.Open(fileName)" to work properly
  55. if isempty(strfind(fileName,'\'))
  56. fileName = [cd '\' fileName];
  57. end
  58. excelObj = actxserver('Excel.Application');
  59. excelWorkbook = excelObj.workbooks.Open(fileName);
  60. worksheets = excelObj.sheets;
  61. sheetIdx = 1;
  62. sheetIdx2 = 1;
  63. numSheets = worksheets.Count;
  64. % Prevent beeps from sounding if we try to delete a non-empty
  65. % worksheet.
  66. excelObj.EnableSound = false;
  67. % Loop over all sheets
  68. while sheetIdx2 <= numSheets
  69. % Saves the current number of sheets in the workbook
  70. temp = worksheets.count;
  71. % Check whether the current worksheet is the last one. As there
  72. % always
  73. % need to be at least one worksheet in an xls-file the last sheet
  74. % must
  75. % not be deleted.
  76. if or(sheetIdx>1,numSheets-sheetIdx2>0)
  77. % worksheets.Item(sheetIdx).UsedRange.Count is the number of used
  78. % cells.
  79. % This will be 1 for an empty sheet. It may also be one for
  80. %vcertain other
  81. % cases but in those cases, it will beep and not actually delete
  82. % the sheet.
  83. if worksheets.Item(sheetIdx).UsedRange.Count == 1
  84. worksheets.Item(sheetIdx).Delete;
  85. end
  86. end
  87. % Check whether the number of sheets has changed. If this is not
  88. % the case the counter "sheetIdx" is increased by one.
  89. if temp == worksheets.count;
  90. sheetIdx = sheetIdx + 1;
  91. end
  92. sheetIdx2 = sheetIdx2 + 1; % prevent endless loop...
  93. end
  94. excelObj.EnableSound = true;
  95. excelWorkbook.Save;
  96. excelWorkbook.Close(false);
  97. excelObj.Quit;
  98. delete(excelObj);
  99. return;
复制代码

评分

1

查看全部评分

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

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

[ 本帖最后由 seashellingolds 于 2009-10-24 15:32 编辑 ]
发表于 2009-10-24 21:33 | 显示全部楼层


我想楼主也非等闲之辈, 或许楼主有兴趣的话, 不是就可以直接试试看!?
个人以为试试看是最好的方式
发表于 2009-10-25 12:20 | 显示全部楼层

回复 10楼 seashellingolds 的帖子

一个笨的方法是读入你要修改名字sheet的数据并清空,存到你要重新命名的sheet中,最后删除那个空sheet。
发表于 2010-10-11 21:31 | 显示全部楼层
好帖,我顶
发表于 2012-6-24 16:10 | 显示全部楼层
friendchj 发表于 2009-10-24 05:07
example:
clc
clear

太犀利了,哥们,佩服啊
发表于 2012-8-15 22:26 | 显示全部楼层
绝对好贴啊!
学习学习!
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-11-15 23:27 , Processed in 0.079563 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表