将GRID中数据导出到 EXCEL文件中
//将GRID中数据导出到 EXCEL文件中
function ExportDBGridEHToExcel(ADBGridEH: TDBGridEh; AOutputFileName: string ; ASheetName :string ): Boolean;
var
  liFieldIndex: Integer;
  liRecordIndex: Integer;
  loXmlWriter: TXLSReadWriteII2;
begin
  Result := False;
  if (not Assigned(ADBGridEH.DataSource)) or (not Assigned(ADBGridEH.DataSource.DataSet)) then
    Exit;
  try
    liRecordIndex := 0;
    loXmlWriter := TXLSReadWriteII2.Create(nil);
 
    //设置Sheet的name信息
    if Trim(ASheetName) <> '' then
      loXmlWriter.Sheet[0].Name := ASheetName;
 
    ADBGridEH.DataSource.DataSet.DisableControls;
    ADBGridEH.DataSource.DataSet.First;
    //输出标题部分
    for liFieldIndex := 0 to ADBGridEH.Columns.Count - 1 do
    begin
      if ADBGridEH.Columns.Items[liFieldIndex].Visible then
      begin
        loXmlWriter.Sheet[0].AsString[liFieldIndex, liRecordIndex] := ADBGridEH.Columns.Items[liFieldIndex].Title.Caption;
      end;
    end;
    liRecordIndex := 1;
    //输出内容
    while not ADBGridEH.DataSource.DataSet.Eof do
    begin
      for liFieldIndex := 0 to ADBGridEH.Columns.Count - 1 do
      begin
        if ADBGridEH.Columns.Items[liFieldIndex].Visible then
          loXmlWriter.Sheet[0].AsString[liFieldIndex, liRecordIndex] :=
            ADBGridEH.DataSource.DataSet.FieldByName(ADBGridEH.Columns.Items[liFieldIndex].FieldName).AsString;
      end;
      ADBGridEH.DataSource.DataSet.Next;
      liRecordIndex := liRecordIndex + 1;
      //Application.ProcessMessages;
    end;
    loXmlWriter.Filename := AOutputFileName;
    loXmlWriter.Write;
  finally
    ADBGridEH.DataSource.DataSet.EnableControls;
    FreeAndNil(loXmlWriter);
  end;
end; 
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

将GRID中数据导出到 EXCEL文件中

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭