Home > Back-end >  Call demon elder brother and each great god!!!!!!!!!!
Call demon elder brother and each great god!!!!!!!!!!

Time:09-18

C + + builder wants to use the content of the ole operation will excel import database
Has been achieved a cell a cell values and then written to the database
But if big data so inefficient
So I want to take advantage of the range value read operation will area into a two dimensional array
But do not know how to code implementation
Range to a two dimensional array to the variant value
But the rookie of the variant type two-dimensional array a face of meng force
Forget each teacher solve
O demon elder brother grant instruction
At sixes and sevens about according to the guide meaning played left a few lines of code you

The Variant excelApp=CreateOleObject (" Excel. Application ");
String xlsFile=L "E: \ \ ha XLSX";
ExcelApp. OlePropertyGet (" Workbooks "). OleFunction (" open ", WideString (xlsFile));

ExcelApp. OlePropertySet (" Visible ", True);

The Variant workbook=excelApp. OlePropertyGet (" ActiveWorkBook ");
The Variant sheet=workbook. OlePropertyGet (" ActiveSheet ");

String rangeText=L "A1: E17";
The Variant range=sheet. OlePropertyGet (" range ", WideString (rangeText));

//range. PG (" Rows ", 1), PG (" Value ", 1234);

Int col=range. OlePropertyGet (" Columns "). OlePropertyGet (" Count ");//get the number of columns for the selected range
Int row=range. OlePropertyGet (" Rows ".) OlePropertyGet (" Count ");//get the selected range the number of rows


SAFEARRAYBOUND sab [2].//define a 2 d array
Sab [0]. CElements=col;
Sab [0]. LLbound=0;
Sab [1]. CElements=row;
Sab [1]. LLbound=0;

SAFEARRAY * psa=SafeArrayCreate (VT_UI1, sizeof (sab)/sizeof (SAFEARRAYBOUND), sab);

The Variant varChunk;
VarChunk. N=VT_ARRAY | VT_UI1;//an array type
VarChunk. Parray=psa;

VarChunk=range. PG (" Value ");

Psa=varChunk. Parray;
BYTE * buf;
SafeArrayAccessData (psa, (void * *) & amp; Buf);//security pointer

SafeArrayUnaccessData (psa);

Workbook. PR (" Close ");
ExcelApp. FN (" Quit ");
Type varinat two-dimensional array how to define how to assign the range value is assigned to the array over how to value??

CodePudding user response:

Written in python, use this XLRD, fast,

CodePudding user response:

OLE read slowly slowly, when more than 6000 lines of EXCEL, basically just hang up,

Improve the speed of three methods:
1, OLE, open the EXCEL, and then call the COPY COPY data to the memory, then save as TXT file, and then the ADO connection this TXT file, database schema operation directly, the equivalent of a database operation
2, OLE open EXCEL, insert table, OLE save, then ADO connection modified EXCEL file, and database operation directly, this way to EXCEL file format requirement is very high, must standardize
3, open the EXCEL file, file flow directly read EXCEL file streams, identify XLS identifier, is to write a simple EXCEL, only open the function, the fastest speed, and, of course, is the highest difficulty, need you to analyze the EXCEL file format, the MS is the official document,

CodePudding user response:

True cow upstairs, post can also interpret half a year ago, I reply the net friend similar problems, the original poster can search by oneself,

CodePudding user response:

This BBS, a post, can put N for a long time

CodePudding user response:

Had better use ADO way, read table once, then write to excel, I have a resource file is doing this, you can refer to the!
https://download.csdn.net/download/laizx/3915380

CodePudding user response:

Demon elder brother is definitely a great god
  • Related