matlab如何清除excel中的数据库,[转载][转载]通过Matlab删除Excel中的空白表格

转自:中国震动联盟"friendchj"

原出处:Matlab Central

example:

clc

clear

a=1:5;

xlswrite('my.xls',a,'myown');

DeleteEmptyExcelSheets('my.xls')

以下是DeleteEmptyExcelSheets.m文件

%——————————————————————————————————————————%

% 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([fileName ' does not exist !']);

else

% Check whether it is an Excel file

typ = xlsfinfo(fileName);

if ~strcmp(typ,'Microsoft Excel Spreadsheet')

error([fileName ' not an Excel sheet !']);

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 = [cd '' 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;

%————————————————————————————————————————————%

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值