C++Builder Tutorial: Learn To Copy Matrix As A Excel Clipboard In Modern C++

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
Tutorial: Learn To Copy Matrix As A Excel Clipboard In Modern C++
February 17, 2021 By Yilmaz Yoru

Для просмотра ссылки Войди или Зарегистрируйся, 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 Для просмотра ссылки Войди или Зарегистрируйся 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 members as in excel clipboard form. So you can paste all members to excel. This method is good to copy data from matrix which is formed with numeric or alpha numeric parameters. You can also add this method to popup menus of components or things which has parameters in a table form, like Grids.

Basically excel form of a table (only texts) is formed with text string and each columns are separated with tab character ( ‘\t‘ ) and each string lines are listed as in that row.
C++:
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;
}
This C++ Builder Console VCL Application example below copies a 2d matrix to a clipboard in excel form.
C++:
#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;
}
 
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;
}