1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <utilcls.h> // <---- ne pas oublier
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
// la declaration des Variants doit normalement se faire dans le .h
Variant vMSExcel, vFileName, vXLWorkbooks, vXLWorkbook, vWorksheet;
Variant vCell, vValue;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
try
{
vMSExcel = Variant::GetActiveObject("Excel.Application");
}
catch(...)
{
vMSExcel = Variant::CreateObject("Excel.Application");
}
vMSExcel.OlePropertySet("Visible", true);
vFileName = "C:\\Documents and Settings\\blondelle\\Mes documents\\Nouveau dossier\\path1\\path1.csv";
vXLWorkbooks = vMSExcel.OlePropertyGet("Workbooks");
vXLWorkbook = vXLWorkbooks.OleFunction("Open", vFileName);
// ici le fichier path1.csv est charge
vWorksheet = vXLWorkbook.OlePropertyGet("Worksheets", 1);
// on selectionne la Feuil1
vWorksheet.OleProcedure("Select");
// pour ecrire dans une cellule
// ("Cells", ligne, colonne)
vCell = vWorksheet.OlePropertyGet("Cells", 3, 2);
// on recupere le contenu de la cellule
vValue = vCell.OlePropertyGet("Value");
Edit1->Text = vValue;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
vMSExcel.OleFunction("Quit");
vMSExcel = Unassigned;
}
//--------------------------------------------------------------------------- |
Partager