Announcement

Collapse
No announcement yet.

Aus Borland C++ 6 Excel Worksheet bearbeiten

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Aus Borland C++ 6 Excel Worksheet bearbeiten

    Ich habe in der Borland Hilfe folgenden Beispielcode für das arbeiten mit Excel gefunden:
    <PRE>
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
    Variant V = Variant::CreateObject("Excel.Application");
    V.OlePropertySet("Visible", true);

    ShowMessage("Excel has been launched and now is visible");
    V.OleFunction("Quit");
    V = Unassigned;

    }
    </PRE>

    Der Code funktioniert auch, allerdings habe ich keine weiteren Informationen gefunden, wie man eine Worksheet öffnet oder Werte in Zellen schreibt. Für VB gibt eine Menge Beispielcode wie das funktioniert. Wer kann mir helfen Beispiele für C++ zu finden.

  • #2
    Lösung:

    Daten von einer BCB-Anwendung in ein Excelsheet schreiben:
    · include <ComObj.hpp> // include-Reihenfolge beachten!!!
    include <utilcls.h>
    .
    .
    .
    void __fastcall TForm1::SchreibenClick(TObject *Sender)
    {
    Variant Excel;
    try
    {
    Excel = GetActiveOleObject("Excel.Application");
    }
    catch(...)
    {
    Excel = CreateOleObject("Excel.Application");
    }
    //Excel.OlePropertySet("Visible", true); //Excel beim Arbeiten anzeigen, false = ausblenden
    try
    {
    Variant WorkBooks = Excel.OlePropertyGet("WorkBooks");
    WorkBooks.OleFunction("Add");
    Variant ActiveWorkBook = Excel.OlePropertyGet("ActiveWorkbook");
    Variant WorkSheets = Excel.OlePropertyGet("Worksheets");
    Variant WorkSheet = WorkSheets.OlePropertyGet("Item", 1);
    WorkSheet.OleFunction("Activate");
    for (int iRow=0; iRow < 11; iRow++)
    {
    for (int iCol = 0; iCol < 11; iCol++)
    {
    Variant Range = WorkSheet.OlePropertyGet("Cells", iRow+1, iCol+1); // Row=.Zeile / Col=Spalte
    Range.OlePropertySet("Value", "TestText"+IntToStr(iCol)+IntToStr(iRow));
    Application->ProcessMessages();
    }
    }
    ActiveWorkBook.OleFunction("SaveAs", ".\\QueryResult.xls"); //Datei speichern
    }
    catch(...){}
    ShowMessage("Excel wird jetzt wieder geschlossen");
    Excel.OleFunction("Quit");
    Excel = Unassigned;
    }

    Daten aus einem Excelsheet in ein BCB-Programm einlesen:
    · include <ComObj.hpp> // include-Reihenfolge beachten!!!
    include <utilcls.h>
    .
    .
    .
    void __fastcall TForm1::LesenClick(TObject *Sender)
    {
    if (OpenDialog1->Execute()) //*.xls-Dateinamen + Pfad angeben
    {
    try
    {
    Variant Excel;
    Excel = Variant::CreateObject("Excel.Application");
    Excel.OlePropertyGet("ActiveWorkBook");
    // Excel-Datei öffnen
    Excel.OlePropertyGet("WorkBooks").OleFunction("Ope n", OpenDialog1->FileName);
    //Excel.OlePropertySet("Visible", true); //Excel beim Arbeiten anzeigen, false = ausblenden
    Variant Sheet;
    Sheet=Excel.OlePropertyGet("ActiveSheet");
    AnsiString Zelleninhalt = Sheet.OlePropertyGet("Cells", 1, 1).OlePropertyGet("Value");// Zeile 1, Spalte 1 auslesen
    ShowMessage(Zelleninhalt);
    ShowMessage("Excel wird jetzt wieder geschlossen");
    Excel.OleFunction("Quit");
    Excel = Unassigned;
    }
    catch(...){}
    }

    Comment

    Working...
    X