piątek, 20 marca 2026

Opening a file from Libre Office in the StringGrid component - C++ Builder

 File Libre Office -  STAFF.ods 





Application.exe
Project.cpp

#include <vcl.h>
#include <utilcls.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
String my_Path="C:\\Users\\admin\\Documents\\STAFF.ods";
Variant mSe, mDe, mDo, mSh, mCe;
Variant mArgs = VarArrayCreate(OPENARRAY(int, (0, -1)), varVariant);
String f_URL = "file:///" + StringReplace(my_Path, "\\", "/", TReplaceFlags() << rfReplaceAll);
   try {

mSe = Variant::CreateObject("com.sun.star.ServiceManager");
mDe = mSe.OleFunction("createInstance", "com.sun.star.frame.Desktop");


mDo = mDe.OleFunction("loadComponentFromURL", f_URL, "_blank", 0, mArgs);

if (mDo.IsEmpty()) {
ShowMessage("I can't open the file");
return;
}


Variant vSheets = mDo.OleFunction("getSheets");
mSh = vSheets.OleFunction("getByIndex", 0);


int maxCols = 3;
int maxRows = 9;
StringGrid1->ColCount = maxCols;
StringGrid1->RowCount = maxRows;


for (int r = 0; r < maxRows; r++) {
for (int c = 0; c < maxCols; c++) {

mCe=mSh.OleFunction("getCellByPosition", c, r);


StringGrid1->Cells[c][r] = mCe.OlePropertyGet("String");
}
}

ShowMessage("SUCCESS!");

}
catch (Exception &e) {
ShowMessage("Error: " + e.Message);
}
}




Brak komentarzy:

Prześlij komentarz