delphi6 如何访问EXCEL,如何控制EXCEL里的每行每列的数据?

解决方案 »

  1.   

    才10分。
    可以用控件完成,如advstringgrid等表格控件。
    也可以自己写:

    void __fastcall TfrmMain::mnuOpenClick(TObject *Sender)
    {
    //open
        //load cell file
        //Excel
        OpenExcel->DefaultExt = "xls";
        OpenExcel->FileName = "*.xls";
        OpenExcel->Filter = "Excel files (*.xls)|*.xls";
        OpenExcel->FilterIndex = 1; //
        if (OpenExcel->Execute() == true)
        {
            sInfo = "Try to open Excel file...  " + DateToStr(Date()) + TimeToStr(Time());
            Memo1->Lines->Add(sInfo);
            Variant ExcelApp;
            try
            {
                ExcelApp = Variant::CreateObject("Excel.Application");
            }
            catch (...)
            {
                sInfo = "Create Excel object failed!";
                Memo1->Lines->Add(sInfo);
                return ;
            }
            //if (!LoadExcelData(ExcelApp, OpenExcel->Files->Strings[0]))
            if (!LoadExcelData(ExcelApp, OpenExcel->FileName))
            {
                sInfo = "Open Excel file  unknown error!";
                Memo1->Lines->Add(sInfo);
                ExcelApp.OleFunction("Quit");
            }
            ExcelApp.Clear();
        }
    }
    bool __fastcall TfrmMain::LoadExcelData(Variant ExcelAppp, AnsiString sFileName)
    {
        Variant ExcelWorkbooks, ActiveWorkBook, WorkSheet, varCell, ExcWindows;
        AnsiString str;
        //打开指定文件
        try
        {
            ExcelWorkbooks = ExcelAppp.OlePropertyGet("Workbooks");
            ActiveWorkBook = ExcelWorkbooks.OleFunction("Open", sFileName.c_str());
        }
        catch (...)
        {
            sInfo = AnsiString("Open  \"") + sFileName + AnsiString("\"  failed!");
            Memo1->Lines->Add(sInfo);
            return false;
        }
        //取得第一个sheet
        WorkSheet = ActiveWorkBook.OlePropertyGet("WorkSheets", 1);
        //
        int colCount, rowCount;
        colCount = WorkSheet.OlePropertyGet("UsedRange").OlePropertyGet("Columns").OlePropertyGet("Count");
        rowCount = WorkSheet.OlePropertyGet("UsedRange").OlePropertyGet("Rows").OlePropertyGet("Count");
        sInfo = "FileName:" + sFileName;
        Memo1->Lines->Add(sInfo);
        sInfo = "Col:" + IntToStr(colCount);
        Memo1->Lines->Add(sInfo);
        sInfo = "Row:" + IntToStr(rowCount);
        Memo1->Lines->Add(sInfo);
        //whaway:--------------------------------------------------------------
        bool bCheck;
        bCheck = true;
        if (7 != colCount || rowCount < 11)
        {
            sInfo = "Number of Column error,or file is empty!";
            bCheck = false;
        }
        if (!bCheck)
        {
            Memo1->Lines->Add(sInfo);
            return false;
        }
        //whaway:--------------------------------------------------------------
        //whaway:--------------------------------------------------------------
        String iFields[8];
        int irow = 11;
        ProgressBar1->Max = rowCount - 1;
        AdvColumnGrid1->RowCount = m_Count + 2;
        //whaway:--------------------------------------------------------------
        try
        {
            for (irow = 11; irow <= rowCount; irow++)
            {
                varCell = WorkSheet.OlePropertyGet("Cells", irow, 2);
                iFields[2] = varCell.OlePropertyGet("Value");
                // Lon lat
                varCell = WorkSheet.OlePropertyGet("Cells", irow, 3);
                iFields[3] = varCell.OlePropertyGet("Value");
                varCell = WorkSheet.OlePropertyGet("Cells", irow, 4);
                iFields[4] = varCell.OlePropertyGet("Value");
                //Bearing HBWD PN
                varCell = WorkSheet.OlePropertyGet("Cells", irow, 5);
                iFields[5] = varCell.OlePropertyGet("Value");
                varCell = WorkSheet.OlePropertyGet("Cells", irow, 6);
                iFields[6] = varCell.OlePropertyGet("Value");
                varCell = WorkSheet.OlePropertyGet("Cells", irow, 7);
                iFields[7] = varCell.OlePropertyGet("Value");
      

  2.   

    Excel完全可以当成一个数据库来处理