| |

VerySource

 Forgot password?
 Register
Search
View: 1122|Reply: 4

How to operate on Excel files?

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-2-22 15:00:02
| Show all posts |Read mode
An EXCEL file is as follows:
     a b
1 10 20
2 2 1
3 15 2
4 100 50
How to import these things into the table in BCB?
Also follow this format,
Best give an example
Thank you
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-5-9 13:15:01
| Show all posts
If it is in text format, you can read lines by lines yourself.
Reply

Use magic Report

0

Threads

11

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-5-11 07:00:01
| Show all posts
I just wrote a small example for you, take a look,
I will give you a cpp, you can send me an email and I will give you the example I just wrote
// ------------------------------------------------ ---------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
// ------------------------------------------------ ---------------------------
#pragma package (smart_init)
#pragma resource "* .dfm"
TForm1 * Form1;
// ------------------------------------------------ ---------------------------
__fastcall TForm1 :: TForm1 (TComponent * Owner)
    : TForm (Owner)
{
}
// ------------------------------------------------ ---------------------------
bool TForm1 :: FromExcelSaveDataSet (AnsiString mFile, TListView * lv)
{
    bool Result;
    Result = false;
    //
    int i, j;
    AnsiString * mstr;
    Variant mExcel_App;
    Variant WorkBooks;
    Variant WorkBook, Sheet1;
    Variant Range1;
    //
    try
    {
       mExcel_App = CreateOleObject ("Excel.Application");
    }
    catch (...)
    {
       ShowMessage ("Cannot create excel program, please check whether it is installed");
       return false;
    }
    try
    {
        WorkBooks = mExcel_App.OlePropertyGet ("WorkBooks");
        WorkBooks.OleProcedure ("Open", mFile.c_str ()); // Open the file
        WorkBook = mExcel_App.OlePropertyGet ("ActiveWorkBook");
        WorkBook.OlePropertyGet ("Sheets", (Variant) 1) .OleProcedure ("Select"); // sheet 1
        //
        Variant Worksheets = WorkBook.OlePropertyGet ("WorkSheets");
        //
        Sheet1 = WorkBook.OlePropertyGet ("ActiveSheet"); // Get sheet
        //
        int col, row;
        col = Sheet1.OlePropertyGet ("UsedRange"). OlePropertyGet ("Columns"). OlePropertyGet ("Count"); // The number of columns
        row = Sheet1.OlePropertyGet ("UsedRange"). OlePropertyGet ("Rows"). OlePropertyGet ("Count"); // Number of rows
        col + = 1;
        //
        for (int temp = 0; temp <col; temp ++)
        {
            TListColumn * lc = lv-> Columns-> Add (); //-> Caption = "Number";
        }
        mstr = new AnsiString [col]; // column array
        // Start traversing excel
        for (i = 0; i <row + 1; i ++)
        {
           for (j = 1; j <= col; j ++)
           {
              mstr [j-1] = Sheet1.OlePropertyGet ("Cells", i + 1, j);
           }
           //
           TListItem * li = lv-> Items-> Add ();
           li-> Caption = mstr [0];
           for (int colTemp = 0; colTemp <col-1; colTemp ++)
           {
                li-> SubItems-> Add (mstr [colTemp +1]);
           }
        }
        //shut down
        WorkBook.OleProcedure ("Close");
        Result = true;
    }
    __finally
    {
        mExcel_App.OleProcedure ("Quit");
        mExcel_App = Unassigned;
        delete [] mstr;
    }
    //
    return Result;
}
void __fastcall TForm1 :: btn1Click (TObject * Sender)
{
    FromExcelSaveDataSet (edt1-> Text.Trim (), lv1);
}
// ------------------------------------------------ ---------------------------
Many things are not optimized, you write it yourself.
Reply

Use magic Report

0

Threads

11

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-5-11 09:45:01
| Show all posts
I imported excel to the listview on the interface
Reply

Use magic Report

0

Threads

27

Posts

24.00

Credits

Newbie

Rank: 1

Credits
24.00

 China

Post time: 2020-5-25 19:00:01
| Show all posts
#ifndef Unit1H
    #define Unit1H
    #include <Classes.hpp>
    #include <ComCtrls.hpp>
    #include <Controls.hpp>
    #include <StdCtrls.hpp>
    // ------------------------------------------------ ---------------------------
    class TForm1: public TForm
    {
      __published:
        TButton * Button1;
        TListView * ListView1;
        void __fastcall Button1Click (TObject * Sender);
      public:
        __fastcall TForm1 (TComponent * Owner);
    };
    // ------------------------------------------------ ---------------------------
    extern PACKAGE TForm1 * Form1;
    // ------------------------------------------------ ---------------------------
    #endif
////////////////////////////////////////////////// //////////////////////////////////
    #include <vcl.h>
    #pragma hdrstop
    #include "Unit1.h"
    #include <Excel_2k.h>
    #pragma package (smart_init)
    #pragma resource "* .dfm"
    TForm1 * Form1;
    // ------------------------------------------------ ---------------------------
    __fastcall TForm1 :: TForm1 (TComponent * Owner): TForm (Owner)
    {
      ListView1-> ViewStyle = vsReport;
    }
    // ------------------------------------------------ ---------------------------
    void __fastcall TForm1 :: Button1Click (TObject * Sender)
    {
      ListView1-> Clear ();
      ListView1-> Columns-> Clear ();

      WideString theXls;
      theXls = ExtractFilePath (Application-> ExeName) + "yourExcel.xls";
      if (! FileExists (theXls)) {ShowMessage ("There is no file in yourExcel.xls in the current directory"); return;}

      TCOM_Application iExcelApp;
      try {iExcelApp = CoExcelApplication :: Create ();}
      catch (...) {ShowMessage ("Error starting Excel, probably not installed Excel"); return;}
      // iExcelApp-> set_Visible (0, true);

      Workbooks * ibooksPtr;
      iExcelApp-> get_Workbooks (&ibooksPtr);
      ibooksPtr-> Open (theXls.c_bstr (),
      TNoParam (), TNoParam (), TNoParam (), TNoParam (),
      TNoParam (), TNoParam (), TNoParam (), TNoParam (),
      TNoParam (), TNoParam (), TNoParam (), TNoParam (),
      0);

      _WorksheetPtr isheet;
      isheet = iExcelApp-> get_ActiveWorkbook ()-> get_ActiveSheet ();
      long RowCount = isheet-> get_UsedRange (1)-> Rows-> get_Count ();
      long ColCount = isheet-> get_UsedRange (1)-> Columns-> get_Count ();
                           
      Range * iRangePtr;
      Variant Cell1, Cell2, v;
      AnsiString s;

      for (int k = 0; k <ColCount; k ++)
      {
        TListColumn * NewColumn;
        NewColumn = ListView1-> Columns-> Add ();
        NewColumn-> Caption = "第" + IntToStr (k) + "Column";
      }

      for (int m = 0; m <RowCount; m ++)
      {
        TListItem * item;
        item = ListView1-> Items-> Add ();
        for (int n = 0; n <ColCount; n ++)
        {
          Cell1 = isheet-> Cells-> get_Item (TVariant (m + 1), TVariant (n + 1));
          Cell2 = isheet-> Cells-> get_Item (TVariant (m + 1), TVariant (n + 1));
          isheet-> get_Range (Cell1, Cell2,&iRangePtr);
          iRangePtr-> get_Item (TVariant (m + 1), TVariant (n + 1));
          iRangePtr-> Activate ();
          v = iRangePtr-> get_Value2 ();
          s = v.operator AnsiString ();
          if (n == 0) item-> Caption = s;
          else item-> SubItems-> Add (s);
        }
      }

      iRangePtr-> Release ();
      isheet-> Release ();
      ibooksPtr-> Release ();
      iExcelApp-> Quit ();
    }
    // ------------------------------------------------ ---------------------------
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