C++各类文件的基本操作(Xml/Excel)

XML文件(MFC)

 1.打开xml文件并读取内容例子:

CFileDialog dlg(TRUE, _T("xml"), NULL, OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST, _T("XML Files (*.xml)|*.xml||"), NULL);

	if (dlg.DoModal() == IDOK)
	{
		m_Man.m_strOpenXmlFilePath = dlg.GetPathName();
	}
	if (!m_Man.OpenXmlFile(m_Man.m_strOpenXmlFilePath))
	{
		Msg_OutInfo(_T("读取文件失败"));
		return;
	}
bool ThkabcMng::OpenXmlFile(CString  strFilePath)
{
	Reset();
	ThkXmlPlus xmlPlus;
	xmlPlus.SetPathName(strFilePath);
	xmlPlus.Load();
	ThkXmlElement* pElem = xmlPlus.GetRoot();
	if (!pElem)
		return false;
	ThkXmlIndex propIdx = pElem->FindChild("ModelNode"); 
	if (propIdx.IsEmpty())
	{
		return false;
	}
	CString strProp = (*propIdx).GetAttributeValue("ModelName");
	CString strVal = (*propIdx).GetAttributeValue("NodeId");
	if (!m_tAsm.InitAsmMdl(strProp,_tstoi(strVal)))
	{
		return false;
	}
	if (!ReadXmlFile(*propIdx, &m_tAsm))
	{
		return false;
	}
	return true;
}
bool ThkabcMng::ReadXmlFile(ThkXmlElement& Element, Thk501ToolAsmComp* pAsm)
{
	if (pAsm==NULL)
	{
		return false;
	}
	ThkXmlIndex propIdx = Element.FindChild("ModelNode");
	while (!propIdx.IsEmpty())
	{
		CString strProp = (*propIdx).GetAttributeValue("ModelName");

		CString strVal = (*propIdx).GetAttributeValue("NodeId");
		//加入数组
		ThkabcComp* pComp= new ThkabcComp;
		if (!pComp->InitAsmMdl(strProp,_tstoi(strVal)))
		{
			delete pComp;
			pComp = NULL;
			continue;
		}
		pComp->m_pParent = pAsm;
		pAsm->m_arrComp.push_back(pComp);
		ReadXmlFile(*propIdx, pComp);
		propIdx = Element.FindChild("ModelNode",propIdx);
	}
	return true;
}

2.XML文件的导出并保存

void ThkabcDlg::OnBnClickedBtnexportxml()
{
	// TODO: 在此添加控件通知处理程序代码
	CFileDialog dlg(FALSE, _T("xml"), NULL, OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST, _T("XML Files (*.xml)|*.xml||"), NULL);
	if (dlg.DoModal() == IDOK)
	{
		m_Man.m_strExportXmlFilePath = dlg.GetPathName();
	}
	if (!m_Man.ExportXmlFile())
	{
		Msg_OutInfo(_T("导出文件失败"));
		return;
	}
	ShowComBox();
	Msg_OutInfo(_T("导出文件成功"));
	
}
bool ThkabcMng::ExportXmlFile()
{
	ThkXmlPlus xmlPlus;
	xmlPlus.SetPathName(m_strExportXmlFilePath);
	xmlPlus.SetHeader();
	xmlPlus.SetRoot("ModelTree");
	ThkXmlElement* pElem = xmlPlus.GetRoot();
	if (pElem==NULL)
	{
		return false;
	}
	ThkXmlIndex pClsIdx = pElem->AddChild("ModelNode");
	if (pClsIdx.IsEmpty())
	{
		return false;
	}
	pClsIdx->SetAttributeValue("ModelName", m_tAsm.m_strNodeName);
	CString strNodeCode;
	strNodeCode.Format(_T(" % d"), m_tAsm.m_nNodeCode);
	pClsIdx->SetAttributeValue("NodeId",strNodeCode);
	if (!SaveXmlFile(*pClsIdx, &m_tAsm))
	{
		return false;
	}
	return xmlPlus.Save();
}
bool ThkabcMng::SaveXmlFile(ThkXmlElement& Element, ThkabcComp* pAsm)
{
	for (int i=0;i<(int)pAsm->m_arrComp.GetCount();i++)
	{
		Thk501ToolAsmComp* pComp = pAsm->m_arrComp[i];
		if (pComp==NULL)
		{
			continue;
		}
		ThkXmlIndex propIdx = Element.AddChild("ModelNode");
		if (!propIdx.IsEmpty())
		{
			propIdx->SetAttributeValue("ModelName", pComp->m_strNodeName);
			CString strNodeCode;
			strNodeCode.Format(_T(" % d"), pComp->m_nNodeCode);
			propIdx->SetAttributeValue("NodeId", strNodeCode);

			SaveXmlFile(*propIdx, pComp);
		}
	}
	return true;
}

3.EXCEL文件的读取和写入(Qt)

3.1读取

bool Thkabc::ReadFile()
{
	m_Filename=QFileDialog::getOpenFileName(NULL,_TQ("选择清单文件"),"*.xml");
	TbaExcelXml tXml;
	/*TbaGlobal  gl;
	m_Filename=gl.GetSupportPath("config");
	QDir dir(m_Filename);
	m_Filename = dir.absolutePath();
	m_Filename += _TQ("装配清单.xml");*/
	if (!tXml.OpenExcelFile(m_Filename))
	{
		return false;
	}
	if (!tXml.SetActiveSheet(0))                //激活页
	{
		return false;
	}
	int nRow,nCol;
	if (!tXml.GetUsedRange(nRow, nCol))           //获取XML数据的行和列
	{
		return false;
	}

	for (int i=2;i<=nRow;i++)
	{
		QString tstrCode = tXml.GetCell(i, 1);
		QString tstrMdl = tXml.GetCell(i, 2);
		QString tstrSrf = tXml.GetCell(i, 3);
		QString tstrX = tXml.GetCell(i, 4);
		QString tstrY = tXml.GetCell(i, 5);
		if (!DetermineCode(tstrCode))
		{
			continue;
		}
		if (tstrCode.isEmpty() && tstrMdl.isEmpty() && tstrSrf.isEmpty() && tstrX.isEmpty() && tstrY.isEmpty())
		{
			continue;
		}
		ThkGenTestCompInformation* pMdlIFMS = new ThkGenTestCompInformation;
		if (!pMdlIFMS->SetInformation(tstrCode, tstrMdl, tstrSrf, tstrX, tstrY))
		{
			return false;
		}
		m_vMdlIfms.push_back(pMdlIFMS);
	}
	return true;
}

3.2写入

bool ThkabcMan::WriteFile()
{
	TbaExcelXml tXml;
	tXml.AddSheet("abc");
	tXml.SetCellData(1, 1, _TQ("元件编号"));
	tXml.SetCellData(1, 2, _TQ("元件模型"));
	tXml.SetCellData(1, 3, _TQ("所属安装面"));
	tXml.SetCellData(1, 4, _TQ("位置X"));
	tXml.SetCellData(1, 5, _TQ("位置Y"));
	for (int i=0;i<m_vMdlIfms.size();i++)
	{
		tXml.SetCellData(i + 2, 1, m_vMdlIfms[i]->m_tstrCode);
		tXml.SetCellData(i + 2, 2, m_vMdlIfms[i]->m_tstrMdl);
		tXml.SetCellData(i + 2, 3, m_vMdlIfms[i]->m_tstrSrf);
		tXml.SetCellData(i + 2, 4, QString::number(m_vMdlIfms[i]->m_dX));
		tXml.SetCellData(i + 2, 5, QString::number(m_vMdlIfms[i]->m_dY));
	}
	if (m_Filename==NULL)
	{
		msg_outErr(_TQ("导出失败,请先打开您想要保存的文件"));
		return false;
	}
	if (!tXml.SaveAs(m_Filename))
	{
		msg_outInfo(_TQ("导出失败"));
		return false;
	}
	msg_outInfo(_TQ("导出成功"));
	return true;
}

4.导出XLSX文件(C++、MFC)

void ThabcDlg::OnBnClickedReportExport()
{
	CString strInfo;
	//strInfo.Format(_T("%s焊缝统计表.xlsx"), m_Man.m_tSldAsm.GetName());
	strInfo =  m_Man.m_tSldAsm.GetName()+_T("焊缝统计表.xlsx");
	CFileDialog file(FALSE, NULL, strInfo, OFN_PATHMUSTEXIST | OFN_HIDEREADONLY, _T("*.xlsx|*.xlsx||"), this);
	if (file.DoModal() != IDOK)
	{
		return;
	}
	CString strPath = file.GetPathName();
	if (strPath.Find(_T(".xlsx")) < 0)
	{
		strPath += _T(".xlsx");
	}
	if (m_Man.ExpOutMdl(strPath))
	{
		Msg_OutInfo("导出成功");
	}
	else
	{
		Msg_OutErr(_T("导出失败"));
	}
}


bool ThkabcMng::ExpOutMdl(CString strPath)
{
	if (strPath.IsEmpty())
	{
		return false;
	}
	ThkExcelTool tExcel;
	tExcel.CreateBook(true);
	tExcel.AddSheet("焊缝模型信息统计");
	tExcel.SetActiveSheet(0);
	tExcel.SetCellData(1, 1, _T("序号"));
	tExcel.SetCellData(1, 2, _T("焊缝名字"));
	tExcel.SetCellData(1, 3, _T("起端"));
	tExcel.SetCellData(1, 4, _T("末端"));
	tExcel.SetCellData(1, 5, _T("所属装配"));
	int nRow = 2;
	for (int i = 0; i <m_vWeldMdl.size(); i++)
	{
		
		ThkPipWeldMdl* pComp = m_vWeldMdl[i];
		if (pComp==NULL)
		{
			continue;
		}
		tExcel.SetCellData(nRow, 1, i+1);
		tExcel.SetCellData(nRow, 2, pComp->m_strName);
		tExcel.SetCellData(nRow, 3,pComp->m_strStartCompName);
		tExcel.SetCellData(nRow, 4, pComp->m_strEndCompName);
		tExcel.SetCellData(nRow, 5, pComp->m_strBelongName);
		nRow++;
	}
	
	if (!tExcel.SaveAs(strPath))
	{
		return false;
	}
	return true;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

行为艺术家Zzz

新人 求鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值