关于对LIR系统中不规则表格的整理算法研究

来源:互联网 发布:知乎 多功能料理机 编辑:程序博客网 时间:2024/05/22 03:17

接到公司任务,需要把一个不规则的检验数据表格,整理成正常的格式。源表格列数不固定,行数不固定,列名不固定,同一个人员信息出现在不同的表格中。要把这样一个表整理出来,我设计了一个算法。首先转换此表格为CSV格式。也就是文本格式,数据是用,分开的。使用读普通文本文件的方法,读入一行写入Tstringlist对象中,这是第一步,

写完后每个表个每一行的表头和数据都存储在tstringlist列表Jysj中,可以用过tstringlist【i】轻松访问,第二步。将每个人的检验数据进行合并,利用身份证,开始嵌套循环,将Jysj中的表头和数据分别拼接为一个字符串,并删除A中用完的信息,便于减少下次查找,存储在Bt-t(表头),jysj_t(数据)

中,第三部,利用先前做好按照间隔符分离数据‘,’,将数据分离,然后查找对应的表头是否有需要的选项,取对应数据,写入新的excel表格中。整理完毕



procedure TForm1.BitBtn2Click(Sender: TObject);var f:textfile;//打开源文件变量 hbsag_e,hba1c_e,h2_e,alb_e,t3_e,t4_e,na_e,k_e,bun_e,bt_t,jysj_t,t,Bt_temp,xm_e,sfz_e,cea_e,afp_e,glu_e,hb_e,wbc_e,plt_e,ndb_e,nt_e,ntt_e,nqx_e,alt_e,ast_e,tbil_e,dbil_e,cr_e,ua_e,chol_e,tg_e,ldl_ch_e,hdl_ch_e,hcy_e:string; i,j,bz,r:integer;//记录逗号位置 Jysj,temp,bt:TStringList;     //bt表头 Jysj检验数据 temp 临时数据 k,lost:boolean;   //lost 判断是不是最后一个beginmemo1.Clear;if not FileExists(edit1.Text) then  begin    showmessage('找不到源文件');    exit;  end;    AssignFile(f,edit1.Text); //将文件变量f与选择的源文件关联    Reset(f);   jysj:=Tstringlist.Create;    temp:=TStringList.Create;    bt:=Tstringlist.Create;   while not eof(f) do      begin        k:=false;        readln(f,t);        bz:=pos(',',t);       if copy(t,0,bz-1)='医院全称' then         begin       // showmessage(t);            bt_temp:=t; //将表头储存              while not eof(f) do                begin                readln(f,t);                    separateterms(t,',',temp);                    if temp.Strings[6]<>'' then                      begin                        Jysj.Add(bt_temp);//将表头数据装入字符库                        Jysj.Add(t);                      end                    else                     if not checkbox1.Checked then                      begin                          if k then   //如果是第一次的话                          break//跳出循环体                          else                          k:=true;                      end                      else                        break;                end;        end;      end;    //以上为整理基本数据  格式为表头/数据;       r:=2;       lost:=false;      while ((jysj.Count-1)>=0) do  //数据记录不为空        begin              temp.Clear;  //清空临时字符库              bt.Clear;              separateterms(jysj.Strings[1],',',temp); //成功获取后将删除字符库中的数据,因此固定从第一条开始采集              if temp.Strings[6]<>'身份证号'then                begin                  xm_e:=temp.Strings[2];                  sfz_e:=temp.Strings[6];                  J:=1;                   while (j<=jysj.Count-1)  do                    begin                      if pos(sfz_e,jysj.Strings[j])>0 then                        begin                        //showmessage(inttostr(j)+#10+#13+inttostr(jysj.Count-1)+#10+#13+jysj.Strings[j]);                        if copy(jysj.Strings[j],length(jysj.Strings[j])-1,length(jysj.Strings[j]))<>',' then                          begin                            jysj_t:=jysj_t+','+jysj.Strings[j];                            bt_t:=bt_t+','+jysj.Strings[j-1];                          end                            else                          begin                            jysj_t:=jysj_t+jysj.Strings[j];                            bt_t:=bt_t+jysj.Strings[j-1];                          end;                            jysj.Delete(j);                            jysj.Delete(j-1);                        if j=1 then                             j:=1                        else                             j:=j+2;                         end                      else                        begin                        j:=j+2;                        end;                    end;                   //以下为整理资料部分                   separateterms(bt_t,',',bt);  //分离表头                   separateterms(jysj_t,',',temp); //分离数据                   afp_e:='';                   cea_e:='';                   glu_e:='';                   hb_e:='';                   wbc_e:='';                   plt_e:='';                   ndb_e:='';                   nt_e:='';                   ntt_e:='';                   nqx_e:='';                   alt_e:='';                   ast_e:='';                   tbil_e:='';                   dbil_e:='';                   cr_e:='';                   ua_e:='';                   chol_e:='';                   tg_e:='';                   ldl_ch_e:='';                   hdl_ch_e:='';                   hcy_e:='';                   na_e:='';                   k_e:='';                   bun_e:='';                   t3_e:='';                   t4_e:='';                   alb_e:='';                   h2_e:='';                   hba1c_e:='';                   hbsag_e:='';                   for i:=0 to bt.Count-1 do                    begin                      if (bt.Strings[i]='癌胚抗原 (CEA)') and (cea_e='') then                          cea_e:=temp.Strings[i];                      if (bt.Strings[i]='甲胎蛋白(AFP)') and (afp_e='') then                          afp_e:=temp.Strings[i];                      if (bt.Strings[i]='总三碘甲状腺原氨酸') and (t3_e='') then                          t3_e:=temp.Strings[i];                      if (bt.Strings[i]='总甲状腺素') and (t4_e='') then                          t4_e:=temp.Strings[i];                      if (bt.Strings[i]='血糖(GLU)') and (glu_e='') then                          glu_e:=temp.Strings[i];                      if (bt.Strings[i]='餐后2h血糖') and (h2_e='') then                          h2_e:=temp.Strings[i];                      if (bt.Strings[i]='血红蛋白(Hb)') and (hb_e='') then                          hb_e:=temp.Strings[i];                      if (bt.Strings[i]='白细胞计数(WBC)') and (wbc_e='') then                          wbc_e:=temp.Strings[i];                      if (bt.Strings[i]='血小板计数(PLT)') and (plt_e='') then                          plt_e:=temp.Strings[i];                      if (bt.Strings[i]='尿蛋白质') and (ndb_e='') then                          ndb_e:=temp.Strings[i];                      if (bt.Strings[i]='尿糖') and (nt_e='') then                          nt_e:=temp.Strings[i];                      if (bt.Strings[i]='尿酮体') and (ntt_e='') then                          ntt_e:=temp.Strings[i];                      if (bt.Strings[i]='尿潜血') and (nqx_e='') then                          nqx_e:=temp.Strings[i];                      if (bt.Strings[i]='谷丙转氨酶(ALT)') and (alt_e='') then                          alt_e:=temp.Strings[i];                      if (bt.Strings[i]='谷草转氨酶(AST)') and (ast_e='') then                          ast_e:=temp.Strings[i];                      if (bt.Strings[i]='总胆红素(TBIL)') and (tbil_e='') then                          tbil_e:=temp.Strings[i];                      if (bt.Strings[i]='白蛋白(ALB)') and (alb_e='') then                          alb_e:=temp.Strings[i];                      if (bt.Strings[i]='直接胆红素(DBIL)') and (dbil_e='') then                          dbil_e:=temp.Strings[i];                      if (bt.Strings[i]='肌酐(Cr)') and (cr_e='') then                          cr_e:=temp.Strings[i];                      if (bt.Strings[i]='尿素(BUN)') and (bun_e='') then                          bun_e:=temp.Strings[i];                      if (bt.Strings[i]='血钾(K)') and (k_e='') then                          k_e:=temp.Strings[i];                      if (bt.Strings[i]='血钠(Na)') and (na_e='') then                          na_e:=temp.Strings[i];                      if (bt.Strings[i]='尿酸(UA)') and (ua_e='') then                          ua_e:=temp.Strings[i];                      if (bt.Strings[i]='胆固醇(CHOL)') and (chol_e='') then                          chol_e:=temp.Strings[i];                      if (bt.Strings[i]='甘油三酯(TG)') and (tg_e='') then                          tg_e:=temp.Strings[i];                      if (bt.Strings[i]='低密度脂蛋白胆固醇(LDL-CH)') and (ldl_ch_e='') then                          ldl_ch_e:=temp.Strings[i];                      if (bt.Strings[i]='高密度脂蛋白胆固醇(HDL-CH)') and (hdl_ch_e='') then                          hdl_ch_e:=temp.Strings[i];                      if (bt.Strings[i]='同型半胱氨酸(HCY)') and (hcy_e='') then                          hcy_e:=temp.Strings[i];                      if (bt.Strings[i]='糖化血红蛋白(HBA1C)') and (hba1c_e='') then                          hba1c_e:=temp.Strings[i];                      if (bt.Strings[i]='乙型肝炎病毒表面抗原(HBsAg)') and (hbsag_e='') then                          hbsag_e:=temp.Strings[i];                    end;                    // showmessage(bt.Strings[i]+'['+temp.Strings[i]+']');                      excelapp1.Cells.item[r,2]:=xm_e; //写入姓名                      excelapp1.Cells.item[r,4]:=sfz_e; //写入身份证                      excelapp1.Cells.item[r,11]:=FormatDateTime('yyyy-mm-dd',Now());;                      excelapp1.Cells.item[r,128]:=cea_e;                      excelapp1.Cells.item[r,127]:=afp_e;                      excelapp1.Cells.item[r,129]:=t3_e;                      excelapp1.Cells.item[r,130]:=t4_e;                      excelapp1.Cells.item[r,131]:=glu_e;                      excelapp1.Cells.item[r,132]:=h2_e;                      excelapp1.Cells.item[r,133]:=hb_e;                      excelapp1.Cells.item[r,134]:=wbc_e;                      excelapp1.Cells.item[r,135]:=plt_e;                      excelapp1.Cells.item[r,137]:=ndb_e;                      excelapp1.Cells.item[r,138]:=nt_e;                      excelapp1.Cells.item[r,139]:=ntt_e;                      excelapp1.Cells.item[r,140]:=nqx_e;                      excelapp1.Cells.item[r,143]:=alt_e;                      excelapp1.Cells.item[r,144]:=ast_e;                      excelapp1.Cells.item[r,145]:=alb_e;                      excelapp1.Cells.item[r,146]:=tbil_e;                      excelapp1.Cells.item[r,147]:=dbil_e;                      excelapp1.Cells.item[r,148]:=cr_e;                      excelapp1.Cells.item[r,149]:=bun_e;                      excelapp1.Cells.item[r,150]:=K_e;                      excelapp1.Cells.item[r,151]:=na_e;                      excelapp1.Cells.item[r,152]:=ua_e;                      excelapp1.Cells.item[r,153]:=chol_e;                      excelapp1.Cells.item[r,154]:=tg_e;                      excelapp1.Cells.item[r,155]:=ldl_ch_e;                      excelapp1.Cells.item[r,156]:=hdl_ch_e;                      excelapp1.Cells.item[r,157]:=hba1c_e;                      excelapp1.Cells.item[r,158]:=hbsag_e;                      excelapp1.Cells.item[r,248]:=hcy_e;                      memo1.Lines.Add('【'+inttostr(r-1)+'】姓名:'+xm_e+',身份证号:'+sfz_e+',------------->导入成功');                      r:=r+1; //写入下一行                   jysj_t:='';  //当这个人的资料整理结束,清空临时数据                   bt_t:='';                end;        end;   temp.Free;   jysj.Free;    closefile(f); //关闭文件    showmessage('整合完毕!');end;