COM to Excel Operations

Source: Internet
Author: User

Using this method needs to add six files, can be found online, namely: EXCEL.TLH, Excel.tli, MSO.TLH, Mso.tli, VBE6EXT.TLH, Vbe6ext.tli

CString strfilter=_t ("Excel file (*.xls, *.xlsx) |*.xls;*.xlsx| |"); //| All Files (*. *) |*.*| | ";
CFileDialog Dlg (true,null,null,ofn_hidereadonly | Ofn_overwriteprompt,
strfilter);
CString strFilePath;
if (dlg. DoModal () ==idok)
{
Strfilepath=dlg. GetPathName ();
if (Strfilepath.isempty ())
{
MessageBox (_t ("Select the appropriate file is empty!") "), _t (" hint "));
Return
}
}
Else
{
Return
}
M_lstlibmaterial.deleteallitems ();
The following is an Excel table operation
Strfilepath.format (L "%s", strFilePath);
_bstr_t filepath= (_bstr_t) strFilePath;
CoInitializeEx (0, coinit_apartmentthreaded);
Excel::_applicationptr xApp;
Excel::rangeptr rang;
Xapp.createinstance (__uuidof (excel::application));
Excel::_workbookptr Xbook = Xapp->workbooks->open (FilePath);//workspace
int Numsheet=xapp->sheets->getcount ();
CString Strcol;

_variant_t vartmp;

Clastmaterialinfobase *data=null;
Char *buf=null;

BUF = new char[128];
int inum=0;
int rowNum;
int nrow;
CString str;
CString StrText;
_bstr_t text;
M_lstlibmaterial.setredraw (FALSE);
for (int ii=1;ii<=numsheet;ii++)//Only one form is currently considered
{
Excel::_worksheetptr Xsheet=xapp->sheets->getitem (ii);
rownum=xsheet->usedrange->rows->count;//number of rows to be used
int colnum=xsheet->usedrange->columns->count;//Number of columns
for (int jj=2;jj<=rownum;jj++)
{
Str. Format (_t ("%d Lines"), JJ);
Debugoutstr (str);
Strtext.empty ();
Strcol.empty ();
Strcol.format (_t ("g%d"), JJ);
vartmp= (LPCTSTR) strcol;
strtext=xsheet->range[vartmp][vtmissing]->text;
Debugoutstr (_t ("Execution 2"));
if (Strtext.isempty ())
{
Continue
}
M_lstlibmaterial.insertitem (0,_t (""));
M_lstlibmaterial.setitemtext (0,0,strtext);

Strtext.empty ();
Strcol.empty ();
Strcol.format (_t ("i%d"), JJ);
vartmp= (LPCTSTR) strcol;
strtext=xsheet->range[vartmp][vtmissing]->text;
M_lstlibmaterial.setitemtext (0,1,strtext);

Strtext.empty ();
Strcol.empty ();
Strcol.format (_t ("j%d"), JJ);
vartmp= (LPCTSTR) strcol;
strtext=xsheet->range[vartmp][vtmissing]->text;
M_lstlibmaterial.setitemtext (0,2,strtext);

Strtext.empty ();
Strcol.empty ();
Strcol.format (_t ("c%d"), JJ);
vartmp= (LPCTSTR) strcol;
strtext=xsheet->range[vartmp][vtmissing]->text;
M_lstlibmaterial.setitemtext (0,3,strtext);

Strtext.empty ();
Strcol.empty ();
Strcol.format (_t ("a%d"), JJ);
vartmp= (LPCTSTR) strcol;
strtext=xsheet->range[vartmp][vtmissing]->text;
M_lstlibmaterial.setitemtext (0,4,strtext);

Strtext.empty ();
Strcol.empty ();
Strcol.format (_t ("k%d"), JJ);
vartmp= (LPCTSTR) strcol;
strtext=xsheet->range[vartmp][vtmissing]->text;
M_lstlibmaterial.setitemtext (0,5,strtext);

Strtext.empty ();
Strcol.empty ();
Strcol.format (_t ("n%d"), JJ);
vartmp= (LPCTSTR) strcol;
strtext=xsheet->range[vartmp][vtmissing]->text;
M_lstlibmaterial.setitemtext (0,6,strtext);

Strtext.empty ();
Strcol.empty ();
Strcol.format (_t ("o%d"), JJ);
vartmp= (LPCTSTR) strcol;
strtext=xsheet->range[vartmp][vtmissing]->text;
M_lstlibmaterial.setitemtext (0,7,strtext);

Strtext.empty ();
Strcol.empty ();
Strcol.format (_t ("p%d"), JJ);
vartmp= (LPCTSTR) strcol;
strtext=xsheet->range[vartmp][vtmissing]->text;
M_lstlibmaterial.setitemtext (0,8,strtext);

Strtext.empty ();
Strcol.empty ();
Strcol.format (_t ("q%d"), JJ);
vartmp= (LPCTSTR) strcol;
strtext=xsheet->range[vartmp][vtmissing]->text;
M_lstlibmaterial.setitemtext (0,9,strtext);

Strtext.empty ();
Strcol.empty ();
Strcol.format (_t ("r%d"), JJ);
vartmp= (LPCTSTR) strcol;
strtext=xsheet->range[vartmp][vtmissing]->text;
M_lstlibmaterial.setitemtext (0,10,strtext);

Strtext.empty ();
Strcol.empty ();
Strcol.format (_t ("s%d"), JJ);
vartmp= (LPCTSTR) strcol;
strtext=xsheet->range[vartmp][vtmissing]->text;
M_lstlibmaterial.setitemtext (0,11,strtext);
}

}
M_lstlibmaterial.setredraw (TRUE);
Xapp->workbooks->close ();
Xapp->quit ();

COM to Excel Operations

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.