libxl 4.0.3读取Excel
立即下载
资源介绍:
读取表格速度很快
#include
#include
#include "libxl.h"
using namespace libxl;
int main()
{
Book* book = xlCreateXMLBook();
book->setLocale("UTF-8");
if(book)
{
Font* boldFont = book->addFont();
boldFont->setBold();
Font* titleFont = book->addFont();
titleFont->setName(L"Arial Black");
titleFont->setSize(16);
Format* titleFormat = book->addFormat();
titleFormat->setFont(titleFont);
Format* headerFormat = book->addFormat();
headerFormat->setAlignH(ALIGNH_CENTER);
headerFormat->setBorder(BORDERSTYLE_THIN);
headerFormat->setFont(boldFont);
headerFormat->setFillPattern(FILLPATTERN_SOLID);
headerFormat->setPatternForegroundColor(COLOR_TAN);
Format* descriptionFormat = book->addFormat();
descriptionFormat->setBorderLeft(BORDERSTYLE_THIN);
Format* amountFormat = book->addFormat();
amountFormat->setNumFormat(NUMFORMAT_CURRENCY_NEGBRA);
amountFormat->setBorderLeft(BORDERSTYLE_THIN);
amountFormat->setBorderRight(BORDERSTYLE_THIN);
Format* totalLabelFormat = book->addFormat();
totalLabelFormat->setBorderTop(BORDERSTYLE_THIN);
totalLabelFormat->setAlignH(ALIGNH_RIGHT);
totalLabelFormat->setFont(boldFont);
Format* totalFormat = book->addFormat();
totalFormat->setNumFormat(NUMFORMAT_CURRENCY_NEGBRA);
totalFormat->setBorder(BORDERSTYLE_THIN);
totalFormat->setFont(boldFont);
totalFormat->setFillPattern(FILLPATTERN_SOLID);
totalFormat->setPatternForegroundColor(COLOR_YELLOW);
Format* signatureFormat = book->addFormat();
signatureFormat->setAlignH(ALIGNH_CENTER);
signatureFormat->setBorderTop(BORDERSTYLE_THIN);
Sheet* sheet = book->addSheet(L"Invoice");
if(sheet)
{
sheet->writeStr(2, 1, L"Invoice No. 3568", titleFormat);
sheet->writeStr(4, 1, L"Name: John Smith");
sheet->writeStr(5, 1, L"Address: San Ramon, CA 94583 USA");
sheet->writeStr(7, 1, L"Description", headerFormat);
sheet->writeStr(7, 2, L"Amount", headerFormat);
sheet->writeStr(8, 1, L"Ball-Point Pens", descriptionFormat);
sheet->writeNum(8, 2, 85, amountFormat);
sheet->writeStr(9, 1, L"T-Shirts", descriptionFormat);
sheet->writeNum(9, 2, 150, amountFormat);
sheet->writeStr(10, 1, L"Tea cups", descriptionFormat);
sheet->writeNum(10, 2, 45, amountFormat);
sheet->writeStr(11, 1, L"Total:", totalLabelFormat);
sheet->writeFormula(11, 2, L"=SUM(C9:C11)", totalFormat);
sheet->writeStr(14, 2, L"Signature", signatureFormat);
sheet->setCol(1, 1, 40);
sheet->setCol(2, 2, 15);
}
Sheet* sheet2 = book->addSheet(L"Writing a rich string with multiple fonts in one cell");
if(sheet2)
{
RichString* rs = book->addRichString();
Font* font1 = rs->addFont();
font1->setColor(COLOR_RED);
font1->setSize(24);
Font* font2 = rs->addFont();
font2->setSize(24);
Font* font3 = rs->addFont();
font3->setColor(COLOR_BLUE);
font3->setSize(24);
Font* font4 = rs->addFont();
font4->setColor(COLOR_GREEN);
font4->setSize(24);
Font* font5 = rs->addFont();
font5->setScript(SCRIPT_SUPER);
font5->setSize(24);
rs->addText(L"E", font1);
rs->addText(L"=", font2);
rs->addText(L"m", font3);
rs->addText(L"c", font4);
rs->addText(L"2", font5);
sheet2->writeRichStr(5, 3, rs);
sheet2->setProtect(true);
}
Format* alFormat = book->addFormat();
alFormat->setAlignH(ALIGNH_LEFT);
Format* arFormat = book->addFormat();
arFormat->setAlignH(ALIGNH_RIGHT);
Format* alignDateFormat = book->addFormat(alFormat);
alignDateFormat->setNumFormat(NUMFORMAT_DATE);
Font* linkFont = book->addFont();
linkFont->setColor(COLOR_BLUE);
linkFont->setUnderline(UNDERLINE_SINGLE);
Format* linkFormat = book->addFormat(alFormat);
linkFormat->setFont(linkFont);
Sheet* sheet3 = book->addSheet(L"Formula");
if(sheet3)
{
sheet3->setCol(0, 0, 27);
sheet3->setCol(1, 1, 10);
sheet3->writeNum(2, 1, 40, alFormat);
sheet3->writeNum(3, 1, 30, alFormat);
sheet3->writeNum(4, 1, 50, alFormat);
sheet3->writeStr(6, 0, L"SUM(B3:B5) = L", arFormat);
sheet3->writeFormula(6, 1, L"SUM(B3:B5)", alFormat);
sheet3->writeStr(7, 0, L"AVERAGE(B3:B5) = L", arFormat);
sheet3->writeFormula(7, 1, L"AVERAGE(B3:B5)", alFormat);
sheet3->writeStr(8, 0, L"MAX(B3:B5) = L", arFormat);
sheet3->writeFormula(8, 1, L"MAX(B3:B5)", alFormat);
sheet3->writeStr(9, 0, L"MIX(B3:B5) = L", arFormat);
sheet3->writeFormula(9, 1, L"MIN(B3:B5)", alFormat);
sheet3->writeStr(10, 0, L"COUNT(B3:B5) = L", arFormat);
sheet3->writeFormula(10, 1, L"COUNT(B3:B5)", alFormat);
sheet3->writeStr(12, 0, L"IF(B7 > 100;\"large\";\"small\") = L", arFormat);
sheet3->writeFormula(12, 1, L"IF(B7 > 100;\"large\";\"small\")", alFormat);
sheet3->writeStr(14, 0, L"SQRT(25) = L", arFormat);
sheet3->writeFormula(14, 1, L"SQRT(25)", alFormat);
sheet3->writeStr(15, 0, L"RAND() = L", arFormat);
sheet3->writeFormula(15, 1, L"RAND()", alFormat);
sheet3->writeStr(16, 0, L"2*PI() = L", arFormat);
sheet3->writeFormula(16, 1, L"2*PI()", alFormat);
sheet3->writeStr(18, 0, L"UPPER(\"libxl\") = L", arFormat);
sheet3->writeFormula(18, 1, L"UPPER(\"libxl\")", alFormat);
sheet3->writeStr(19, 0, L"LEFT(\"window\";3) = L", arFormat);
sheet3->writeFormula(19, 1, L"LEFT(\"window\";3)", alFormat);
sheet3->writeStr(20, 0, L"LEN(\"string\") = L", arFormat);
sheet3->writeFormula(20, 1, L"LEN(\"string\")", alFormat);
sheet3->writeStr(22, 0, L"DATE(2010;3;11) = L", arFormat);
sheet3->writeFormula(22, 1, L"DATE(2010;3;11)", alignDateFormat);
sheet3->writeStr(23, 0, L"DAY(B23) = L", arFormat);
sheet3->writeFormula(23, 1, L"DAY(B23)", alFormat);
sheet3->writeStr(24, 0, L"MONTH(B23) = L", arFormat);
sheet3->writeFormula(24, 1, L"MONTH(B23)", alFormat);
sheet3->writeStr(25, 0, L"YEAR(B23) = L", arFormat);
sheet3->writeFormula(25, 1, L"YEAR(B23)", alFormat);
sheet3->writeStr(26, 0, L"DAYS360(B23;TODAY()) = L", arFormat);
sheet3->writeFormula(26, 1, L"DAYS360(B23;TODAY())", alFormat);
sheet3->writeStr(28, 0, L"B3+100*(2-COS(0)) = L", arFormat);
sheet3->writeFormula(28, 1, L"B3+100*(2-COS(0))", alFormat);
sheet3->writeStr(29, 0, L"ISNUMBER(B29) = L", arFormat);
sheet3->writeFormula(29, 1, L"ISNUMBER(B29)", alFormat);
sheet3->writeStr(30, 0, L"AND(1;0) = L", arFormat);
sheet3->writeFormula(30, 1, L"AND(1;0)", alFormat);
sheet3->writeStr(32, 0, L"HYPERLINK() = L", arFormat);
sheet3->writeFormula(32, 1, L"HYPERLINK(\"http://www.libxl.com\")", linkFormat);
}
Sheet* sheet4 = book->addSheet(L"aligning-colors-borders");
if(sheet4)
{
sheet4->setDisplayGridlines(false);
sheet4->setCol(1, 1, 30);
sheet4->setCol(3, 3, 11.4);
sheet4->setCol(4, 4, 2);
sheet4->setCol(5, 5, 15);
sheet4->setCol(6, 6, 2);
sheet4->setCol(7, 7, 15.4);
const wchar_t* nameAlignH[] = {L"ALIGNH_LEFT", L"ALIGNH_CENTER", L"ALIGNH_RIGHT"};
AlignH alignH[] = {ALIGNH_LEFT, ALIGNH_CENTER, ALIGNH_RIGHT};
for(int i = 0; i < sizeof(nameAlignH) / sizeof(const wchar_t*); ++i)
{
Format* format = book->addFormat();
format->setAlignH(alignH[i]);
format->setBorder();
sheet4->writeStr(i * 2 + 2, 1, n