VC++讀取Excel表格和向Excel表格輸出內容示範代碼

在寫程序的過程中大家經常會遇到操作Excel進行表格內容讀寫的需求,這裏我給大家分享一下如何用VC++來實現Excel讀取。微軟的office系統提供了OLE COM自動化功能,通過COM接口操作word和Excel非常方便,在編寫代碼時首先需要將Excel類型庫引入工程項目中,其主要步驟下:1)在項目菜單中選擇“類嚮導“;2)選擇”類型爲呂的MFC類“,並從文件中找到office所在的目錄,打開本機安裝的Excel的可執行文件;3)在接口中選擇_Application、_Wordbook、_Worksheet、range、font等並生成相關類。其操作如下圖所示:

但由於office版本差異導入時接口也有一些不同,並且程序 在其他機器上運行時版本不匹配會的問題,爲了解決這個問題,可以用office2003生成一個類型庫,本示例代碼中已經生成了相關頭文件和CPP(excel9.h和excel9.cpp),大家可以直接用,它可以兼容2003及以上版本的excel。

本示例代碼主要包括兩個功能,讀取Excel表格內容和輸出Excel內容,其函數主要代碼如下:

//讀取Excel文件函數
void ReadExcelFile(CString strFileName)
{
	xsc::_Application  ExcelApp;           // 定義Excel應用程序
	xsc::Workbooks     wbsBooks;
	xsc::_Workbook     wbBook;
	xsc::Sheets        wssSheets;
	xsc::_Worksheet    wsSheet;

	if (::CoInitialize(NULL) == E_INVALIDARG)
	{
		AfxMessageBox(_T("初始化Com失敗!"));
	}
	// 創建Excel2000服務器(啓動Excel)
	if (!ExcelApp.CreateDispatch(_T("Excel.Application"), NULL))
	{
		AfxMessageBox(_T("創建Excel服務失敗!"));
		::CoUninitialize();
	}
	ExcelApp.SetVisible(false);

	try
	{
		wbsBooks.AttachDispatch(ExcelApp.GetWorkbooks(), TRUE);
		wbBook.AttachDispatch(wbsBooks.Add(COleVariant(strFileName)), TRUE);

		wssSheets = wbBook.GetWorksheets();
		wsSheet = wssSheets.GetItem(_variant_t("Sheet1")); // Get Sheet1
	}
	catch (...)
	{
		AfxMessageBox("error");
	}

	//這裏只是示例一下讀取單元格內容的方式,只讀取A,B, C三列,兩行的內容
	CString strAllValue;
	for (int i = 0; i < 2; i++)
	{
		//讀取第i行A列的數據
		CString strColumn, strCellValue;
		strColumn.Format("A%d", i + 1);
		xsc::Range range = wsSheet.GetRange(COleVariant(strColumn), COleVariant(strColumn));
		_variant_t varValue = range.GetValue();
		strCellValue = (char*)_bstr_t(varValue);
		strAllValue = strAllValue + strCellValue + "\t";

		//讀取第i行B列的數據
		strColumn.Format("B%d", i + 1);
		range = wsSheet.GetRange(COleVariant(strColumn), COleVariant(strColumn));
		varValue = range.GetValue();
		strCellValue = (char*)_bstr_t(varValue);
		strAllValue = strAllValue + strCellValue + "\t";

		//讀取第i行C列的數據
		strColumn.Format("C%d", i + 1);
		range = wsSheet.GetRange(COleVariant(strColumn), COleVariant(strColumn));
		varValue = range.GetValue();
		strCellValue = (char*)_bstr_t(varValue);
		strAllValue = strAllValue + strCellValue + "\r\n";
	}

	ExcelApp.Quit();

	AfxMessageBox(strAllValue);
}
//生成並保存Excel文件
void SaveExcelFile(CString strFileName)
{
	using namespace xsc;
	COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND,
		VT_ERROR), vtTrue((short)TRUE), vtFalse((short)FALSE);
	xsc::_Application *ExcelApp = new _Application;

	ExcelApp->CreateDispatch("excel.application");

	//得到WorkBooks
	xsc::Workbooks  ExcelBook = ExcelApp->GetWorkbooks();
	xsc::Sheets     ExcelSheet = ExcelBook.Add(vtOptional);
	xsc::_Workbook  workBook;
	xsc::_Worksheet workSheet;
	xsc::Range    range;
	workBook.AttachDispatch(ExcelApp->GetApplication());
	ExcelSheet = workBook.GetSheets();

	////////////////////////////////////////////
	workSheet = ExcelSheet.GetItem(COleVariant((short)1));
	workSheet.Activate();

	//*********************************************************************************//
	//寫表頭
	Range cell_range = workSheet.GetRange(COleVariant("A1"), COleVariant("A1"));
	cell_range.BorderAround(COleVariant((short)1), (long)2, (long)1, vtOptional);//設置邊框屬性
	cell_range.SetHorizontalAlignment(COleVariant((short)3));//設置水平對齊方式
	cell_range.SetVerticalAlignment(COleVariant((short)2));//設置垂直對齊方式
	cell_range.SetValue(COleVariant("姓名"));
	xsc::Font font = cell_range.GetFont();
	font.SetSize(COleVariant((short)10));//設置字體大小,也可以設置字體

	cell_range = workSheet.GetRange(COleVariant("B1"), COleVariant("B1"));
	cell_range.BorderAround(COleVariant((short)1), (long)2, (long)1, vtOptional);
	cell_range.SetHorizontalAlignment(COleVariant((short)3));
	cell_range.SetVerticalAlignment(COleVariant((short)2));
	cell_range.SetValue(COleVariant("年齡"));
	font = cell_range.GetFont();
	font.SetSize(COleVariant((short)10));

	cell_range = workSheet.GetRange(COleVariant("C1"), COleVariant("D1"));
	cell_range.BorderAround(COleVariant((short)1), (long)2, (long)1, vtOptional);
	cell_range.SetHorizontalAlignment(COleVariant((short)3));
	cell_range.Merge(COleVariant((short)0));//這裏是合併單元格
	cell_range.SetVerticalAlignment(COleVariant((short)2));
	cell_range.SetValue(COleVariant("簡介"));
	font = cell_range.GetFont();
	font.SetSize(COleVariant((short)10));

	//下面可以循環寫內容,這裏只是示例一下只填充一行數據
	cell_range = workSheet.GetRange(COleVariant("A2"), COleVariant("A2"));
	cell_range.BorderAround(COleVariant((short)1), (long)2, (long)1, vtOptional);
	cell_range.SetHorizontalAlignment(COleVariant((short)3));
	cell_range.SetVerticalAlignment(COleVariant((short)2));
	cell_range.SetValue(COleVariant("張三"));
	font = cell_range.GetFont();
	font.SetSize(COleVariant((short)10));

	cell_range = workSheet.GetRange(COleVariant("B2"), COleVariant("B2"));
	cell_range.BorderAround(COleVariant((short)1), (long)2, (long)1, vtOptional);
	cell_range.SetHorizontalAlignment(COleVariant((short)3));
	cell_range.SetVerticalAlignment(COleVariant((short)2));
	cell_range.SetValue(COleVariant("28"));
	font = cell_range.GetFont();
	font.SetSize(COleVariant((short)10));

	cell_range = workSheet.GetRange(COleVariant("C2"), COleVariant("D2"));
	cell_range.BorderAround(COleVariant((short)1), (long)2, (long)1, vtOptional);
	cell_range.SetHorizontalAlignment(COleVariant((short)3));
	cell_range.Merge(COleVariant((short)0));
	cell_range.SetVerticalAlignment(COleVariant((short)2));
	cell_range.SetValue(COleVariant("這裏需要填寫個人簡歷......"));
	font = cell_range.GetFont();
	font.SetSize(COleVariant((short)10));
	cell_range.SetColumnWidth(COleVariant((short)30));


	//設置爲顯示
	ExcelApp->SetVisible(TRUE);
	delete ExcelApp;

}

這個示例代碼很好地演示了讀取和輸出Excel文件的操作,工程是在VS2017下編寫並編譯通過,大家可以下載源代碼,並直播引用Excel的相關類文件,希望能給大家提供幫助!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章