Jorg just suggested to place this peace of code here - and I obey
I inserted the attached code into the grid-sample and it's supposed to copy / paste data from or to MS-Excel.
The basic thing is, that in Excel the clipboard data seperates the fields within one line with tabs, the lines (no wonder) with a linefeed.
I did all things manually - OK, there may be a better solution (more automatic or without my "GetSelectedRows()" problem), but this solution works. Use it where appropriate.
el-512
Code: Select all
#include <wx/clipbrd.h>
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();
}
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);
}