//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#include "Math.h"
#include "Stdio.h"
#include "Unit8.h"
#include "Unit6.h"
#include "string.h"
#include "iostream.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm8 *Form8;

AnsiString Main_Name;
AnsiString Other_Name[100];
AnsiString OpenDB_Name;
int DB_Mod = 1;
int DB_Num = 0;
AnsiString Auto_DBF_Loaction = "";
int ajs = 0;
int found = 0;
AnsiString str20[60][500];
AnsiString wx[60][200];
AnsiString elsyhd[60][200];
AnsiString yxhd[60][200];

String WellName[2000][2];
String str16[50][500];
String NumToLetter(int n);
int CountMainName(String s);
String Excel_Name = NULL;
int pos_start = 3;
void ResrtAll();        //重置变量
int Main_Count0 = 0;
int space3 = 0;
String daa073_auto_open_path = NULL;
int ss = 0;

AnsiString change_well_name6(AnsiString s);
AnsiString change_name6(AnsiString s);

int flag073 = -1;
AnsiString filename1 = NULL;
AnsiString filename2 = NULL;
AnsiString change_name_single(AnsiString s);

int out_put_mod = 1;
int STEP = 0;


//---------------------------------------------------------
__fastcall TForm8::TForm8(TComponent* Owner)
        : TForm(Owner)
{
Form8->Caption = "断层识别(自动选井)";
}
//---------------------------------------------------------------------------

void __fastcall TForm8::BitBtn1Click(TObject *Sender)
{
   int i0=0;AnsiString Str;
   OpenDialog1->Filter="加载Excel井名文件(*.xlsx)|*.xlsx|加载Excel井名文件(*.xls)|*.xls|加载TXT井名文件(*.txt)|*.txt|所有文件(*.*)|*.*";
   if(OpenDialog1->Execute())
   {
        if(OpenDialog1->Filter!="加载Excel井名文件(*.xlsx)|*.xlsx|加载Excel井名文件(*.xls)|*.xls|所有文件(*.*)|*.*"){
        for(int I=0;I<OpenDialog1->Files->Count;I++)
        {
                int i2=0;AnsiString cc1;
                FILE *fp1;
                fp1=fopen(OpenDialog1->Files->Strings[I].c_str(),"r");
                if(fp1)
                {
                        cc1=OpenDialog1->Files->Strings[I];
                        LAB1 = cc1;
                        Variant ExcelApp;   //对象
                        Variant WorkBook1;  //工作薄
                        Variant WorkSheet1; //工作表
                        try{
                          ExcelApp=Variant::CreateObject("Excel.Application");
                        }
                        catch(...){
                          MessageBox(Handle,"无法启动Excel！","信息提示",MB_ICONSTOP|MB_OK);
                        return;
                        }
                        ExcelApp.OlePropertySet("Visible",false);//设置Excel为可见
                        //打开指定的Excel文件,文件中最好只设定一个Sheet
                        ExcelApp.OlePropertyGet("WorkBooks").OleProcedure("Open",cc1.c_str());

                        WorkBook1=ExcelApp.OlePropertyGet("ActiveWorkBook");
                        WorkSheet1=WorkBook1.OlePropertyGet("ActiveSheet");//获得当前默认的Sheet
                        Variant aCol1=WorkSheet1.OlePropertyGet("Columns",1);
                        Variant aCol2=WorkSheet1.OlePropertyGet("Columns",10);
                        Variant aRow1=WorkSheet1.OlePropertyGet("Rows",1);
                        Variant aRow2=WorkSheet1.OlePropertyGet("Rows",100);
                        int nRow=WorkSheet1.OlePropertyGet("Range",aRow1,aRow2).OlePropertyGet("Rows").OlePropertyGet("Count");//行
                        int nCol=WorkSheet1.OlePropertyGet("Range",aCol1,aCol2).OlePropertyGet("Columns").OlePropertyGet("Count");//列
                        for(int i=1;i<=nRow;i++)
                        {
                                for(int j=1;j<=nCol;j++)
                                {
                                        AnsiString Str;
                                        Str=WorkSheet1.OlePropertyGet("Cells",i,j).OlePropertyGet("Value");
                                        if(Str!=NULL && Str!="")
                                        {
                                                sn[i0]=LowerCase(Str);
                                                i0=i0+1;
                                                break;

                                        }
                                }
                                if(i0 > 0)
                                break;
                        }
                        num=i0;
                        WorkBook1.OleProcedure("Close");//关闭表格
                        ExcelApp.OleFunction("Quit");//退出Excel
                }
                fclose(fp1);
        }
        }
  }
  /*
  else if
  (
        OpenDialog1->Filter!="加载txt井名文件(*.txt)|*.txt")
        {
        String fnm;
        LAB1 = OpenDialog1->Files->Strings[0];
        TStringList* S = new TStringList();
        fnm=OpenDialog1->FileName;
        S->LoadFromFile(fnm);
        for(int n=0;n<S->Count;n++)
        {
           sn[n]=S->Strings[n];
        }
        num=S->Count;
        delete S;
  }
  */



  turn=0;

   if(num > 0 && LAB1.Length() > 3)
   {
        STEP ++;
        Form8->BitBtn2->Enabled = true;
        Form8->Label1->Caption = LAB1;

   }
      if(STEP >= 3)
   Form8->BitBtn4->Enabled = true;










}
//---------------------------------------------------------------------------

void __fastcall TForm8::BitBtn2Click(TObject *Sender)
{
   FILE *fc,*fo;
   int count = 0;
   AnsiString File_name;
   File_name = ChangeFileExt(la_Name[js],"");
   OpenDialog1->Filter="daa073（*.DBF)|*.DBF|所有文件(*.*)|*.*";
   OpenDialog1->Execute();
   daa073_auto_open_path = OpenDialog1->Files->Strings[0].c_str();
   LAB2 = daa073_auto_open_path.c_str();
   if(LAB2.Length() > 5)
   {
        STEP ++;
        Form8->BitBtn3->Enabled = true;
        Form8->Label2->Caption = LAB2;
   }
   if(STEP >= 3)
   Form8->BitBtn4->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm8::BitBtn3Click(TObject *Sender)
{
   FILE *fc,*fo;
   int count = 0;
   AnsiString File_name;
   File_name = ChangeFileExt(la_Name[js],"");
   OpenDialog1->Filter="daa073（*.DBF)|*.DBF|所有文件(*.*)|*.*";
   OpenDialog1->Execute();
   daa073_auto_open_path = OpenDialog1->Files->Strings[0].c_str();
   LAB3 = daa073_auto_open_path.c_str();
   if(LAB3.Length() > 5)
   {
        STEP ++;
        Form8->Label3->Caption = LAB3;
   }
   if(STEP >= 3)
   Form8->BitBtn4->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm8::BitBtn4Click(TObject *Sender)
{
Form8->Close();
Form6->Func_C(Sender);
}
//---------------------------------------------------------------------------











void __fastcall TForm8::RadioButton1Click(TObject *Sender)
{
   SearchRange = 0;
}
//---------------------------------------------------------------------------

void __fastcall TForm8::RadioButton2Click(TObject *Sender)
{
   SearchRange = 1;
}
//---------------------------------------------------------------------------

