我写的采用csv格式将数据转换为excel的函数,带有分栏

来源:互联网 发布:java 数据级权限控制 编辑:程序博客网 时间:2024/05/01 04:14
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

声明部分
procedure  DataToexcelcsv(SaveFileName:string;DataSet:TDataSet;ShowCompleteBoX:Boolean=True;GroupCount:integer=1);

......
{------------------------------------------------------}
{检测findStr是否in mainStr,如果存在则返回True,否则False}
{------------------------------------------------------}
function TFun.IsStrInOtherStr(mainStr,FindStr: string): Bool;
begin
 if strPos(pAnsiChar(mainStr),pAnsichar(FindStr))=nil
 then
   result:=False
 else
   result:=True;
end;
-------------------------------------------------------------------------------------
//lijinhao 2004-4-4
//采用csv格式..将数据转换为excel.
//速度非常快,而且具有分栏功能
//避免了用comobj带来到弊端
//GroupCount:用于设定分栏数。。默认为1
//ShowCompleteBoX:boolean;来设定完成是否显示完成提示
//-------------------------------------------------------------------------------
procedure  TFun.DataToexcelcsv(SaveFileName:string;DataSet:TDataSet;ShowCompleteBoX:Boolean;GroupCount:integer);
  Function CheckStr(str:string):string;
  begin
    if IsStrInOtherStr(str,',') then str:='"'+str+'"';
    result:=str;
  end;
  //===============//
var
  excelFile:TextFile;
  iRecordCount:integer;//记录数
  iFieldCount:integer;//字段数
  i,j,k:integer;
  TempStr:string;
begin
  try
     if  Not DataSet.Active then DataSet.Open;
     iRecordCount:=DataSet.RecordCount;
     iFieldCount:=DataSet.FieldCount;
     assignFile(excelFile,SaveFileName+'.csv');
     rewrite(excelFile);
     DataSet.First;
     (*--------写字段头------*)
      TempStr:='';
      for K:=0 to iFieldCount-1 do //字段数
      begin
        if TempStr<>'' then
         TempStr:=TempStr+','+CheckStr(DataSet.Fields[k].FieldName)
        else
         TempStr:=CheckStr(DataSet.Fields[k].FieldName)
      end;(* for K:=1 to FieldCount do*)
      for i:= 1 to GroupCount-1 do  TempStr:=TempStr+','+TempStr;
      writeLn(excelFile,TempStr);
      //---------------------------------
     (*写入记录,按分栏数来写*)
     i:=1;
     while i<=round(iRecordCount div GroupCount) do
     begin
       TempStr:='';
       //如:F0 F1 F2 F3 | F0 F1 F2 F3
        for j:=1 to GroupCount do //分栏数
        begin
           if DataSet.Eof then break;
           inc(i);
           for K:=0 to iFieldCount-1 do //字段数
           begin
            //--------------
             if tempstr<>'' then
                TempStr:=TempStr+','+CheckStr(DataSet.Fields[k].AsString)
             else
                TempStr:=CheckStr(DataSet.Fields[k].AsString);
            //------------
           end;(* for K:=1 to FieldCount do*)
           DataSet.Next;
        end;(* for j:=1 to GroupCount do*)
        writeLn(excelFile,TempStr);
       if DataSet.Eof then break;
    end;//while i<=round(iRecordCount div GroupCount) do
    if ShowCompleteBoX then MessageBox(0,'完成DataToexcel的转换!','完成提示:',mb_ok+MB_IconInformation)
 finally
   closeFile(excelFile);
 end;
end;

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击