Qt: Open csv files with libxl-compatible interface

739 Views Asked by At

In my app I use libxl library for read xls[x] files. Data taken from the files and shown into the table QTableWidget. But now I need to add support for read csv files. How I can do it compatible with current code (with libxl methods load(), getSheet() etc) (libxl doesn't support csv).

Book* book;
if (excelfileName.endsWith(".xlsx")) {
    book = xlCreateXMLBook();
} else if (excelfileName.endsWith(".xls")) {
    book = xlCreateBook();
} else {
    book = new CSVBook();
}

book->load(excelfileName.toLocal8Bit().data());

Which design pattern I should use for it?

1

There are 1 best solutions below

0
On BEST ANSWER

To do it I created new class csvbook

template<class TCHAR>
class csvbook: public libxl::IBookT<char>
{
    <...>
}

and redeclare all virtual methods from IBookT. At next step I wrote body for all methods of csvbook at the same file with declaration.

Object created in function

template<class TCHAR>
csvbook<TCHAR>* csvCreateBook()
{
    csvbook<TCHAR>* book;
    book = new csvbook<TCHAR>;
    return book;
}

And as result

Book* book;
if (excelfileName.endsWith(".xlsx")) {
    book = xlCreateXMLBook();
} else if (excelfileName.endsWith(".xls")) {
    book = xlCreateBook();
} else {
    book = csvCreateBook<char>();
}