Clipboard, also called as the paste buffer, is is a buffer which operating systems provide to copy things (texts, bitmaps, tables etc.) for short term and transfer within and between application programs. The clipboard is usually temporary and unnamed, and its contents reside in the computer’s memory.
In C++ Builder there are good components to use Excel forms, excel supported Grid components etc. In this post example we would like to copy excel table to clipboard and paste it to StringGrid in C++ Builder. This method is good to copy data from excel tables to your component members.
Clipboard from an Excel consists L’\t‘ tab characters , so first we should delimitate each lines. We can use this delimiter function for each rows.
1 2 3 4 5 6 7 8 9 |
void split(wchar_t delimiter, UnicodeString str, TStringList *listofstrings) { listofstrings->Clear(); listofstrings->Delimiter = delimiter; listofstrings->StrictDelimiter = True; // Requires D2006 or newer. listofstrings->DelimitedText = str; } |
We can use Clipboard() function to get all lines as below.
1 2 3 |
UnicodeString us=Clipboard()->AsText); |
This text includes all table with both L’\n’ return characters for each row and L’\t’ tab characters for each columns. We can easily see each members with this small example below. We need to delimit clipboard to rows first and delimit each row to columns. We need these to support Unicode strings.
1 2 3 4 5 |
UnicodeString us; TStringList *str_rows= new TStringList(); TStringList *str_cols= new TStringList(); |
So, we can delimit each lines of clipboard text with return and tab characters by using our split() function as below;
1 2 3 4 |
split( L'\n', Clipboard()->AsText, str_rows); //Delimit lines with return char split( L'\t', str_rows->Strings[j], str_cols); //Delimit line with tab char |
We should do this operation in two for loops for columns and rows. So we can set cell of grid at i,j as below;
1 2 3 |
StringGrid1->Cells[i][j]=str_cols->Strings[i]; |
If we combine all together, full C++ Builder VCL codes of this example with a StringGrid, a Memo and a Button should be as below;
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 |
#include <clipbrd.hpp> #pragma hdrstop #include "Excel_to_StringGrid_VCL_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void split(wchar_t delimiter, UnicodeString str, TStringList *listofstrings) { listofstrings->Clear(); listofstrings->Delimiter = delimiter; listofstrings->StrictDelimiter = True; // Requires D2006 or newer. listofstrings->DelimitedText = str; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { UnicodeString us; TStringList *str_rows= new TStringList(); TStringList *str_cols= new TStringList(); split( L'\n', Clipboard()->AsText, str_rows); //Delimit lines with return char StringGrid1->RowCount=str_rows->Count; for(int j=0; j<str_rows->Count; j++) { if(str_cols->Count>StringGrid1->ColCount) StringGrid1->ColCount=str_cols->Count; split( L'\t', str_rows->Strings[j], str_cols); //Delimit line with tab char for(int i=0; i<str_cols->Count; i++) { Memo1->Lines->Add(IntToStr(i)+","+IntToStr(j)+"="+str_cols->Strings[i]); StringGrid1->Cells[i][j]=str_cols->Strings[i]; } } } |
And if you copy a table and paste it with a Button1, the result will as below
StringGrid supports Unicode Strings that means you can also safely copy all tables including texts in all languages. In theory there should be no problem, if there is check your locale settings or may be you need better locale settings in your codes and may be better font supports that language.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition