C++ libxl庫讀寫Excel

#include<iostream>
#include<stdio.h>
#include <windows.h>
#include "libxl.h"
using namespace std;
using namespace libxl;

#pragma comment(lib,"libxl.lib")


int main()
{


	Book* book = xlCreateBook();
	if (book)
	{
		Sheet* sheet = book->addSheet("Sheet1");
		if (sheet)
		{
			sheet->writeStr(2, 1, "Hello, World !");
			sheet->writeNum(4, 1, 1000);
			sheet->writeNum(5, 1, 2000);

			Font* font = book->addFont();
			font->setColor(COLOR_RED);
			font->setBold(true);
			Format* boldFormat = book->addFormat();
			boldFormat->setFont(font);
			sheet->writeFormula(6, 1, "SUM(B5:B6)", boldFormat);

			Format* dateFormat = book->addFormat();
			dateFormat->setNumFormat(NUMFORMAT_DATE);
			sheet->writeNum(8, 1, book->datePack(2008, 4, 29), dateFormat);

			sheet->setCol(1, 1, 12);
		}

		if (book->save("example.xls"))
		{
			//::ShellExecute(NULL, "open", "example.xls", NULL, NULL, SW_SHOW);
		}
		else
		{
			std::cout << book->errorMessage() << std::endl;
		}
	}


	book->release();

	Book* bookr = xlCreateBook();




	if (bookr->load("example.xls"))
	{
		Sheet *sheet = bookr->getSheet(0);
		if (sheet) {

			cout << sheet->readNum(4, 1) << endl;
			cout << sheet->readNum(5, 1) << endl;
			cout << sheet->readNum(6, 1) << endl;//不支持計算所以爲0
		}
	}
	bookr->release();


	system("pause");

	return 0;

}

 

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