compatibility of a grid with excel

This forum can be used to talk about general design strategies, new ideas and questions in general related to wxWidgets. If you feel your questions doesn't fit anywhere, put it here.
Post Reply
tinca
In need of some credit
In need of some credit
Posts: 3
Joined: Sun May 01, 2005 11:55 pm

compatibility of a grid with excel

Post by tinca » Mon May 02, 2005 12:09 am

I designed a wxgrid and when I try to import data from a column of an excell file (I copy a series of values from a column) wit a copy and paste procedure I only get the firt value copied in my grid .
Could anyone help me to get all a table from an excel file to be copied simultaniously in a grid?
Tinca

el-512
In need of some credit
In need of some credit
Posts: 9
Joined: Wed May 04, 2005 7:00 am

Post by el-512 » Wed May 04, 2005 9:59 am

Hi,

I have done something similar with a list control - you seen, the columns are seperated with tabs, the lines with a linefeed.

Here my code:

Code: Select all

        item_list = (wxListCtrl *) FindWindow(list_id);
        if (item_list == NULL) {
            return;
        }
        if (item_list->GetSelectedItemCount() <= 0) {
            return;
        }
        max_col = item_list->GetColumnCount();
        max_line = item_list->GetItemCount();

        i = -1;

// VERY IMPORTANT - this line took me more than 1 hour
        list_element.SetMask(wxLIST_MASK_STATE | wxLIST_MASK_TEXT);

        for (k=0; k<max_col; k++) {
            item_list->GetColumn(k,list_element);

            copy_data = copy_data + list_element.GetText();
            if (k < max_col-1) {
                copy_data = copy_data + wxT("\t");
            }
        }

        do {
            i = item_list->GetNextItem(i,wxLIST_NEXT_ALL,wxLIST_STATE_SELECTED);
            if ( i >= 0) {
                copy_data = copy_data + wxT("\n");
                list_element.SetId(i);
                for (k=0; k<max_col; k++) {
                    list_element.SetColumn(k);
                    item_list->GetItem(list_element);

                    copy_data = copy_data + list_element.GetText();
                    if (k < max_col-1) {
                        copy_data = copy_data + wxT("\t");
                    }
                }
            }
        } while (i >= 0);

        wxOpenClipboard();
        wxEmptyClipboard();
        wxSetClipboardData(wxDF_TEXT,copy_data.c_str(),0,0);
        wxCloseClipboard();

good luck

tinca
In need of some credit
In need of some credit
Posts: 3
Joined: Sun May 01, 2005 11:55 pm

more help, sorry I'm a beginner

Post by tinca » Sun May 08, 2005 2:11 pm

Thanks for the help, but I haven't been able to understand completly your example. I'm a beginner and maybe it could be usefull if you could send me some more explanations or a longer part of your code.
Bye
Tinca

el-512
In need of some credit
In need of some credit
Posts: 9
Joined: Wed May 04, 2005 7:00 am

Post by el-512 » Mon May 09, 2005 8:01 am

Hope this helps, OK this is the COPY and not the PASTE, but this one was faster. Paste will follow later.

This is now a code which I included into the grid-sample - and it works. Don't foreget to include
#include <wx/clipbrd.h>

Code: Select all

void GridFrame::CopyData( wxCommandEvent& WXUNUSED(ev) )
{
int i,k;
wxString copy_data;
bool something_in_this_line;

    copy_data.Clear();

    for (i=0; i<grid->GetRows(); i++) {     // step through all lines
        something_in_this_line = false;     // nothing found yet
        for (k=0; k<grid->GetCols(); k++) { // step through all colums
            if (grid->IsInSelection(i,k)) { // this field is selected!!!
                if (something_in_this_line == false) {  // first field in this line => may need a linefeed
                    if (copy_data.IsEmpty() == false) {     // ... if it is not the very first field
                        copy_data = copy_data + wxT("\n");  // next LINE
                    }
                    something_in_this_line = true;
                } else {                                // if not the first field in this line we need a field seperator (TAB)
                    copy_data = copy_data + wxT("\t");  // next COLUMN
                }
                copy_data = copy_data + grid->GetCellValue(i,k);    // finally we need the field value :-)
            }
        }
    }

/*  I tried this, but it DID NOT WORK!!!!
wxArrayInt row_index, col_index;

    row_index = grid->GetSelectedRows();    // the ARRAYS were EMPTY!!! Don't know why!
    col_index = grid->GetSelectedCols();

    for (i=0; i<row_index.GetCount(); i++) {
        if (i > 0) {
            copy_data = copy_data + wxT("\n");  // next ROW
        }
        for (k=0; k<col_index.GetCount(); k++) {
            if (k > 0) {
                copy_data = copy_data + wxT("\t");  // next COLUMN
            }
            copy_data = copy_data + grid->GetCellValue(row_index[i],col_index[k]);
        }
    }
*/

    wxOpenClipboard();          // now copy all these things into the clipbord
    wxEmptyClipboard(); 
    wxSetClipboardData(wxDF_TEXT,copy_data.c_str(),0,0); 
    wxCloseClipboard(); 
}

el-512
In need of some credit
In need of some credit
Posts: 9
Joined: Wed May 04, 2005 7:00 am

Post by el-512 » Mon May 09, 2005 9:10 am

OK, here the rest.

The include was not inserted correctly last time -anyhow.
remember - I inserted these lines in the grid sample. I hope it is no problem for you to get to the "grid-pointer" and the "copy" or "paste" event.

Maybe there is a better way, but this is my way - and it works.

Hope this works for you too.

Code: Select all


#include <wx/clipbrd.h>

void GridFrame::PasteData( wxCommandEvent& WXUNUSED(ev) )
{
wxString copy_data;
wxString cur_field;
wxString cur_line;
int i,k,k2;

    wxOpenClipboard();          // now copy all these things into the clipbord
    copy_data = (char *)wxGetClipboardData(wxDF_TEXT);
    wxCloseClipboard();

    // I admit, the string contains all fields from min-selection to max selection,
    // even if rows or columns inbetween are NOT selected
    // Don't know if this is an EXCEL or WX problem!
    // But as the same thing happenes in Word, I think, it's Bills fault!

//    i = 0; k = 0;   // Where to insert - 0,0 or left/top current cursor position
    i = grid->GetGridCursorRow();
    k = grid->GetGridCursorCol();
    k2= k;

    do {
        cur_line = copy_data.BeforeFirst('\n');
        copy_data = copy_data.AfterFirst('\n');
        do {
            cur_field = cur_line.BeforeFirst('\t');
            cur_line  = cur_line.AfterFirst ('\t');
            grid->SetCellValue(i,k,cur_field);
            k++;
        } while(cur_line.IsEmpty() == false);
        i++;
        k = k2;
    } while (copy_data.IsEmpty() == false);
}

Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:

Post by Jorg » Mon May 09, 2005 9:19 am

Hi el-512,

Maybe it is a good idea to copy / paste this part on the Code Dump forum? This way others can also benefit from your solution.

Regards,
- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb

tinca
In need of some credit
In need of some credit
Posts: 3
Joined: Sun May 01, 2005 11:55 pm

Post by tinca » Sun May 22, 2005 12:20 am

I still have problems, I'm a TRUE beginner.
I still am not able to complete the job. Can someone add the missing parts or correct my mistakes?
Here are the two files I tried to set up:

----------------------form1.h------------

#ifndef __FORM1_H__
#define __FORM1_H__
#include <wx/grid.h>
#include <wx/clipbrd.h>
#include <wx/string.h>
class PersonalDialog: public wxDialog
{
private:


public:
PersonalDialog(const wxString& title, const wxPoint& pos, const wxSize& size);
~PersonalDialog();
public:
wxGrid* grid;

protected:
void CopyData(wxCommandEvent &event);
void PasteData(wxCommandEvent &event);

DECLARE_EVENT_TABLE()

};

#endif

----------------------form1.cpp------------

#include <string>
#include "precomp.h"
#include "form1.h"
#if !defined(__WXMSW__) && !defined(__WXPM__)
#include "sample.xpm"
#endif
using namespace std;

BEGIN_EVENT_TABLE(PersonalDialog, wxDialog)

END_EVENT_TABLE()

void PersonalDialog::CopyData( wxCommandEvent& WXUNUSED(ev) )
{
int i,k;
wxString copy_data;
bool something_in_this_line;

copy_data.Clear();

for (i=0; i<grid->GetRows(); i++) {
something_in_this_line = false;
for (k=0; k<grid->GetCols(); k++) {
if (grid->IsInSelection(i,k)) {
if (something_in_this_line == false) {
if (copy_data.IsEmpty() == false) {
copy_data = copy_data + wxT("\n");
}
something_in_this_line = true;
} else {
copy_data = copy_data + wxT("\t");
}
copy_data = copy_data + grid->GetCellValue(i,k);
}
}
}


wxOpenClipboard();
wxEmptyClipboard();
wxSetClipboardData(wxDF_TEXT,copy_data.c_str(),0,0);
wxCloseClipboard();
}

void PersonalDialog::PasteData( wxCommandEvent& WXUNUSED(ev) )
{
wxString copy_data;
wxString cur_field;
wxString cur_line;
int i,k,k2;

wxOpenClipboard();
copy_data = (char *)wxGetClipboardData(wxDF_TEXT);
wxCloseClipboard();
i = grid->GetGridCursorRow();
k = grid->GetGridCursorCol();
k2= k;

do {
cur_line = copy_data.BeforeFirst('\n');
copy_data = copy_data.AfterFirst('\n');
do {
cur_field = cur_line.BeforeFirst('\t');
cur_line = cur_line.AfterFirst ('\t');
grid->SetCellValue(i,k,cur_field);
k++;
} while(cur_line.IsEmpty() == false);
i++;
k = k2;
} while (copy_data.IsEmpty() == false);
}


PersonalDialog::PersonalDialog(const wxString& title, const wxPoint& pos, const wxSize& size) :
wxDialog((wxDialog*)NULL, -1, title, pos, size)
{
grid = new wxGrid( this, -1, wxPoint( 50, 50 ), wxSize( 578, 300 ) );
grid-> CreateGrid( 100, 10 );



SetIcon(wxICON(sample));
}


PersonalDialog::~PersonalDialog()

Post Reply