libxl创建实例

来源:互联网 发布:剑三妖孽成男捏脸数据 编辑:程序博客网 时间:2024/04/29 03:18
//.cpp 头文件
//20170612 LuckyRen#include <iostream>#include <windows.h>#include "libxl.h"#include <map>#include "string"#pragma comment(lib,"libxl.lib")using namespace libxl;
//测试按钮void CLibxlTestDlg::OnBnClickedBtnT(){//源文件路径CString tPath = _T("C:\\Users\\Desktop\\TT\\正式.xls");//目标文件路径CString tPath1 = _T("C:\\Users\\Desktop\\TT\\正式1.xls");//向//源文件路径CString ttPath = _T("C:\\Users\\Desktop\\TT\\正式2.xls");//保存路径CString SavePath = _T("C:\\Users\\Desktop\\TT\\out.xls");//sheet 名称CopySheet(tPath, tPath1, SavePath);CopySheet(ttPath, SavePath, SavePath);//CopySheet(ttPath, tPath1, SavePath);//打开Book::ShellExecute(NULL, L"open", SavePath, NULL, NULL, SW_SHOW); return ;}

/*函数名称:CopySheet()创建日期:20170622 功能描述:复制一个Book中的sheet内容到另一个Book中的sheet表中,格式内容不改变参数描述:srcPath:源Book路径     dstPath:目标Book路径   savePath:保存路径*/void CLibxlTestDlg ::CopySheet(CString srcPath, CString dstPath, CString savePath){//创建源文件booklibxl::Book* srcBook = xlCreateBook();//创建目标booklibxl::Book* dstBook = xlCreateBook();//加载源BooksrcBook->load(srcPath);//获取源book中第一页sheet表Sheet* srcSheet = srcBook->getSheet(0);//获取源sheet表名称CString SheetName = srcSheet->name();//加载目标bookdstBook->load(dstPath);//获取目标book中sheet表总个数int Index = dstBook->sheetCount();CString Name[6];for(int i=0; i < Index; i++){//获取目标sheet表名称Sheet *dstSheet = dstBook->getSheet(i);//获取目标sheet表名称CString sheetname = dstSheet->name();Name[i] = dstSheet->name(); }//向目标book中已有sheet表的下一个位置添加一个新的sheet表//Sheet *dstSheet = dstBook->insertSheet(Index, SheetName);//添加一个新的sheet表Sheet* dstSheet = dstBook->addSheet(SheetName);for(int col = srcSheet->firstCol(); col < srcSheet->lastCol(); ++col){//设置新Sheet表列宽格式dstSheet->setCol(col, col, srcSheet->colWidth(col), 0, srcSheet->colHidden(col));}std::map<Format*, Format*> formats;//复制格式Format *srcFormat, *dstFormat;int rowFirst = 0; //第一行int rowLast = 0;  //最后一行int colFirst = 0; //第一列int colLast = 0;  //最后一列for(int row = srcSheet->firstRow(); row < srcSheet->lastRow(); ++row){//源sheet表单元格第一行 到单元格最后一行//设置目标Sheet表行高度格式dstSheet->setRow(row, srcSheet->rowHeight(row), 0, srcSheet->rowHidden(row));//源Sheet表单元格第一列 到单元格最后一列for(int col = srcSheet->firstCol(); col < srcSheet->lastCol(); ++col){// 复制合并块//判断指定的单元是否在合并区域if(srcSheet->getMerge(row, col, &rowFirst, &rowLast, &colFirst, &colLast)){              dstSheet->setMerge(rowFirst, rowLast, colFirst, colLast);              }srcFormat = srcSheet->cellFormat(row, col);if(!srcFormat) continue;////检查格式if(formats.count(srcFormat) == 0){                    //没有找到格式,在输出文件中创建它dstFormat = dstBook->addFormat(srcFormat);int count = dstBook->formatSize();formats[srcFormat] = dstFormat;}else{// 格式已经创建dstFormat = formats[srcFormat];}////复制单元格的值CellType ct = srcSheet->cellType(row, col);switch(ct){case CELLTYPE_NUMBER:{double value = srcSheet->readNum(row, col, &srcFormat);                         dstSheet->writeNum(row, col, value, dstFormat);break;}case CELLTYPE_BOOLEAN:{bool value = srcSheet->readBool(row, col, &srcFormat);dstSheet->writeBool(row, col, value, dstFormat);break;}case CELLTYPE_STRING:{const wchar_t* s = srcSheet->readStr(row, col, &srcFormat);                dstSheet->writeStr(row, col, s, dstFormat);break;}case CELLTYPE_BLANK:                    {srcSheet->readBlank(row, col, &srcFormat);dstSheet->writeBlank(row, col, dstFormat);            break;}case CELLTYPE_EMPTY:{break;}}}}formats.clear();//保存路径dstBook->save(savePath);dstBook->release();srcBook->release();return;}


原创粉丝点击