导EXCEL单表单方法

//记着在函数外面释放
char *Cov(CString str,int &nBytes)
{
int nLength = str.GetLength();
nBytes = WideCharToMultiByte(CP_ACP,0,str,nLength,NULL,0,NULL,NULL);
char* pName = new char[nBytes + 1];
memset(pName,0,nLength + 1);
WideCharToMultiByte(CP_OEMCP, 0, str, nLength, pName, nBytes, NULL, NULL);
pName[nBytes] = 0;
return pName;
}
//导出EXCEL( 换行, 换单元格)
void CCShowMaterialInfo::OnBnClickedBtnOk()
{
// TODO: 在此添加控件通知处理程序代码
//导出Excel
BOOL bNameValid;
CString strType;
CString strAllType;
CString strWrite,strTmp;
char *bufWrite=NULL;
int nBytes=0;
int nR=0;
int nC=0;
bNameValid = strType.LoadString(IDS_STRTYPEXLS);
VASSERT(bNameValid);
bNameValid = strAllType.LoadString(IDS_STRALLTYPEXLS);
VASSERT(bNameValid);

CFileDialog dlg(false,
strType,
NULL,
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
strAllType);

if(dlg.DoModal() == IDOK)
{
CString m_FilePath = dlg.GetPathName();
CString m_path;
m_path = m_FilePath;
UpdateData(TRUE);
try
{
CFile f;
f.Open(m_path,CFile::modeWrite|CFile::modeCreate);
strWrite=_T("当前总码:");
if (b_flg)
{
((CComboBox *)GetDlgItem(IDC_COMBO_TotalCode))->GetWindowText(strTmp);
}
else
{
((CEdit *)GetDlgItem(IDC_EDIT_RTotalCode))->GetWindowText(strTmp);
}
strWrite+=strTmp;
strWrite+=_T(" ");

strTmp=_T("后制程总码:");
strWrite+=strTmp;
((CEdit *)GetDlgItem(IDC_EDIT_ATotalCode))->GetWindowText(strTmp);
strWrite+=strTmp;
strWrite+=_T(" ");

((CStatic *)GetDlgItem(IDC_STATIC_RproductName))->GetWindowText(strTmp);
strWrite+=strTmp;
strWrite+=_T(" ");

((CStatic *)GetDlgItem(IDC_STATIC_RproductCode))->GetWindowText(strTmp);
strWrite+=strTmp;
strWrite+=_T(" ");//中途空两行
strTmp=_T("物料详细信息: ");
strWrite+=strTmp;

nBytes=0;
bufWrite=Cov(strWrite,nBytes);
f.Write(bufWrite,nBytes);
if (bufWrite!=NULL)
{
delete[]bufWrite;
bufWrite=NULL;
}
//先写物料信息
int nColCount=m_listMaterialInfo.GetHeaderCtrl()->GetItemCount();
LVCOLUMN lvcolu;
TCHAR str[256];
lvcolu.mask = LVCF_TEXT;
lvcolu.pszText = str;
lvcolu.cchTextMax = 256;
strWrite.Empty();
for (nC=0;nC<nColCount;nC++)
{
strTmp.Empty();
m_listMaterialInfo.GetColumn(nC,&lvcolu);
strTmp=lvcolu.pszText;
strTmp+=_T(" ");
strWrite+=strTmp;
}
strWrite+=_T(" ");
nBytes=0;
bufWrite=Cov(strWrite,nBytes);
f.Write(bufWrite,nBytes);
if (bufWrite!=NULL)
{
delete[]bufWrite;
bufWrite=NULL;
}
//开始写物料数据
int nRowNum=m_listMaterialInfo.GetItemCount();
for (nR=0;nR<nRowNum;nR++)
{
strWrite.Empty();
for (nC=0;nC<nColCount;nC++)
{
strTmp.Empty();
strTmp=m_listMaterialInfo.GetItemText(nR,nC);
strTmp+=_T(" ");
strWrite+=strTmp;
}
strWrite+=_T(" ");
nBytes=0;
bufWrite=Cov(strWrite,nBytes);
f.Write(bufWrite,nBytes);
if (bufWrite!=NULL)
{
delete[]bufWrite;
bufWrite=NULL;
}
}
//开始电检信息写入
strWrite.Empty();
strWrite+=_T(" ");
strTmp=_T("电池性能检测: ");
strWrite+=strTmp;
nColCount=m_listElec.GetHeaderCtrl()->GetItemCount();
for (nC=0;nC<nColCount;nC++)
{
strTmp.Empty();
m_listElec.GetColumn(nC,&lvcolu);
strTmp=lvcolu.pszText;
strTmp+=_T(" ");
strWrite+=strTmp;
}
strWrite+=_T(" ");
nBytes=0;
bufWrite=Cov(strWrite,nBytes);
f.Write(bufWrite,nBytes);
if (bufWrite!=NULL)
{
delete[]bufWrite;
bufWrite=NULL;
}
//开始写电检结果
nRowNum=m_listElec.GetItemCount();
for (nR=0;nR<nRowNum;nR++)
{
strWrite.Empty();
for (nC=0;nC<nColCount;nC++)
{
strTmp.Empty();
strTmp=m_listElec.GetItemText(nR,nC);
strTmp+=_T(" ");
strWrite+=strTmp;
}
strWrite+=_T(" ");
nBytes=0;
bufWrite=Cov(strWrite,nBytes);
f.Write(bufWrite,nBytes);
if (bufWrite!=NULL)
{
delete[]bufWrite;
bufWrite=NULL;
}
}

//开始气密性信息写入
strWrite.Empty();
strWrite+=_T(" ");
strTmp=_T("气密性能检测: ");
strWrite+=strTmp;
nColCount=m_listGas.GetHeaderCtrl()->GetItemCount();
for (nC=0;nC<nColCount;nC++)
{
strTmp.Empty();
m_listGas.GetColumn(nC,&lvcolu);
strTmp=lvcolu.pszText;
strTmp+=_T(" ");
strWrite+=strTmp;
}
strWrite+=_T(" ");
nBytes=0;
bufWrite=Cov(strWrite,nBytes);
f.Write(bufWrite,nBytes);
if (bufWrite!=NULL)
{
delete[]bufWrite;
bufWrite=NULL;
}
//开始写气密性结果
nRowNum=m_listGas.GetItemCount();
for (nR=0;nR<nRowNum;nR++)
{
strWrite.Empty();
for (nC=0;nC<nColCount;nC++)
{
strTmp.Empty();
strTmp=m_listGas.GetItemText(nR,nC);
strTmp+=_T(" ");
strWrite+=strTmp;
}
strWrite+=_T(" ");
nBytes=0;
bufWrite=Cov(strWrite,nBytes);
f.Write(bufWrite,nBytes);
if (bufWrite!=NULL)
{
delete[]bufWrite;
bufWrite=NULL;
}
}
f.Flush();
f.Close();
MessageBox(_T("导出成功!"),_T("提示"));
}
catch(...)
{

}
}
}

原文地址:https://www.cnblogs.com/lishennan/p/4775560.html