xlnt_demo,利用C++开源库xlnt读写excel,vs2015工程
立即下载
资源介绍:
利用C++开源库xlnt读写excel的vs2015工程。详细教程参考博客:https://humfrey.blog.csdn.net/article/details/107527883
#include
#include
#include
#include
void read_to_screen(std::string filepath)
{
xlnt::workbook wb;
wb.load(filepath);
auto ws = wb.active_sheet();
std::clog << "Processing spread sheet" << std::endl;
// 会把当前占用的单元格全都打印出来,空单元格则为空格。
for (auto row : ws.rows(false))
{
for (auto cell : row)
{
std::clog << cell.to_string() << "\t";
}
std::clog << std::endl;
}
std::clog << "Processing complete" << std::endl;
}
std::vector< std::vector > read_to_vector(std::string filepath)
{
xlnt::workbook wb;
wb.load(filepath);
auto ws = wb.active_sheet();
std::clog << "Processing spread sheet" << std::endl;
std::clog << "Creating a single vector which stores the whole spread sheet" << std::endl;
std::vector< std::vector > theWholeSpreadSheet;
for (auto row : ws.rows(false))
{
std::vector aSingleRow;
for (auto cell : row)
{
aSingleRow.push_back(cell.to_string());
}
theWholeSpreadSheet.push_back(aSingleRow);
}
std::clog << "Processing complete" << std::endl << std::endl;
std::clog << "Reading the vector and printing output to the screen" << std::endl;
for (int rowInt = 0; rowInt < theWholeSpreadSheet.size(); rowInt++)
{
for (int colInt = 0; colInt < theWholeSpreadSheet.at(rowInt).size(); colInt++)
{
std::cout << theWholeSpreadSheet.at(rowInt).at(colInt) << "\t";
}
std::cout << std::endl;
}
return theWholeSpreadSheet;
}
void save_demo(std::string filepath)
{
xlnt::workbook wb;
xlnt::worksheet ws = wb.active_sheet();
//赋值为数值
ws.cell("A1").value(0.5);
//设置单位格格式,设为百分比形式
ws.cell("A1").number_format(xlnt::number_format::percentage());
//赋值为字符串
ws.cell("B2").value("string data");
//赋值为公式
ws.cell("C3").formula("=RAND()");
//合并单元格
ws.merge_cells("C3:C4");
//冻结窗格
ws.freeze_panes("B2");
//保存
wb.save(filepath);
}
void save_from_vector(std::string filepath)
{
std::vector< std::vector > wholeWorksheet;
for (int outer = 0; outer < 10; outer++)
{
std::vector singleRow;
for (int inner = 0; inner < 10; inner++)
{
//给vector中的每个元素赋值
std::string val = std::to_string(inner + 1);
singleRow.push_back(val);
}
wholeWorksheet.push_back(singleRow);
}
//创建工作簿
std::clog << "Creating workbook" << std::endl;
xlnt::workbook wbOut;
//创建工作表,并把vector中的元素写入表格
xlnt::worksheet wsOut = wbOut.active_sheet();
//给工作表设置名称
wsOut.title("data");
std::clog << "Looping through vector and writing to spread sheet" << std::endl;
for (int fOut = 0; fOut < wholeWorksheet.size(); fOut++)
{
std::clog << "Row" << fOut << std::endl;
for (int fIn = 0; fIn < wholeWorksheet.at(fOut).size(); fIn++)
{
// 给单元格赋值
// 特别注意:工作表中的单元格下标是从1开始,而vector中的元素下标是从0开始
wsOut.cell(xlnt::cell_reference(fIn + 1, fOut + 1)).value(wholeWorksheet.at(fOut).at(fIn));
}
}
std::clog << "Finished writing spread sheet" << std::endl;
wbOut.save(filepath);
}
int main()
{
//read_to_screen("test_read.xlsx");
//read_to_vector("test_read.xlsx");
//save_demo("test_save.xlsx");
save_from_vector("test_save.xlsx");
getchar();
return 0;
}
资源文件列表:
xlnt_demo.zip 大约有122个文件