首页 > 代码库 > NativeExcel 读取文件

NativeExcel 读取文件

class function T_EShopDataBill.ImportData(const AFileName: String;  AList: T_EShopDataModelList; var AErr: string): Boolean;var    Book: IXLSWorkbook;    ws: IXLSWorksheet;    i : Integer;    nOrderIDRow,nTrackNoRow : Integer;    obj : T_EShopDataModel;    tmpIden,tmpTrackNo : String;    sSql : String;    tmpQry : TADOQuery;begin   Book := TXLSWorkbook.Create ;   try     i := Book.Open(AFileName);     ws := Book.WorkSheets[1];     if ws.UsedRange.Rows.Count = 0 then       Exit;     nOrderIDRow := 0;     nTrackNoRow := 0;     for I := 1 to ws.UsedRange.Rows[1].Count -1 do     begin        if nOrderIDRow = 0 then            if CompareText( VarToStr( ws.UsedRange.Cells[1,i].Value),单据备注) = 0 then              nOrderIDRow := i;        if nTrackNoRow = 0 then           if CompareText( VarToStr( ws.UsedRange.Cells[1,i].Value),运单编号) = 0 then              nTrackNoRow := i;        if (nOrderIDRow > 0 ) AND (nTrackNoRow > 0) then          Break;     end;      // ShowInformationMsg(inttostr(nOrderIDRow)+ +IntTostr(nTrackNoRow));     if (nOrderIDRow = 0) or (nTrackNoRow = 0) then       Exit;     for i := 2 to  ws.UsedRange.Rows.Count  do     begin        with ws.UsedRange do        begin          if VarIsNull(Cells[i,nTrackNoRow].Value) then            Break;          if VarIsNull(Cells[i,nOrderIDRow].Value) then            Break;        end;        tmpTrackNo := ws.UsedRange.Cells[i,nTrackNoRow].Value;        tmpIden := ws.UsedRange.Cells[i,nOrderIDRow].Value;        if ( Trim(tmpTrackNo) = ‘‘) or (tmpIden = ‘‘) then        begin          Break;        end;        obj := T_EShopDataModel.Create;        obj.fOrderID := tmpIden;        obj.fTrackNo := tmpTrackNo;        AList.List.Add(obj);     end;   finally     Book.Close;   end;end