C ++ Method for reading EXECL table data

Source: Internet
Author: User

Reading the content of an EXCEL table with C ++ is a very troublesome task. Today I read the content of the EXCEL table with CStringArray mentioned in "VC ++", which is really good. This format is usually used to convert an EXCEL file into a CSV file as a format for data interaction between different programs ). Then, use the C ++ program to read CSV files. There are two main classes: class CRow
{
Private:
CStringArray * _ row;
Public:
CRow (CStringArray * row );
Int getColumnCount (void );
CString getColumn (int I );
};
Class CSheet
{
Private:
CTypedPtrArray <CPtrArray, CStringArray *> _ rows;
Public:
CSheet (void );
~ CSheet (void );
Int loadFrom (ifstream & in );
Int getRowCount (void );
CRow getRow (int I );
}. For details, refer to "pulse VC ++" demo: int main ()
{
CSheet sheet; // open the csv file
Ifstream in ("test.csv ");
// Load to CSheet
Sheet. loadFrom (in); // open the target file. txt
Ofstream out ("test.txt"); char * sTmp = new char [100]; for (int I = 0; I <sheet. getRowCount (); I ++)
{
_ Tprintf (_ T ("[% 02d]"), I );
// Obtain the specified row
CRow row = sheet. getRow (I );
For (int j = 0; j <row. getColumnCount (); j ++)
{
// Obtain the specified Column
CString s = row. getColumn (j );
_ Tprintf (_ T ("% s"), s); // convert the CString type to the char * type
Sprintf (sPane, _ T ("% s"), s );
// Save it in the TXT file
// Cout <"\ n" <sTmp <endl;
Out <sPane <"\ t ";
}
_ Tprintf (_ T ("\ r \ n"), I );
Out <"\ n ";
}
Out. close ();
In. close ();
Return 0;
} (From http://www.kaifa6.com/v/MFCShouce/class/CStringArray.htm) CStringArray class member

Structure

CStringArray Construct an empty CString object Array

Bind
GetSize Obtains the number of elements in the array.
SetSize Set the number of elements contained in this array
GetUpperBound Returns the largest valid index.

Operator
FreeExtra Release all unused memory outside the boundary of the current array
RemoveAll Delete all elements from the array

Element access
GetAt Returns the value at the given index.
SetAt Set the value of the element at the given index. do not increase the array size.
ElementAt Returns a temporary reference to an element pointer in an array.
GetData Access Allowed to elements in the array. It can be NULL.

Extended Array
SetAtGrow Set the value at the given index. If necessary, the array can be increased.
Add Add an element at the end of the array. The array can be increased as needed.
Append Add another array to the array; Increase the array if necessary
Copy Copy another array to this array. If necessary, the array can be increased.

Insert/delete
InsertAt Insert one element or all elements in another array at the specified index)
RemoveAt Deletes an element at a specified index.

Operator
Operator [] Sets or obtains the elements at the specified index.
Webmaster statistics 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'height =" 0 "src =" http://b74.cnzz.com/stat.htm? Id = 1126421 & r = & lg = zh-cn & ntime = 0.88826700 "width =" 0 "border =" 0 "1258335242 & repeatip = '1 & rtime = 0 & cnzz_eid = 98208410-1258335242-http % 3A // response "'/>

This article is from the "ClickFuture" blog, please be sure to keep this source http://pingpeace.blog.51cto.com/304509/227743

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.