| |

VerySource

 Forgot password?
 Register
Search
View: 788|Reply: 3

About C ++ Builderf manipulation of Excel tables, ask prawn

[Copy link]

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-28 16:40:01
| Show all posts |Read mode
I use OLE to manipulate the EXCEL table. There is no problem in opening the table and storing the data. The problem is how to determine if the EXCEL table has been opened in the future if a table is already opened by the application, to avoid repeatedly opening the Excel program and a certain worksheet. Thank you for your help
Reply

Use magic Report

0

Threads

27

Posts

24.00

Credits

Newbie

Rank: 1

Credits
24.00

 China

Post time: 2020-3-3 10:15:01
| Show all posts
TCOM_Application iExcelApp;
    HRESULT hr;
    IUnknown * IUnExcelAppPtr;
    hr = GetActiveObject (CLSID_ExcelApplication, 0, (IUnknown **)&IUnExcelAppPtr);
    if (hr == MK_E_UNAVAILABLE)
    {
      ShowMessage ("No Excel started, restart Excel for control.");
      try {iExcelApp = CoExcelApplication :: Create ();}
      // CoExcelApplication is at Excel_2k.h
      catch (...) {ShowMessage ("Error starting Excel, probably no Excel installed"); return;}
    }
    else
    {
      ShowMessage ("Excel has already been started, just [connect] to this operation.");
      IUnExcelAppPtr-> QueryInterface (IID__Application, (void **)&iExcelApp);
      IUnExcelAppPtr-> Release ();
    }
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-3-3 11:30:01
| Show all posts
Enumerate instances of the Excel application and find the currently open file for each instance
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-3-17 11:45:01
| Show all posts
Thank you two prawns, the problem is solved, haha. I post my code for future communication. Show ugliness:
#include <vcl.h>
#pragma hdrstop
#include "Comobj.hpp"
#include "Unit1.h"
// ------------------------------------------------ ---------------------------
#pragma package (smart_init)
#pragma resource "* .dfm"
#define PG OlePropertyGet
#define PS OlePropertySet
#define FN OleFunction
#define PR OleProcedure
TForm1 * Form1;
// ------------------------------------------------ ---------------------------
__fastcall TForm1 :: TForm1 (TComponent * Owner)
    : TForm (Owner)
{
}
// ------------------------------------------------ ---------------------------

void __fastcall TForm1 :: Button1Click (TObject * Sender)
{
    Variant ExcelApp, WorkBook1, Sheet1, Range;
    bool bExcelRun = true;
    HWND h = NULL;
    h = FindWindow (0, "Microsoft Excel-Data Template 1");
    if (h == NULL)
    {
        try
        {
            GetActiveOleObject ("Excel.Application");
        }
        catch (...)
        {
            bExcelRun = false;
        }
        if (bExcelRun == false)
        {
            ExcelApp = Variant :: CreateObject ("Excel.Application");
        }
        else
        {
            ExcelApp = Variant :: GetActiveObject ("Excel.Application");
        }
        ExcelApp.PS ("Visible", (Variant) true);
        ExcelApp.PG ("WorkBooks"). FN ("Add", "F:\\mydata\\data template.xlt");
    }
    else
    {
        ExcelApp = Variant :: GetActiveObject ("Excel.Application");
        WorkBook1 = ExcelApp.PG ("ActiveWorkBook");
    }
    WorkBook1 = ExcelApp.PG ("ActiveWorkBook");
    Sheet1 = WorkBook1.PG ("ActiveSheet");
    Sheet1.PG ("Cells", 10,10) .PS ("Value", 100.05);

}
Ha ha.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list