C++ 对Excel的操作

虽然网上对Excel的操作已经很多了,但还是想在这里在谈谈,也对自己这段时间对Excel操作的总结吧,这里和网上部分不同的是存在着Excel的模板

1)检查模板

2)生成临时模板的目标文件

3)将模板中的内容先复制到目标文件

4)写信息

5)保存Excel

PS : 在保存excel时,删除

关于excel.h和excel.cpp的生成和修改在之前的文章中就说过,这儿就不再说了,来具体的吧。

 

	_Application  m_ExcelApp;
	Workbooks     m_ExcelBooks;
	_Workbook     m_ExcelBook;
	Worksheets    m_ExcelSheets;
	_Worksheet    m_ExcelSheet;
	Range         m_Range;

/*******************************************************************************
函数名   :OpenExcel
功能     :打开Excel程序
输入参数 :无
输出参数 :无
返回值   :BOOL
编写人   :GongHiro
日起     :2013-10-14
*******************************************************************************/
BOOL CControlExcel::OpenExcel(CString szFilePath)
{
	//syetem win8.1 office2010(破解版),CreateDispatch失败,但可以创建生成Excel
	if (0 != CoInitialize(NULL))
	{
		//MessageBox(_T("初始化COM Excel类型库失败"));
		//return FALSE;
	}

	if (!m_ExcelApp.CreateDispatch(_T("Excel.Application"), NULL))
	{
		//MessageBox(_T("初始化Excel应用程序失败"));
		//return FALSE;
	}
	try
	{
		m_ExcelBooks.AttachDispatch(m_ExcelApp.GetWorkbooks(), TRUE);
		m_ExcelBook.AttachDispatch(m_ExcelBooks.Add((_variant_t)szFilePath), TRUE);  
		m_ExcelSheets.AttachDispatch(m_ExcelBook.GetSheets(), TRUE);
	}
	catch(...)
	{
		;
	}
	//设置Excel显示
	m_ExcelApp.SetVisible(FALSE);

	return TRUE;
}


/*******************************************************************************
函数名   :CloseExcel
功能     :关闭Excel程序
输入参数 :无
输出参数 :无
返回值   :BOOLz
编写人   :GongKiro
日期     :2013-10-14
*******************************************************************************/
BOOL CControlExcel::CloseExcel()
{
	m_Range.ReleaseDispatch();  
	m_ExcelSheet.ReleaseDispatch();  
	m_ExcelSheets.ReleaseDispatch();  
	m_ExcelBook.ReleaseDispatch();  
	m_ExcelBooks.ReleaseDispatch();  
	m_ExcelApp.SetDisplayAlerts(FALSE);  
	m_ExcelApp.Quit();  
	m_ExcelApp.SetDisplayAlerts(TRUE);  
	m_ExcelApp.ReleaseDispatch();  
	OleUninitialize();  

	return TRUE;
}


/*******************************************************************************
函数名   :checkFile
功能     :check File exit and is open or not
输入参数 :无
输出参数 :无
返回值   :BOOL
编写人   :GongKiro
日起     :2013-10-14
*******************************************************************************/
BOOL CControlExcel::CheckFile(CString szFilePath, CString szWarning)
{
	CFile File;
	CFileFind FileFind;
	
	//检查文件是否存在
	BOOL bTemp = szFilePath.IsEmpty();
	bTemp = FileFind.FindFile(szFilePath);
	if (szFilePath.IsEmpty() || (FALSE == FileFind.FindFile(szFilePath)))
	{
		szWarning.Format("File %s doesn't exist, please check it", szFilePath);
		FileFind.Close();
		return FALSE;
	}
	//检查文件是否被打开
	if (!File.Open(szFilePath, CFile::modeRead))
	{
		szWarning.Format("%s is in use by another program, please close it first!", szFilePath);
		File.Close();
		return FALSE;
	}
	File.Close();
	FileFind.Close();
	
	return TRUE;
}


/*******************************************************************************
函数名   :checkFile
功能     :check File exit and is open or not
输入参数 :无
输出参数 :无
返回值   :BOOL
编写人   :GongKiro
日起     :2013-10-14
*******************************************************************************/
BOOL CControlExcel::ExportInfoToFile(CString szWarning)
{
	CFileDialog dlg(FALSE, "", "ExcelTest", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
		("*.xls"),NULL);
	if (IDOK != dlg.DoModal())
	{
		return FALSE;
	}
	CString szSavePath = dlg.GetPathName();

	CString szEXEPath;
	CString Temp;
	szEXEPath.ReleaseBuffer(GetModuleFileName(NULL,szEXEPath.GetBuffer(MAX_PATH),MAX_PATH));
	int idx = szEXEPath.ReverseFind(_T('\\'));
	Temp = szEXEPath.Left(idx);
	Temp = szEXEPath.Left(Temp.ReverseFind(_T('\\')));
	//GetCurrentDirectory(MAX_PATH, szEXEPath.GetBuffer(MAX_PATH));
	
	CString szFilePath      = Temp + "\\template\\ExcelTest.xlsx";
	CString szFilePathTemp  = Temp + "\\template\\ExcelTest1.xlsx";

	if (FALSE == CheckFile(szFilePath, szWarning))
	{
		return FALSE;
	}

	CFileFind FileFind;
	BOOL bDesFileExists = FileFind.FindFile(szFilePathTemp);
	if (TRUE == bDesFileExists)
	{
		CFile Fileopen;
		if (!Fileopen.Open(szFilePathTemp, CFile::modeRead))
		{
			Fileopen.Close();
			szWarning.Format("%s is used by another program, please close it first!", szFilePathTemp);  
		}
	}
	FileFind.Close();

	if (TRUE == bDesFileExists)
	{
		CopyFile(szFilePath, szFilePathTemp, FALSE);
	}
	else
	{
		CopyFile(szFilePath,szFilePathTemp, TRUE);
	}
	if (!OpenExcel(szFilePathTemp))
	{
		szWarning.Format("%s Open Faile !", szFilePathTemp);
		return FALSE;
	}

	try
	{
		COleVariant
			covTrue((short)TRUE),
			covFalse((short)FALSE),
			covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR); 

		//m_ExcelBooks.AttachDispatch(m_ExcelApp.GetWorkbooks(), TRUE);
		//m_ExcelBook.AttachDispatch(m_ExcelBooks.Add((_variant_t)szFilePathTemp), TRUE);  
		//m_ExcelSheets.AttachDispatch(m_ExcelBook.GetSheets(), TRUE);
		m_ExcelSheet.AttachDispatch(m_ExcelSheets.GetItem((_variant_t)(short)(1)));
		m_Range.AttachDispatch(m_ExcelSheet.GetCells(), TRUE);

		for (int i = 1; i < 4; ++i)
		{
			m_Range.SetItem((_variant_t)(3), (_variant_t)(i),(COleVariant)(_T("Hiro")));
			m_Range.SetItem((_variant_t)(2), (_variant_t)(i),(_variant_t)(i));
		}
	}
	catch (...)
	{
		szWarning.Format("%s Faile to Export");
		CloseExcel();
		return FALSE;
	}
	CFile::Remove(szFilePathTemp);
	CString szExcelVersion = m_ExcelApp.GetVersion();
	if("9.0" == szExcelVersion || "10.0" == szExcelVersion || "11.0" == szExcelVersion)
	{
		m_ExcelSheet.SaveAs(szSavePath, vtMissing, vtMissing, vtMissing, vtMissing, 
			vtMissing, vtMissing, vtMissing, vtMissing, vtMissing);
	}
	else
	{
		m_ExcelSheet.SaveAs(szSavePath, _variant_t(56), vtMissing, vtMissing, vtMissing, 
			vtMissing, vtMissing, vtMissing, vtMissing, vtMissing);
	}
	

	if (!CloseExcel())
	{
		szWarning.Format("Failed to close %s", szSavePath);
	}

	return TRUE;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值