如何从excel中获得unicodestring

嗨,我正在使用ExcelFormat库从一个Excel文件(.xls)中提取一些内容,它包含一些Unicode字符这里是代码

BasicExcel xls(from); XLSFormatManager fmt_mgr(xls); BasicExcelWorksheet* sheet = xls.GetWorksheet(0); CellFormat fmt_general(fmt_mgr); fmt_general.set_format_string("0.000"); for (int y = 0; y<2; ++y) { for (int x = 0; x<2; ++x) { BasicExcelCell* cell = sheet->Cell(y, x); cout << sheet->Cell(y, x)->GetWString(); CellFormat fmt(fmt_mgr, cell); const Workbook::Font& font = fmt_mgr.get_font(fmt); string font_name = stringFromSmallString(font.name_); const wstring& fmt_string = fmt.get_format_string(); cell->SetFormat(fmt_general); cout << endl; } } cout << "write: " << from << endl; xls.SaveAs(to); 

此代码效果很好,它复制Excel文件正确与Unicode字符,但在将数据保存到新文件之前,我需要做一些操作,如果string不是在Unicode我可以使用这个

 sheet->Cell(y, x)->GetString(); 

它工作正常,但是当我有unicode数据我甚至不能读取它

 sheet->Cell(y, x)->GetWString(); 

因为它返回一些数字,我不能使用它,我应该如何将GetWstring的结果转换为适当的文本格式

 cout << sheet->Cell(y, x)->GetWString(); 

GetWString返回wchar_t*cout不知道该怎么做 – 你应该使用wcout来代替。

从最接近的API到我可以find:

 const wchar_t* GetWString() const Get an Unicode string. Returns 0 if cell does not contain an Unicode string. 

从文章:

http://www.codeproject.com/Articles/13852/BasicExcel-A-Class-to-Read-and-Write-to-Microsoft

在挖掘了一些其他的库之后,我发现libxl是一个很好的兼容unicode和其他语言的程序,在你下载的软件包中有一些很好的例子,如果你是这个库的新手,我build议使用libxl中的一个基本代码就像这样

 #include "libxl.h" using namespace libxl; int main() { Book* book = xlCreateBook(); // xlCreateXMLBook() for xlsx if(book) { Sheet* sheet = book->addSheet(L"Sheet1"); if(sheet) { sheet->writeStr(2, 1, L"Hello, World !"); sheet->writeNum(3, 1, 1000); } book->save(L"example.xls"); book->release(); } } 

但这个库是不是免费的,你可以parsing100个单元格或行,我认为但不是更多,它可用于Linux的Windows和Mac