C++保存数据到Excel(2)ExcelFormat库的使用

目的说明:Visual Studio2010开发C++程序,调用ExcelFormat库进行Excel的读写操作。

使用方法:

(1)将源文件中的BasicExcel.cpp、BasicExcel.hpp、ExcelFormat.cpp、ExcelFormat.h直接加入到工程中。

将上述四个文件直接加入到工程可能遇到到问题与解决方法:
可能错误一:fatal error C1010: 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include "stdafx.h

可能错误二:error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“0”不匹配值“2”

(2)ExcelFormat库的使用方法

1.调用处添加头文件和命名空间

#include "ExcelFormat.h"
using namespace ExcelFormat;

2.功能实现

下面给出一个例子:

//save data into excel file at selected position
BasicExcel xls;

// create sheet 1 and get the associated BasicExcelWorksheet pointer
xls.New(1);
BasicExcelWorksheet* sheet = xls.GetWorksheet(0);
XLSFormatManager fmt_mgr(xls);


// 设置Excel表格中的字体

ExcelFont font_bold;
font_bold._weight = FW_BOLD; // 700

CellFormat fmt_bold(fmt_mgr);
fmt_bold.set_font(font_bold);

int col, row = 0;
//写入第一行,也就是表头
for(col=0; col<10; ++col) {
	BasicExcelCell* cell = sheet->Cell(row, col);

	cell->Set("TITLE");
	cell->SetFormat(fmt_bold);
	}

//写入下面的数据
	while(++row < 4) {
	for(int col=0; col<10; ++col)
	sheet->Cell(row, col)->Set("text");
	}
	++row;

	xls.SaveAs(filePath);

效果:


更多具体功能的实现可以参考下载的src文件中的Examples.cpp文件

猜你喜欢

转载自blog.csdn.net/qq_28093585/article/details/78854556