怎样能够知道Excel的一个sheet中到底有多少行和列是有用的(100分)

  • 主题发起人 主题发起人 Jao
  • 开始时间 开始时间
J

Jao

Unregistered / Unconfirmed
GUEST, unregistred user!
我在程序中用Rows.Count想取出到底有多少行数据要处理,可是得到的是这个工
作薄中总共的行数65535,请问用什么方法可以知道有多少行和列中有数据,而其它
的行或列是没有用的?
谢谢!
 
试试Excel.WorkSheet.UsedRange
 
xWolf兄,

100大洋双手奉上-----请大家以后多指教.
谢谢!
 
function funDR():boolean;
var
iRow,iCol,iSheet:integer;
SoursceExcel:OleVariant;
DRCfgInfo:TDRCfgInfo;
begin
with frmShJDR do
begin
if DRCfgInfo.fieldname='' then DRCfgInfo.fieldname:='项目';
ShJDR_SourceData_TEMPtb:=TTable.Create(frmShJDR);
if opendialog1.Execute then
begin
DRCfgInfo.filename:=opendialog1.FileName;
SoursceExcel:=CreateOleObject('Excel.application');
SoursceExcel.visible:=false;
SoursceExcel.WorkBooks.Open(DRCfgInfo.filename);
DRCfgInfo.WordsheetsCount:=SoursceExcel.Worksheets.count;

for iSheet:=1 to DRCfgInfo.WordsheetsCount do
begin
DRCfgInfo.RowsCount:=SoursceExcel.Worksheets[iSheet].UsedRange.Rows.Count;
DRCfgInfo.ColsCount:=SoursceExcel.Worksheets[iSheet].UsedRange.Columns.Count;
PrgBar^.Max:=DRCfgInfo.RowsCount;
PrgBar^.Min:=0;
PrgBar^.Step:=1;
PrgBar^.Position:=0;


ShJDR_SourceData_TEMPtb.DatabaseName:=ConfigInfo.dbName;
with ShJDR_SourceData_TEMPtb do
begin
if Active then Close;
TableName:='ShJDR_SourceData_TEMPtb';
//取消动态创建数据库
if Exists then DeleteTable;

with FieldDefs do
begin
Clear;
with AddFieldDef do begin
Name := 'id';
DataType := ftString;
Size := 5;
Required := false;
end;//with AddFieldDef do begin

for iCol:=1 to DRCfgInfo.ColsCount do
begin
with AddFieldDef do begin
Name :=DRCfgInfo.fieldname+IntToStr(iCol);
DataType := ftString;
Size := 15;
Required := false;
end;//with AddFieldDef do begin
// Add('logoic',ftString,25,false);
end;//for iCol:=1 to DRCfgInfo.ColsCount do
end;//with FieldDefs do
{为计算完成率临时表}
CreateTable;
AutoRefresh:=false;
IndexFieldNames:='id';
ReadOnly:=false;
Active:=true;
end;

for iRow:=1 to DRCfgInfo.RowsCount do
begin

ShJDR_SourceData_TEMPtb.Insert;
ShJDR_SourceData_TEMPtb.FieldByName('id').AsString:=IntToStr(iRow);

for iCol:=1 to DRCfgInfo.ColsCount do
begin
ShJDR_SourceData_TEMPtb.FieldByName(DRCfgInfo.fieldname+IntToStr(iCol)).AsString:=
SoursceExcel.Worksheets[iSheet].UsedRange.Cells[iRow,iCol].text;
end;//for iCol:=1 to DRCfgInfo.ColsCount do
ShJDR_SourceData_TEMPtb.Post;
PrgBar^.StepIt;

end;//for iRow:=1 to DRCfgInfo.RowsCount do
end;//for iSheet:=1 to DRCfgInfo.WordsheetsCount do

//关闭并退出execel对象.
SoursceExcel.ActiveWorkBook.Close;
SoursceExcel.Quit;
SoursceExcel.visible:=true;
SoursceExcel:=Unassigned;
end;// if opendialog1.Execute then
ShJDR_SourceData_TEMPtb_DataSource.DataSet:=ShJDR_SourceData_TEMPtb;
result:=false;
end;

end;
 
后退
顶部