• C++ builder 2010 操作Excel表格的编程实现


    //---------------------------------------------------------------------------
    
    #include <vcl.h>
    #pragma hdrstop
    
    #include "ProcessBar.h"
    #include <stdio.h>     // For FILE, fopen, fstat, fileno, fread and fclose
    #include <sysstat.h>  // For fstat and the stat struct
    #include <Math.hpp>    // For Min
    #include <memory>       //For STL auto_ptr class
    
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm4 *Form4;
    //---------------------------------------------------------------------------
    __fastcall TForm4::TForm4(TComponent* Owner)
        : TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm4::btn1Click(TObject *Sender)
    {
        FILE *F;
        char MyData[2047];
        long BytesRead;
    
        F = fopen("../oui.txt", "r");   // Path relative to Debug
                                        // Use a file larger than 2048 bytes to make it interesting.
        if (F)
        {
        struct stat statbuf;
        fstat(fileno(F), &statbuf);
        pb1->Max = statbuf.st_size;
        if (pb1->Max > 10)
        {
          pb1->Step = (pb1->Max)/10;
          pb1->Step = Min(pb1->Step, 2047);
        }
        else
          pb1->Step = pb1->Max;
        std::auto_ptr<char> DataBuffer(new char[pb1->Step]);
        for (pb1->Position = 0;
             pb1->Position < pb1->Max;
             pb1->StepIt())  // Move the ProgressBar Position using StepIt.
        {
          fread(DataBuffer.get(), pb1->Step, 1, F);
          // Do this or else the read wraps and starts over.
          pb1->Step =
            Min(pb1->Step, pb1->Max - pb1->Position);
        }
    
        mmo1->Lines->Add(DataBuffer.get());
    
        fclose(F);
        DataBuffer.release();
        }
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm4::btn2Click(TObject *Sender)
    {
    
        Variant vExcelApp, vWorkbook, vRange, Sheet1;
        vExcelApp = Variant::CreateObject("Excel.Application");
        vExcelApp.OlePropertySet("Visible", false);
        vExcelApp.OlePropertyGet("WorkBooks").OleProcedure("Open",
            dlgOpen1->FileName.c_str());
        vWorkbook = vExcelApp.OlePropertyGet("ActiveWorkbook");
    
        int BookCount = vWorkbook.OlePropertyGet("Sheets").OlePropertyGet
        ("Count");
        for (int i = 0; i < BookCount; i++)
        {
            AnsiString SheetName = vWorkbook.OlePropertyGet("Sheets", i + 1)
            .OlePropertyGet("Name");
            // ShowMessage(SheetName);
    
        }
    
        // Sheet1   =   vWorkbook.OlePropertyGet( "ActiveSheet ");
        // vExcelApp.Exec(PropertyGet( "Cells ") < <1 < <3).Exec(PropertySet( "Value ") < <15);     //赋值
        // Sheet1.OlePropertyGet( "Rows ",   1).OlePropertyGet( "Insert ");       //插入
        String kkl, hgf;
        int it = 9, st = 1;
    
        while (it > 8 && BookCount >= st)
        {
            vWorkbook.OlePropertyGet("Sheets", st).OleProcedure("Select");
            // 选择sheet
            for (it = 1; it <= 8; it++)
            {
                kkl = vExcelApp.Exec(PropertyGet("Cells") << 5 << it);
                //if (kkl.Pos(qz1) > 0 || kkl.Pos(qz2) > 0) // 读取
                    break;
            }
            st++;
        }
    
        int kk = 1;
        for (kk = 1; kk <= 8; kk++)
        {
            kkl = vExcelApp.Exec(PropertyGet("Cells") << kk << it);
            //if (kkl.Pos(qz1) > 0 || kkl.Pos(qz2) > 0) // 读取
                break;
        }
    
        hgf = vExcelApp.Exec(PropertyGet("Cells") << kk << it + 2);
        int ki = 1;
        while (hgf.Trim() != "")
        {
            if (ki == StringGrid2->RowCount - 1)
                //StringGrid1->RowCount++;
            // kkl==vExcelApp.Exec(PropertyGet( "Cells ") < <kk < <it);
    
            StringGrid2->Cells[1][ki] = vExcelApp.Exec
                (PropertyGet("Cells") << kk << it); // 2-4列的数据
            StringGrid2->Cells[2][ki] = vExcelApp.Exec
                (PropertyGet("Cells") << kk << it + 1);
            StringGrid2->Cells[3][ki] = vExcelApp.Exec
                (PropertyGet("Cells") << kk << it + 2);
            kk++;
            ki++;
            hgf = vExcelApp.Exec(PropertyGet("Cells") << kk << it + 2);
        }
    
        vExcelApp.OlePropertyGet("ActiveSheet").OlePropertyGet("Cells ", 1, 2)
            .OleProcedure("Select");
        vRange = vExcelApp.OlePropertyGet("Selection");
        vRange.Exec(Function("Sort") << vExcelApp.OlePropertyGet("Selection")
            << 1);
    
        // vWorkbook.OleProcedure( "Save");
        vWorkbook.OleProcedure("Close");
        vExcelApp.OleFunction("Quit");
        vWorkbook = Unassigned;
        vExcelApp = Unassigned;
    }
    // ---------------------------------------------------------------------------
  • 相关阅读:
    下拉刷新ListView的实现原理
    自己总结的一些Java公用函数库
    异常处理的最佳实践[转载]
    Android Parcelable和Serializable的区别
    Android BroadcastReceiver介绍
    添加androidsupportv4 错误 java.lang.ClassNotFoundException: android.support.v4.view.ViewPager in loader dalvik.system.PathClassLoader
    [转载]SQL SERVER 数据类型详解
    pthread
    main参数
    实验二测试
  • 原文地址:https://www.cnblogs.com/kernel0815/p/3318187.html
Copyright © 2020-2023  润新知