Exporting CListCtrl controls to Excel files

Source: Internet
Author: User

CListCtrl the properties of the column can be obtained by using the bool Clistctrl::getcolumn (int ncol, lvcolumn* pcolumn), ncol the column ordinal to obtain the property value, Pcolumn to Lvcolumn The pointer to the structure body. The UINT mask in the lvcolumn structure, as input, determines which properties to return, and if the mask value contains Lvcf_text, the first address pointer of the string cache needs to be placed into the LPTSTR psztext in the structure; Variable, the cache size is placed int cchtextmax; Variable.

The main problem with this function is that the main problem is expected to return Lvcf_text, set the psztext without setting the Cchtextmax variable, and usually clear the allocated memory by 0 when allocating the lvcolumn structure space, so the column name cannot always be returned. Instead of clearing 0 allocated space, directly set mask as Lvcf_text, you might get the value of the column name. Of course, as long as the correct setting of Psztext and Cchtextmax will not have these problems.

Another problem is the number of columns. There is no direct function to get the number of columns, and you can only increment the Ncol value from 0 to determine whether the return value of GetColumn (Ncol,pcolumn) is False to calculate the number of columns.

A packaged, exported CListCtrl data function is as follows:///////////////////////////////////////////////////////////////////////////////
void Getexceldriver (clistctrl* plist, CString strtitle)
Parameters:
Plist the list control pointer that needs to be exported
Strtitle Exported Datasheet Headers
Description
Export all data from the CListCtrl control to an Excel file. The Excel file name is passed by the user "Save as"
The dialog box is entered as specified. Create a worksheet named Strtitle that contains all the data in the list control, including
column names and data items) are saved as text into an Excel worksheet. Maintain a row and column relationship.
//
edit by [R] @dotlive. cnblogs.com
///////////////////////////////////////////////////////////////////////////////
void Exportlisttoexcel (clistctrl* plist, CString strtitle)
{
CString Warningstr;
if (Plist->getitemcount () >0) {
CDatabase database;
CString Sdriver;
CString Sexcelfile;
CString sSQL;
CString tablename = strtitle;

Retrieves whether the Excel driver Microsoft Excel Driver (*.xls) is installed
Sdriver = Getexceldriver ();
if (Sdriver.isempty ())
{
No Excel drivers found
AfxMessageBox ("Do not install excel!\n, please install Excel software before using export function!");
Return
}

Default filename
if (! Getdefaultxlsfilename (Sexcelfile))
Return

Creating a string for access
Ssql.format ("driver={%s};D sn="; Firstrowhasnames=1; Readonly=false; Create_db=\ "%s\";D bq=%s ", Sdriver,
Sexcelfile, Sexcelfile);

Create a database (both Excel table files)
if (database. OpenEx (Ssql,cdatabase::noodbcdialog))
{
Create a table structure
int i;
Lvcolumn Columndata;
CString ColumnName;
int columnnum = 0;
CString STRH;
CString STRV;
sSQL = "";
STRH = "";
Columndata.mask = Lvcf_text;
Columndata.cchtextmax = 100;
Columndata.psztext = Columnname.getbuffer (100);
For (I=0;plist->getcolumn (i,&columndata); i++)
{
if (i!=0)
{
sSQL = sSQL + ",";
STRH = Strh + ",";
}
sSQL = sSQL + "" + Columndata.psztext + "TEXT";
STRH = Strh + "" + Columndata.psztext + "";
}
Columnname.releasebuffer ();
Columnnum = i;
sSQL = "CREATE TABLE" + tablename + "(" + sSQL + ")";
Database. ExecuteSQL (sSQL);

Inserting data items
int nitemindex;
For (Nitemindex=0;nitemindexgetitemcount (); nitemindex++) {
STRV = "";
For (I=0;igetitemtext (nitemindex,i) + "'";
}

sSQL = "INSERT into" + tablename
+ "(" + STRH + ")"
+ "VALUES (" + STRV + ")";
Database. ExecuteSQL (sSQL);
}

}

Close Database
Database. Close ();

Warningstr.format ("Export file is stored in%s!", sexcelfile);
AfxMessageBox (WARNINGSTR);
}
}

Welcome to discuss the programming and development of VC,. NET,. NET Moblie, graphic images, etc.

This article supporting source code

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.