从Excel导入信息在StringGrid显示

 

1、控件:2个Button,1个label,1个edit,1个StringGrid,1个OpenDialog,1个PopuMenu

2、双击‘......’按钮,选择文件路径

 1 //打开指定的文件
 2 OpenDialog1.InitialDir := ExtractFilePath(Application.ExeName) + '模板\';
 3 OpenDialog1.filter := '文本文件(*.xls)|*.xls';
 4 OpenDialog1.DefaultExt := 'XLS';
 5 OpenDialog1.Title := '打开Excel文件';
 6 OpenDialog1.FileName := '';
 7 if OpenDialog1.Execute then
 8 begin
 9   edtFileUrl.Text := OpenDialog1.FileName;
10 end
11 else
12 begin
13   exit;
14 end;

3、双击‘读取’按钮

 1 procedure Tfrm4.Button2Click(Sender: TObject);
 2 var
 3   ExcelApp: Variant;  //Excel变量
 4   ExcelFiles: string; //打开的Excel文件名
 5   i, j: Integer;
 6   SheetNo: Integer;   //所在sheet页编号
 7 begin
 8   inherited;
 9 
10   RzStringGrid.DefaultDrawing := True;
11   if edtFileUrl.Text = '' then
12   begin
13     Application.MessageBox('请先选择导入文件!', '提示', MB_OK + MB_ICONWARNING);
14     Exit;
15   end;
16   ExcelFiles := edtFileUrl.Text;//选择到的文件路径
17   SheetNo := 1;//sheet编号
18   ExcelApp := CreateOleObject('Excel.Application');
19   ExcelApp.Visible := false;
20   ExcelApp.WorkBooks.Open(ExcelFiles);
21   ExcelApp.WorkSheets[SheetNo].Activate;//Excel第一个Sheet是活动页
22   try
23     try
24       MaxRow := ExcelApp.WorkSheets[SheetNo].UsedRange.Rows.Count;    //所有行数
25       MaxCol := ExcelApp.WorkSheets[SheetNo].UsedRange.Columns.Count;  //所有列数
26       with RzStringGrid do
27       begin
28         ColWidths[2] := 100;//设置第二列的宽度,StringGrid列是从0开始的
29       //StringGrid的行数
30       RzStringGrid.RowCount := MaxRow - 1;//减去最后一行提示信息行
31       //StringGrid的列数
32       RzStringGrid.ColCount := MaxCol;
33       //标题行
34       RzStringGrid.FixedRows := 1;//设置标题行只有一行
35       RzStringGrid.FixedCols := 0;
36       for i := 1 to MaxRow - 1 do//读入数据,控制行
37       begin
38         for j := 1 to MaxCol do//读入数据,控制列
39         begin
40           //Excel表格的行和列是从1开始的,StringGrid的行和列是从0开始
41           RzStringGrid.Cells[j-1, i-1] := VarToStr(ExcelApp.WorkSheets[SheetNo].Cells[i, j].Value);
42         end;
43       end;
44     except
45       on E: Exception do
46       begin
47         ShowMessage('异常类名称:' + E.ClassName + #13#10 + '异常信息:' + E.Message);
48         Application.MessageBox('数据读入错误或者数据文件格式错误!', PChar(Application.Title), MB_OK + MB_ICONWARNING);
49       end;
50     end;
51   finally
52     ExcelApp.ActiveWorkBook.Close;
53     ExcelApp.Quit;
54     ExcelApp := Unassigned;
55     PageControl1.ActivePage := TabSheet2;
56   end;
57 end;

4、PopuMenu添加PopuMenu右键弹出事件

1   //如果表格不为空或者表格的行数大于1(标题行当然不允许删)
2   if (RzStringGrid <> nil) and (RzStringGrid.RowCount > 1) then 
3   begin
4     mniNDelRow.Enabled := True;//则允许删除选中行
5   end
6   else
7   begin
8     mniNDelRow.Enabled := False;//否则不允许
9   end; 

5、给PopuMenu添加一个右键的值,双击会弹出如下

然后双击删除当前行:

1   //如果表格不为空或者表格的行数大于1(标题行当然不允许删)
2   if (RzStringGrid <> nil) or (RzStringGrid.RowCount > 1) then
3   begin
4     for i := RzStringGrid.row to RzStringGrid.RowCount - 1 do
5       RzStringGrid.Rows[i] := RzStringGrid.Rows[i+1];//删除一行,把后一行往前挪一行
6     RzStringGrid.RowCount := RzStringGrid.RowCount - 1;//将StringGrid的总行数减1
7   end;

6、与5同理,双击清空当前列表

1    //定义一个integer类型的变量i
2    //如果表格不为空或者表格的行数大于1(标题行当然不允许删)
3    for i := 1 to RzStringGrid.RowCount -1  do  //如果不清表头则从1开始
4    begin
5      RzStringGrid.Rows[i].Clear;//清空行的同时调用删除行的方法
6      mniNDelRowClick(Self);
7    end;

7、StringGrid的PopuMneu属性记得选择你添加的那个

8、以下是StringGrid的属性,如果哪里不对,自己对照改下

 完了,就这些就可以读取Excel的内容到Stringgrid显示了

 

posted @ 2018-04-23 17:08  一字节  阅读(348)  评论(0编辑  收藏  举报