请教高手关于把excel的数据读到stringgrid。急 !!!
我点BtnInputClick,打开'Notepal.xls'这个报表,我想要的是,我打这个报表后我任意选中里的几条数据然后显示到TStringGrid,然后再把这些显示到TStringGrid中的数据保存到一数据表。应该怎么做啊。。。在线等。。急啊
下面是我打开'Notepal.xls'这个报表的,但是没显示到TStringGrid,错在那了
procedure TShipFrm.BtnInputClick(Sender: TObject);
var
ExcelApp: Variant;
i, l: integer;
begin
ExcelApp := CreateOleObject('Excel.Application');
ExcelApp.Visible := True;
// ExcelApp.WorkBooks.Add;
ExcelApp.WorkBooks.Open(ExtractFilePath(Application.ExeName) +
'Notepal.xls');
l := 1;
for i := 1 to sgrdList.RowCount - 1 do
begin
ExcelApp.Cells[i + 3, 1].Value := inttostr(i);
sgrdList.Cells[2, i] := ExcelApp.Cells[i + 3, 2].Value;
sgrdList.Cells[5, i] := ExcelApp.Cells[i + 3, 3].Value;
sgrdList.Cells[6, i] := ExcelApp.Cells[i + 3, 4].Value;
sgrdList.Cells[7, i] := ExcelApp.Cells[i + 3, 5].Value;
sgrdList.Cells[8, i] := ExcelApp.Cells[i + 3, 5].Value;
sgrdList.Cells[9, i] := ExcelApp.Cells[i + 3, 6].Value;
sgrdList.Cells[10, i] := ExcelApp.Cells[i + 3, 7].Value;
sgrdList.Cells[11, i] := ExcelApp.Cells[i + 3, 8].Value;
sgrdList.Cells[12, i] := ExcelApp.Cells[i + 3, 9].Value;
sgrdList.Cells[13, i] := ExcelApp.Cells[i + 3, 10].Value;
sgrdList.Cells[14, i] := ExcelApp.Cells[i + 3, 11].Value;
sgrdList.Cells[15, i] := ExcelApp.Cells[i + 3, 12].Value;
sgrdList.Cells[16, i] := ExcelApp.Cells[i + 3, 13].Value;
l := i;
end;
[解决办法]
需要先:
sgrdList.RowCount:=ExcelApp.最大行号
[解决办法]
vba是这样写:
bookf.Sheets(1).[c65536].End(xlUp).row
delphi下可能要:
ExcelApp.Sheets(1).[c65536].End(xlUp).row
[解决办法]
粘到stringgrid上应该 自己循环实现
[解决办法]
空值?那就给一个空字符串:''嘛
[解决办法]
主要看看你'Notepal.xls'这个文件里面的格式是什么样的。你的代码看上去没啥问题,
但是如果你的内容不在sheet1的话,你这个就是读不出来的,下面我教你一招:
1:把ExcelApp.Visible := True;放到for循环后面
2:在 for i := 1 to sgrdList.RowCount - 1 do前面加上一句话ExcelApp.WorkSheets[n].Activate;其中n的值是:你内容所在的sheet位置(从1开始)
明白了吧?
如果不行,你把代码和你那个Notepal.xls文件打包给我我帮你调。