Menu

updates

pogzy
2024-05-16
2024-05-27
  • pogzy

    pogzy - 2024-05-16

    Hi,

    This lib seems very promising and quite complete. I've added a little function in WorkBook.h:
    CWorksheet * GetSheetByName(std::string title)
    {
    for ( size_t i = 0; i < m_worksheets.size(); i++ )
    if ( title == m_worksheets[ i ]->GetTitle().toStdString() )
    return m_worksheets[ i ];
    }

    Without it, I didn't find any way to create a wrapper class with a constructor that build the workbook, a functions that add a sheet and another that add data.
    All provided samples are monolithic code bloc, which seems to me not enough close to the usage I plan to do.
    It could be helpful to have a set of functions to handle sheets easily.

    Regards

     
  • Alexandr Belyak

    Alexandr Belyak - 2024-05-16

    Hi,
    Thank you for your offer. Can you explain in more detail what is difficult about creating your wrapper class?
    Now you can create a worksheet using the CWorkbook::AddSheet method and get a reference to the new CWorksheet (you can get a pointer from this reference). This reference to the CWorksheet (and the pointer obtained from it) is correct until the end of the life of the CWorkbook object or the save of the workbook.
    This library was originally designed to fast save large amounts of data. Because of this, when creating a CWorksheet object, an XML file is immediately created for it with information that cannot be changed. This creates a number of restrictions on manipulations with worksheets.
    Because of this, when using the library, you need to know the structure of the workbook in advance.

     
  • pogzy

    pogzy - 2024-05-22

    Hi,

    I've tried to create a wrapper class with

    // create a sheet
    AddSheet(std::string sheetTitle);
    // add data to the created sheet
    AddData(std::string sheetTitle, std::map<std::string, std::vector\<std::string="">> data);
    // to close and save the file
    saveFile(std::string fileName);</std::string,>

    But doing this, it was impossible due to compiler error that requires some default constructor for CWorksheet. I am using VS 2022 and impossible to find any way to do this done.

    "Now you can create a worksheet using the CWorkbook::AddSheet method and get a reference to the new CWorksheet (you can get a pointer from this reference). This reference to the CWorksheet (and the pointer obtained from it) is correct until the end of the life of the CWorkbook object or the save of the workbook."
    That is what I tried to do, but the compiler did not accept this way of doing without an existing CWorksheet public default constructor. May be there is no issue with gcc.

    I did compile example samples, and they work.

     
  • Alexandr Belyak

    Alexandr Belyak - 2024-05-27

    Hi,
    I apologize for taking a long time to answer - I had problems with free time. You probably need a solution something like this:

    #include <map>
    #include <string>
    
    #include <Xlsx/Workbook.h>
    
    using namespace SimpleXlsx;
    
    class CWrapper
    {
        public:
            // create a sheet
            void AddSheet( const std::string & sheetTitle )
            {
                CWorksheet & Sheet = m_book.AddSheet( sheetTitle );
                m_Sheets[ sheetTitle ] = & Sheet;
            }
            // add data to the created sheet
            void AddData( const std::string & sheetTitle, int data )
            {
                std::map< std::string, CWorksheet * >::iterator it = m_Sheets.find( sheetTitle );
                if( it == m_Sheets.end() )  // Or assert( it != m_Sheets.end() ) if it is correct for your situation
                    return;
                it->second->BeginRow().AddCell( data ).EndRow();
            }
            // to close and save the file
            void saveFile( const std::string & fileName )
            {
                m_book.Save( fileName );
            }
    
        private:
            CWorkbook m_book;
    
            std::map< std::string, CWorksheet * > m_Sheets; // Name and pointer
    };
    
    int main()
    {
        CWrapper W;
        W.AddSheet( "Test" );
        W.AddData( "Test", 123 );
        W.saveFile( "WrapperTest.xlsx" );
        return 0;
    }
    
     

Log in to post a comment.

MongoDB Logo MongoDB