Results 1 to 7 of 7

Thread: How can I Read Write Excel File

  1. #1
    Join Date
    Nov 2006
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Question How can I Read Write Excel File

    Hi all,
    I had Create Excel, add workbook und sheet .
    How can I Read Write Excel File'row/line using Qt? can you give me an example!
    thanks !

  2. #2
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy Re: How can I Read Write Excel File

    Please do not start many posts with the same problem!

  3. #3
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How can I Read Write Excel File

    I don't think anyone has done this, but there are some links regarding excel file formats:
    http://sc.openoffice.org/excelfileformat.pdf
    http://chicago.sourceforge.net/devel/docs/excel/
    http://en.wikipedia.org/wiki/Microsoft_Excel

    You'll have to implement them. Or steal the code from OpenOffice?

    Regards

  4. #4
    Join Date
    May 2007
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How can I Read Write Excel File

    I think you should c this post

    http://www.qtcentre.org/forum/f-qt-p...xcel-8378.html

    Regards

  5. #5
    Join Date
    Sep 2007
    Location
    Szczecin, Poland
    Posts
    153
    Thanks
    7
    Thanked 11 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How can I Read Write Excel File

    Hi,

    Once there was a thread on this forum that described Excel reading via QAxObject in details,
    but i can't find it anymore.

    Anyway... i've copied example(a conclusion of whole thread) to a local file(just in case) so i should give it back to forum

    so...
    Qt Code:
    1. QAxObject* excel = new QAxObject( "Excel.Application", 0 );
    2. QAxObject* workbooks = excel->querySubObject( "Workbooks" );
    3. QAxObject* workbook = workbooks->querySubObject( "Open(const QString&)", exchange->getFilename() );
    4. QAxObject* sheets = workbook->querySubObject( "Worksheets" );
    5.  
    6. QList<QVariantList> data; //Data list from excel, each QVariantList is worksheet row
    7.  
    8. //worksheets count
    9. int count = sheets->dynamicCall("Count()").toInt();
    10.  
    11. for (int i=1; i<=count; i++) //cycle through sheets
    12. {
    13. //sheet pointer
    14. QAxObject* sheet = sheets->querySubObject( "Item( int )", i );
    15.  
    16. QAxObject* rows = sheet->querySubObject( "Rows" );
    17. int rowCount = rows->dynamicCall( "Count()" ).toInt(); //unfortunately, always returns 255,
    18. // so you have to check somehow validity of cell values
    19. QAxObject* columns = sheet->querySubObject( "Columns" );
    20. int columnCount = columns->dynamicCall( "Count()" ).toInt(); //similarly, always returns 65535
    21.  
    22. //One of possible ways to get column count
    23. int currentColumnCount = 0;
    24. for (int col=1; col<columnCount; col++)
    25. {
    26. QAxObject* cell = sheet->querySubObject( "Cells( int, int )", COLUMN_COUNT_ROW, col );
    27. QVariant value = cell->dynamicCall( "Value()" );
    28. if (value.toString().isEmpty())
    29. break;
    30. else
    31. currentColumnCount = col;
    32. }
    33. columnCount = currentColumnCount;
    34.  
    35. //sheet->dynamicCall( "Calculate()" ); //maybe somewhen it's necessary, but i've found out that cell values
    36. // are calculated without calling this function. maybe it must be called just to recalculate
    37.  
    38. for (int row=1; row <= rowCount; row++)
    39. {
    40. QVariantList dataRow;
    41. bool isEmpty = true; //when all the cells of row are empty, it means that file is at end (of course,
    42. // it maybe not right for different excel files. it's just criteria to calculate
    43. // somehow row count for my file)
    44. for (int column=1; column <= columnCount; column++)
    45. {
    46. QAxObject* cell = sheet->querySubObject( "Cells( int, int )", row, column );
    47. QVariant value = cell->dynamicCall( "Value()" );
    48. if (!value.toString().isEmpty() && isEmpty)
    49. isEmpty = false;
    50. dataRow.append(value);
    51. }
    52. if (isEmpty) //criteria to get out of cycle
    53. break;
    54. data->append(dataRow);
    55. }
    56. }
    57.  
    58. workbook->dynamicCall("Close()");
    59. excel->dynamicCall("Quit()");
    To copy to clipboard, switch view to plain text mode 

    Note that, end user will need excel installed to read xls files in your application...
    Last edited by jacek; 4th January 2008 at 17:05. Reason: wrapped too long lines
    See GrEEn (Graphics Effects Environment)
    http://sourceforge.net/project/platf...roup_id=232746
    a qt-based plugins oriented MDI image processing application(contains also qt plugins like styles & imageformats).

  6. The following 3 users say thank you to mchara for this useful post:

    amoswood (14th May 2009), hypnotic401 (13th July 2011), jacek (4th January 2008)

  7. #6
    Join Date
    Nov 2007
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How can I Read Write Excel File

    hi friends,
    Does any one have a sample project to show how to write and read a excel file.
    Or is there any other method other than the one below mentioned.
    Thanks in advance
    regards,
    Sakthi

  8. #7
    Join Date
    May 2011
    Posts
    11
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Question Re: How can I Read Write Excel File - Solved

    Hi all, I'm getting memory access violation when from the example above in:
    Qt Code:
    1. QVariant value = cell->dynamicCall( "Value()" );
    To copy to clipboard, switch view to plain text mode 
    I have compiled qt with code generation /MTd (multithreaded debug) and not /MDd (multithreaded debug dll) but the libs are static linked, so, no Qt DLL.
    Qt Code:
    1. bool QAxBase::dynamicCallHelper(const char *name, void *inout, QList<QVariant> &vars, QByteArray &type)
    2. {
    3. if (isNull()) { //the callstack says here is the memory access violation....
    4. qWarning("QAxBase::dynamicCallHelper: Object is not initialized, or initialization failed");
    5. return false;
    6. }
    7. ......
    To copy to clipboard, switch view to plain text mode 

    any idea?
    Thanks

    hmmm tried to call QAxObject* cell = sheet->querySubObject( "Cells( int, int )", row, 0);
    (column 0)... first column is 1, not 0
    Last edited by galrub; 13th July 2011 at 21:27.

Similar Threads

  1. Read \Write Excel File using Qt
    By xingshaoyong in forum Qt Programming
    Replies: 4
    Last Post: 27th July 2007, 23:07
  2. FSWriteFork in MAC OS to write data to a file.
    By vishal.chauhan in forum General Programming
    Replies: 5
    Last Post: 2nd July 2007, 07:48
  3. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 16:21
  4. who can give me a tutorials of write .pro file
    By fengtian.we in forum Qt Programming
    Replies: 5
    Last Post: 20th May 2007, 17:25
  5. Replies: 13
    Last Post: 1st June 2006, 15:01

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.