BasicExcel封装了读写excel的操作(com),速度很快,不过往xls文件中写入中文会出现乱码。
在写入中文钱先将中文编码转换为unicode然后在写入xls文件可解决乱码问题
添加一个函数,所有的字符串先使用GB2312ToUnicode转换一下
然后再调用basicexcel的set函数即可
GB2312ToUnicode定义如下
void GB2312ToUnicode(CString str_,WCHAR*& pWStr_)//得到Unicode字符串
{
int count = str_.GetLength()*2+1;
if(pWStr_ == NULL)
{
pWStr_ = new WCHAR[count];
}
DWORD UnicodeSize=MultiByteToWideChar(CP_ACP,0,(LPCSTR)str_.GetBuffer(),count,pWStr_,(DWORD)count);
}
使用basicexcel操作例子代码
BasicExcelWorksheet* sheet = xls.GetWorksheet((size_t)0);
XLSFormatManager fmt_mgr(xls);
ExcelFont font_bold;
font_bold._weight = 700; // bold
CellFormat fmt_bold(fmt_mgr);
fmt_bold.set_font(font_bold);
BasicExcelCell* cell = sheet->Cell(0, 0);
WCHAR * buf=new WCHAR[1000];
GB2312ToUnicode("客户号",buf);
cell->Set(buf);
cell->SetFormat(fmt_bold);
cell = sheet->Cell(0, 1);
GB2312ToUnicode("导入结果",buf);
cell->Set(buf);
cell->SetFormat(fmt_bold);
xls.SaveAs(destxls);
delete [] buf;