Delphi中对Excel表格文件的导入和导出操作

2022-12-24 14:33:04 作者:admin

本文整理自网络,侵删。

 首先,Delphi要在Uses部分,把要使用的外部程序的类写入。在这里,我们把“ExtCtrls,ComObj”写入Uses部分。将Listview列表中的数据导入到Excel文件中。procedure TfreadExcel.WriteExcel;var  i,idex : Integer;  ExcelApp:Variant;begin  try    dlgSave.FileName := '学生信息.xls';    if dlgSave.Execute then    begin      if FileExists(dlgSave.FileName) then      begin        try          if application.messagebox('该文件已经存在,要覆盖吗?','询问',mb_yesno+mb_iconquestion) = idyes then          begin            DeleteFile(PChar(dlgSave.FileName));          end          else          begin            dlgSave.free;            Exit;          end;        except          dlgSave.free;          Exit;        end;      end;       try        ExcelApp:=CreateOleObject('Excel.Application');        ExcelApp.Caption:='应用程序调用 Microsoft Excel';        ExcelApp.WorkBooks.Add; //新增工作簿      except        Application.MessageBox('无法调用Excel!','提示',MB_OK);        Exit;      end;       try        idex := 1;        for i:=0 to rzlvshow.Items.Count-1 do        begin          ExcelApp.Cells[idex,1].Value:=rzlvshow.Items[i].SubItems.strings[0];          ExcelApp.Cells[idex,2].Value:=rzlvshow.Items[i].SubItems.strings[1];          ExcelApp.Cells[idex,3].Value:=rzlvshow.Items[i].SubItems.strings[2];          ExcelApp.Cells[idex,4].Value:=rzlvshow.Items[i].SubItems.strings[3];          ExcelApp.Cells[idex,5].Value:=rzlvshow.Items[i].SubItems.strings[4];          ExcelApp.Cells[idex,6].Value:=rzlvshow.Items[i].SubItems.strings[5];          ExcelApp.Cells[idex,7].Value:=rzlvshow.Items[i].SubItems.strings[6];          idex := idex+1;        end;         if ExcelApp.Worksheets[1].SaveAs(dlgSave.FileName)=0 then        begin          Application.MessageBox('数据导出成功!','导出提示',MB_OK);        end;      finally        ExcelApp.Quit;      end;    end;  except    Application.MessageBox('保存xls文件失败!','提示',MB_OK);    Exit;  end;end;

将Excel数据导入到Listivew列表中:
procedure TfreadExcel.ReadExcel;var  ExcelApp : Variant;  WorkBook : OLEVARIANT;  ExcelRowCount : Integer;  item :TListItem;  i,num : Integer; begin  try    if dlgOpen.Execute then    begin      try        num := 0;        ExcelApp := CreateOLEObject('Excel.Application');      except        Application.MessageBox('excel没有安装', '提示信息', MB_OK+MB_ICONASTERISK+MB_DEFBUTTON1+MB_APPLMODAL);        Exit;      end;       rzlvshow.Items.Clear;//对ListView先进行清空      WorkBook := ExcelApp.WorkBooks.Open(dlgOpen.FileName);//使用opendialog对话框指定      ExcelApp.Visible := false;      ExcelRowCount := WorkBook.WorkSheets[1].UsedRange.Rows.Count;      //获取Excel的行数      SetLength(theArray, ExcelRowCount);       for i := 1 to ExcelRowCount do      begin        item := rzlvshow.Items.Add;        item.Data := Pointer(i);        theArray[num].agreement_id := Trim(excelapp.Cells[i,1].Value);         //主协议编号        theArray[num].asset_id     := Trim(excelapp.Cells[i,2].Value);         //资产单元编号        theArray[num].allot_no     := Trim(excelapp.Cells[i,3].Value);         //申请书编号        theArray[num].frozen_adjustfare := excelapp.Cells[i,4].Value;          //本金        theArray[num].init_date    := excelapp.Cells[i,5].Value;                //交易日期        theArray[num].remark       := Trim(excelapp.Cells[i,6].Value);         //备注         item.SubItems.Add(IntToStr(num+1));        item.SubItems.Add(theArray[num].agreement_id);        item.SubItems.Add(theArray[num].asset_id);        item.SubItems.Add(theArray[num].allot_no );        item.SubItems.Add(FloatToStr(theArray[num].frozen_adjustfare));        item.SubItems.Add(IntToStr(theArray[num].init_date));        item.SubItems.Add(theArray[num].remark);        num := num + 1;      end;       if ExcelRowCount=num then      begin        Application.MessageBox('数据导入完成!','提示',MB_OK);      end;      WorkBook.close;      ExcelApp.Quit;    end;  except      Application.MessageBox('文件打开失败!','提示',MB_OK);  end;end;

获得Excel的行和列数:
  ExcelRowCount : Integer;  ExcelColCount : Integer;  ExcelRowCount := WorkBook.WorkSheets[1].UsedRange.Rows.Count;      //获取Excel的行数  ExcelColCount := WorkBook.WorkSheets[1].UsedRange.columns.Count;   //获取Excel的列数
对Excel文件的导入和导出就先介绍到这里,欢迎大家光临,如果哪里有讲错的请及时联系我 : 新浪邮箱:chao_song2011@sina.cn     qq:1574203887――――――――――――――――
原文链接:https://blog.csdn.net/lovescjinbao/article/details/20044675

相关阅读 >>

Delphi 解析json生成json

用Delphi写console控制台程序

Delphi tdictionary保存到文件

Delphi 得到文件夹下所有文件名

Delphi限制edit编辑框的输入

Delphi 根据进程名得到进程所在路径

Delphi 释放res资源文件

Delphi xe 下打开内存泄漏调试功能

Delphi 字符串中加入换行符slinebreak

Delphi 解决Delphi indy10下 utf8码转换的问题

更多相关阅读请进入《Delphi》频道 >>



在线咨询 拨打电话