我们在一些项目中仅需要导出简单的数据至Excel,这个过程有很多方法,但是今天我将为您分享的是在C++中导出自定义格式的Excel实现方法。

本篇将分享如何导出如上图供货清单的代码实例
第一步:下载LibXL类库
第二步:新建项目并引入
#include "libxl.h"#include <sstream>using namespace libxl;
第三步:主要功能实现
Book* book = xlCreateBook(); int logoId = book->addPicture(L"logo.png"); // 字体预设,包含字 、字色等,可以提前定义几个 Font* textFont = book->addFont(); textFont->setSize(8); textFont->setName(L"Century Gothic"); Font* titleFont = book->addFont(textFont); titleFont->setSize(38); titleFont->setColor(COLOR_GRAY25); Font* font12 = book->addFont(textFont); font12->setSize(12); Font* font10 = book->addFont(textFont); font10->setSize(10); // 格式预设,比如对齐、文本格式、边框格式等 Format* textFormat = book->addFormat(); textFormat->setFont(textFont); textFormat->setAlignH(ALIGNH_LEFT); Format* titleFormat = book->addFormat(); titleFormat->setFont(titleFont); titleFormat->setAlignH(ALIGNH_RIGHT); Format* companyFormat = book->addFormat(); companyFormat->setFont(font12); Format* dateFormat = book->addFormat(textFormat); dateFormat->setNumFormat(book->addCustomNumFormat(L"[$-409]mmmm\ d\,\ yyyy;@")); Format* phoneFormat = book->addFormat(textFormat); phoneFormat->setNumFormat( book->addCustomNumFormat(L"[<=9999999]###\-####;\(###\)\ ###\-####") ); Format* borderFormat = book->addFormat(textFormat); borderFormat->setBorder(); borderFormat->setBorderColor(COLOR_GRAY25); borderFormat->setAlignV(ALIGNV_CENTER); Format* percentFormat = book->addFormat(borderFormat); percentFormat->setNumFormat(book->addCustomNumFormat(L"#%_)")); percentFormat->setAlignH(ALIGNH_RIGHT); Format* textRightFormat = book->addFormat(textFormat); textRightFormat->setAlignH(ALIGNH_RIGHT); textRightFormat->setAlignV(ALIGNV_CENTER); Format* thankFormat = book->addFormat(); thankFormat->setFont(font10); thankFormat->setAlignH(ALIGNH_CENTER); Format* dollarFormat = book->addFormat(borderFormat); dollarFormat->setNumFormat( book->addCustomNumFormat(L"_($* # ##0.00_);_($* (# ##0.00);_($* -_);_(@_)") ); // 数据填充,先根据我们自定义内容按照坐标添加sheet、title、绑定格式等 Sheet* sheet = book->addSheet(L"Sales Receipt"); sheet->setDisplayGridlines(false); sheet->setCol(1, 1, 36); sheet->setCol(0, 0, 10); sheet->setCol(2, 4, 11); sheet->setRow(2, 47.25); sheet->writeStr(2, 1, L"Sales Receipt", titleFormat); sheet->setMerge(2, 2, 1, 4); sheet->setPicture(2, 1, logoId); sheet->writeStr(4, 0, L"Apricot Ltd.", companyFormat); sheet->writeStr(4, 3, L"Date:", textFormat); sheet->writeFormula(4, 4, L"TODAY()", dateFormat); sheet->writeStr(5, 3, L"Receipt #:", textFormat); sheet->writeNum(5, 4, 652, textFormat); sheet->writeStr(8, 0, L"Sold to:", textFormat); sheet->writeStr(8, 1, L"John Smith", textFormat); sheet->writeStr(9, 1, L"Pineapple Ltd.", textFormat); sheet->writeStr(10, 1, L"123 Dreamland Street", textFormat); sheet->writeStr(11, 1, L"Moema, 52674", textFormat); sheet->writeNum(12, 1, 2659872055, phoneFormat); sheet->writeStr(14, 0, L"Item #", textFormat); sheet->writeStr(14, 1, L"Description", textFormat); sheet->writeStr(14, 2, L"Qty", textFormat); sheet->writeStr(14, 3, L"Unit Price", textFormat); sheet->writeStr(14, 4, L"Line Total", textFormat); for(int row = 15; row < 38; ++row) { sheet->setRow(row, 15); for(int col = 0; col < 3; ++col) { sheet->writeBlank(row, col, borderFormat); } sheet->writeBlank(row, 3, dollarFormat); std::wstringstream stream; stream << "IF(C" << row + 1 << ">0;ABS(C" << row + 1 << "*D" << row + 1 << ");""")""; sheet->writeFormula(row声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!