首先,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文件的導入和導出就先介紹到這裏,歡迎大家光臨,如果哪裏有講錯的請及時聯繫我 : 新浪郵箱:[email protected] qq:1574203887