没有合适的资源?快使用搜索试试~ 我知道了~
ExcelFormat Library.docx
需积分: 0 4 下载量 187 浏览量
2023-03-29
15:28:47
上传
评论
收藏 92KB DOCX 举报
温馨提示
试读
14页
ExcelFormat Library.docx
资源推荐
资源详情
资源评论
ExcelFormat Library
By Martin Fuchs, 3 Feb 2011
Introduction
There has been the C++ project BasicExcel for quite some years now. It is very useful to
read and write Excel sheets in.xls format. However, it contains only very basic functionality.
There are missing features like text formatting using fonts, and display formats and
formulas are not yet supported. This article covers this additional functionality. The new,
derived project ExcelFormat is based on the old BasicExcel code, and just adds what
I was missing for my own needs. If you want to see where I am using the new library, you
can look at the Export functions of Service Managerand Uninstall Manager.
XLS Format
This library processes Excel files in the BIFF8 XLS file format. For a detailed documentation
of the format and all its internals, there are two documents available:
� OpenOffice provides OpenOffice.org's Documentation of the Microsoft Excel File Format.
� Microsoft published the Microsoft Office Binary File Formats a few years later.
Compatibility
You can use the code on MS Windows with the MSVC compilers of VC++ 6.0 SP3 or later.
Using GCC on MacOSX or Linux is also supported, but with some limitations. May be it
even works in other environments like Sun Solaris, but this is not yet tested. Here you can
see a table of environments with their respective status:
small XLS files
larger XLS files
WIN32, using Windows API IStorage
OK
OK
WIN64, using Windows API IStorage
OK
OK
MacOS X / Linux 32 bit, using BasicExcel CompoundFile implementation
OK
issues
MacOS X / Linux 64 bit, using BasicExcel CompoundFile implementation
OK
issues
The issues listed above are because of the incomplete CompoundFile implementation of
the underlying BasicExcellibrary. I already fixed some part of the BasicExcel code
regarding numbers in RKValue format and 64 bit compatibility, but there are still issues
that show up when reading or writing larger XLS files which use XBAT or SBAT entries.
Using the Code
To use the new formatting functionality, first create an XLSFormatManager object, like in
the example1() function, and attach it to an existing BasicExcel object:
Collapse | Copy Code
void example1(const char* path)
{
BasicExcel xls;
// create sheet 1 and get the associated BasicExcelWorksheet pointer
xls.New(1);
BasicExcelWorksheet* sheet = xls.GetWorksheet(0);
XLSFormatManager fmt_mgr(xls);
You can find all the examples of this article in the source code file Examples.cpp.
To define a custom font, create an ExcelFont object and set any needed properties, for
example, the font weight for a bold font:
Collapse | Copy Code
ExcelFont font_bold;
font_bold._weight = FW_BOLD; // 700=bold, 400=normal
The format of an Excel cell can be defined by a CellFormat object, which holds the
chosen font and some more properties:
Collapse | Copy Code
CellFormat fmt_bold(fmt_mgr);
fmt_bold.set_font(font_bold);
After you have prepared the CellFormat, you can choose the font and display settings
of Excel cells by callingSetFormat():
Collapse | Copy Code
// Create a table containing a header row in bold and four rows below.
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;
Text color is specified by setting color indices in ExcelFont, for example:
Collapse | Copy Code
ExcelFont font_red_bold;
font_red_bold._weight = FW_BOLD;
font_red_bold._color_index = EGA_RED;
CellFormat fmt_red_bold(fmt_mgr, font_red_bold);
fmt_red_bold.set_color1(COLOR1_PAT_SOLID); // solid background
fmt_red_bold.set_color2(MAKE_COLOR2(EGA_BLUE,0)); // blue background
CellFormat fmt_green(fmt_mgr, ExcelFont().set_color_index(EGA_GREEN));
for(col=0; col<10; ++col) {
BasicExcelCell* cell = sheet->Cell(row, col);
cell->Set("xxx");
cell->SetFormat(fmt_red_bold);
剩余13页未读,继续阅读
资源评论
老鬼城
- 粉丝: 2
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功