Clipboard also called the paste buffer, is a buffer that operating systems provide to copy things (texts, bitmaps, tables, etc.) for the 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, we would like to post a small snippet to copy a matrix member as in excel clipboard form. So you can paste all members to excel. This method is good to copy data from a matrix that is formed with numeric or alphanumeric parameters. You can also add this method to popup menus of components or things which has parameters in a table form, like Grids.
Excel form of a table (only texts) is formed with a text string and each column is separated with a tab character ( ‘\t‘ ) and each string line is listed as in that row.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
void matrix2D_to_excelclip( int *m, int columns, int rows) { TStringList *strlist= new TStringList(); for (int j=0; j<rows; j++) { String str = ""; for (int i=0; i<columns; i++) str += IntToStr( *((m+j*columns) + i))+'\t'; strlist->Add(str); } Clipboard()->AsText = strlist->Text; strlist->Free(); } |
This C++ Builder Console VCL Application example below copies a 2d matrix to a clipboard in excel form.
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 |
#include <vcl.h> #include <windows.h> #include <tchar.h> #include <stdio.h> #include <clipbrd.hpp> #pragma hdrstop #pragma argsused void matrix2D_to_excelclip( int *m, int columns, int rows) { TStringList *strlist= new TStringList(); for (int j=0; j<rows; j++) { String str = ""; for (int i=0; i<columns; i++) str += IntToStr( *((m+j*columns) + i))+'\t'; strlist->Add(str); } Clipboard()->AsText = strlist->Text; strlist->Free(); } int _tmain( int argc, _TCHAR* argv[] ) { int mat[3][4] = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}}; matrix2D_to_excelclip( (int *)&mat, 4, 3 ); return 0; } |
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition