Excel导入StringGrid,只有前五行有数据。
var
ExcelApp : Variant;
sheet : Variant;
ExcelRowCount : integer; //Excel行数
ExcelColCount : integer; //Excel列数
i : integer; //行变量
j : integer; //列变量
str : String;
WBK : OleVariant;
begin
if Edit1.Text = ' ' then
showmessage( '首先选择要导入的Excel文件 ')
else
begin
//导入数据到DBGrid
ExcelApp:=CreateOleObject( 'Excel.Application '); //建立Excel对象
WBK := ExcelApp.WorkBooks.Open(Edit1.Text); //打开指定文件
sheet := ExcelApp.WorkSheets[1];
ExcelApp.Visible := False; //不显示Excel
ExcelRowCount := ExcelApp.WorkSheets[1].UsedRange.Rows.Count; //行数
ExcelColCount := ExcelApp.WorkSheets[1].UsedRange.Columns.Count; //列数
showmessage(inttostr(ExcelColCount));
for i := 1 to ExcelRowCount do
begin
for j := 1 to ExcelColCount do
begin
str := sheet.cells[j,i];
showmessage( '第 '+inttostr(i)+ '行,第 '+inttostr(j)+ '列的值为 '+str);
stringgrid1.Cells[j-1,i-1] := str;
end;
stringgrid1.RowCount := StringGrid1.RowCount + 1;
end;
WBK.Close(SaveChanges := True);
end;
end;
把Excel的前5行删了,Excel导入Stringgrid后还是只显示前5行,奇怪了,不知道为什么。
[解决办法]
我看了你的代码。发现你行列在StringGrid中是列行。
这个有没有关系?
[解决办法]
sheet.cells[j,i]改为shee.cells[i,j]
因为excell的单元格内容是[行,列]
stringgrid的单元格内容是[列,行]