USEGEAR

导航

unigui导出TMS.Flexcel【5】

 参考代码

procedure TUniFrmeWebEmbedBase.ExportData;
 //导出到excel
var
  FlexCelImport1: TExcelFile;
  i, rowindex, colindex: Integer;
  ms : TMemoryStream;
begin
  { 检查数据集 }
  if Assigned(cdsMain) and cdsMain.Active then
  try
    { 创建Excel文件 }
    FlexCelImport1 := TXlsFile.Create(true);//NewFile(1);
    FlexCelImport1.NewFile(1);

//  FlexCelImport1.SupportsXlsx := True;
//  FlexCelImport1.NewFile(WorkSheetCount, TExcelFileFormat(2) );  //2 = v2010, 1 = v2007, 0 = v2003
//  FlexCelImport1.ActiveSheet := 1;

    cdsMain.First;
    { 第一行为标题 }
    rowindex := 1;
    colindex := 1;
    for i := 0 to cdsMain.FieldCount -1 do
      { 只导出数据集中可视的字段 }
      if cdsMain.Fields[i].Visible then
      begin
        FlexCelImport1.SetCellValue(rowindex, colindex,cdsMain.Fields[i].DisplayLabel);
        inc(colindex);
      end;
    { 依次导出数据 }
    while not cdsMain.Eof do
    begin
      { 增加一行 }
      inc(rowindex);
      colindex := 1;
      for i := 0 to cdsMain.FieldCount -1 do
        { 只导出数据集中可视的字段 }
        if cdsMain.Fields[i].Visible then
        begin
          { 时间和日期字段需要单独出来一下,否则出来的都是浮点数 }
          if cdsMain.Fields[i].DataType in [ftDate, ftDateTime, ftTime, ftTimeStamp] then
            FlexCelImport1.SetCellValue(rowindex, colindex, cdsMain.Fields[i].AsString)
          else
            FlexCelImport1.SetCellValue(rowindex, colindex, cdsMain.Fields[i].Value);
          inc(colindex);
        end;
      { 下一条 }
      cdsMain.Next;
    end;
    FlexCelImport1.PrintLandscape := true;
   // FlexCelImport1.
    { 创建内存流 }
    ms := TMemoryStream.Create;
    try
      { 将Excel保存到内存流 }
      FlexCelImport1.save(ms);
      ms.Position := 0;
      { 将数据流发送到客户端,同时指定下载文件名,非常简单 }
      UniSession.SendStream(ms, 'usegear.xls' );
    finally
      ms.Free;
    { 清理内存 }
      FlexCelImport1.Free;
    end;
  except
    on E : Exception do
      FlexCelImport1.Free;
  end;
end;


参考:
http://forums.unigui.com/index.php?/topic/4197-unigrid-dataset-to-xlsx-export-using-tms-flexcel-an-example/?hl=excel

https://www.cnblogs.com/ChinaEHR/p/4012113.html
一并感谢。
View Code

 

调用

procedure TUniFrmeWebEmbedBase_authoManage.act_ExportExecute(Sender: TObject);
begin
  cdsMain.Data := Unit_dm.DataModule1.getTabeldata('select * from SM_role') ;
  inherited;
end;

效果

 

 

 

这里

TUniFrmeWebEmbedBase_authoManage = class(TUniFrmeWebEmbedBase)

TUniFrmeWebEmbedBase = class(TUniFrame)

posted on 2018-08-13 16:13  USEGEAR  阅读(1758)  评论(0编辑  收藏  举报