关闭

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

标签: excelintegerstringfunction
309人阅读 评论(0) 收藏 举报
//将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; 
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:9151次
    • 积分:219
    • 等级:
    • 排名:千里之外
    • 原创:10篇
    • 转载:6篇
    • 译文:0篇
    • 评论:1条
    文章存档
    最新评论