运用该方法需要添加六个文件,网上可以找到,即:excel.tlh、excel.tli、mso.tlh、mso.tli、vbe6ext.tlh、vbe6ext.tli
CString strFilter=_T("Excel文件(*.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("选择相应文件为空!"),_T("提示"));
return;
}
}
else
{
return;
}
m_lstLibMaterial.DeleteAllItems();
//下面对Excel表进行操作
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);//工作区
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++)//目前只考虑一个表单
{
Excel::_WorksheetPtr xSheet=xApp->Sheets->GetItem(ii);
rowNum=xSheet->UsedRange->Rows->Count;//被使用行数
int colNum=xSheet->UsedRange->Columns->Count;//列数
for (int jj=2;jj<=rowNum;jj++)
{
str.Format(_T("%d行"),jj);
//DebugOutStr(str);
strText.Empty();
strCol.Empty();
strCol.Format(_T("G%d"),jj);
varTmp=(LPCTSTR)strCol;
strText=xSheet->Range[varTmp][vtMissing]->Text;
//DebugOutStr(_T("执行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();