windows下C++读取Excel(仅支持xlsx格式)

test.cpp

#include 
#include 

//Unicode 转 utf-8
bool WStringToString(const std::wstring &wstr, std::string &str)
{
	int nLen = (int)wstr.length();
	int nDesSize = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)wstr.c_str(), nLen, NULL, 0, NULL, NULL);
	str.resize(nDesSize, '\0');

	int nResult = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)wstr.c_str(), nLen, (LPSTR)str.c_str(), nDesSize, NULL, NULL);

	if (nResult == 0)
	{
		//DWORD dwErrorCode = GetLastError();
		return FALSE;
	}

	return TRUE;
}

int Writexl(const char* filename)
{
	const char Signal_0Name[][16] = { "id", "chID", "PTCPCBTem", " PTCOverHeat", "PTCOverCrnt", "PTCOverVol", " PTCRlngCtr", "PTCActuCrnt", "PTCActuPwr", "Ptc_work_statu", "PTCUnderVol", "PTCIGBTSts", " PTCHVLckSts", "PTCIGBTOverHeat", "PTCChksm" };
	const char Signal_1Name[][16] = { "id", "chID", "PTCSecRlngCtr", "PTCActuVol", " PTCINNTCSts", "PTCOUTNTCSts", "PTCIGBTNTCSts", "PTCIGBTTem", "PTCInptTem", "PTCOtptTem", "PTCSecChksm" };

	xlnt::workbook wb;
	xlnt::worksheet sheetMain = wb.active_sheet();
	xlnt::worksheet sheet1 = wb.copy_sheet(sheetMain);
	xlnt::worksheet sheet2 = wb.copy_sheet(sheet1);

	//----------------sheetmain----------------------------------------------
	//常规操作
	sheetMain.cell("A1").value(5);
	sheetMain.cell("B2").value("string data");
	sheetMain.cell("C3").formula("=RAND()");
	//中文处理
	std::string strDes;
	WStringToString(L"测试", strDes);
	sheetMain.cell(5, 5).value(strDes);

	sheetMain.title("main");

	//----------------sheet1----------------------------------------------
	//行批量写入
	for (int i = 0; i<15; i++)
	{
		sheet1.cell(i + 1, 1).value(Signal_0Name[i]);
	}
	//常规操作
	sheet1.cell("A2").value(5);
	sheet1.cell("B3").value("string data");
	sheet1.cell("C4").formula("=RAND()");

	//合并单元格
	sheet1.merge_cells("C3:C4");
	sheet1.freeze_panes("B2");

	sheet1.title("sheet1");

	//----------------sheet2----------------------------------------------
	//列批量写入
	for (int i = 0; i<11; i++)
	{
		sheet2.cell(1, i + 1).value(Signal_1Name[i]);
	}
	sheet2.cell("C1").value("C1");
	sheet2.cell("B1").value("B1");
	sheet2.title("sheet2");
	sheet2.freeze_panes("B2");

	if(filename[0] == '\0')
		wb.save("test.xlsx");
	else
		wb.save(filename);
	return 0;
}

int Readxl(const char* filename)
{
	int err = 0;
	xlnt::workbook wb/*(xlnt::path("E:\\project\\xyyh\\xltest\\test_passwd.xlsx"),"1234")*/;
	//if (filename[0] == '\0')
		//err = wb.load("E:\\project\\xyyh\\xltest\\test.xlsx");
		err = wb.load("E:\\project\\xyyh\\xltest\\test.xlsx");
	//else
	//	err = wb.load(filename);

	if (err)
		return err;

	//xlnt::worksheet ws = wb.active_sheet();

	std::vector<std::string> names = wb.sheet_titles();

	for (int i = 0;i < names.size();i++)
	{
		xlnt::worksheet ws = wb.sheet_by_title(names[i]);
		xlnt::range cols = ws.columns();
		xlnt::range rows = ws.rows();
		int ColLength = cols.length();
		int RowLength = rows.length();
		std::cout << "test有效列数为: " << ColLength << std::endl;
		std::cout << "test有效行数为: " << RowLength << std::endl;

		std::string A1Value = cols[0][0].value<std::string>();
		std::string A1Value2 = cols[0][1].value<std::string>();
		std::string A1Value3 = cols[1][0].value<std::string>();
		std::string A1Value4 = cols[1][1].value<std::string>();
		std::string B1Value = rows[0][0].value<std::string>();
		double B1Value2 = rows[0][1].value<double>();
		std::string B1Value3 = rows[1][0].value<std::string>();
		std::string B1Value4 = rows[1][1].value<std::string>();
	}
	return 0;
}

int main()
{
	Writexl("");
	Readxl("");

	return 0;
}

库下载地址:https://download.csdn.net/download/gongxie0235/11296269
或者关注我微信公众号《码农茶歇》回复“xlnt”免费下载。

你可能感兴趣的:(windows,windows,xlnt,C++,excel)