PDA

View Full Version : How can I Read Write Excel File



xingshaoyong
28th July 2007, 04:23
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 !

whitefurrows
28th July 2007, 07:58
Please do not start many posts with the same problem!

marcel
28th July 2007, 08:15
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

Mona Shokrof
3rd January 2008, 08:16
I think you should c this post

http://www.qtcentre.org/forum/f-qt-programming-2/t-read-excel-8378.html

Regards

mchara
4th January 2008, 07:30
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...


QAxObject* excel = new QAxObject( "Excel.Application", 0 );
QAxObject* workbooks = excel->querySubObject( "Workbooks" );
QAxObject* workbook = workbooks->querySubObject( "Open(const QString&)", exchange->getFilename() );
QAxObject* sheets = workbook->querySubObject( "Worksheets" );

QList<QVariantList> data; //Data list from excel, each QVariantList is worksheet row

//worksheets count
int count = sheets->dynamicCall("Count()").toInt();

for (int i=1; i<=count; i++) //cycle through sheets
{
//sheet pointer
QAxObject* sheet = sheets->querySubObject( "Item( int )", i );

QAxObject* rows = sheet->querySubObject( "Rows" );
int rowCount = rows->dynamicCall( "Count()" ).toInt(); //unfortunately, always returns 255,
// so you have to check somehow validity of cell values
QAxObject* columns = sheet->querySubObject( "Columns" );
int columnCount = columns->dynamicCall( "Count()" ).toInt(); //similarly, always returns 65535

//One of possible ways to get column count
int currentColumnCount = 0;
for (int col=1; col<columnCount; col++)
{
QAxObject* cell = sheet->querySubObject( "Cells( int, int )", COLUMN_COUNT_ROW, col );
QVariant value = cell->dynamicCall( "Value()" );
if (value.toString().isEmpty())
break;
else
currentColumnCount = col;
}
columnCount = currentColumnCount;

//sheet->dynamicCall( "Calculate()" ); //maybe somewhen it's necessary, but i've found out that cell values
// are calculated without calling this function. maybe it must be called just to recalculate

for (int row=1; row <= rowCount; row++)
{
QVariantList dataRow;
bool isEmpty = true; //when all the cells of row are empty, it means that file is at end (of course,
// it maybe not right for different excel files. it's just criteria to calculate
// somehow row count for my file)
for (int column=1; column <= columnCount; column++)
{
QAxObject* cell = sheet->querySubObject( "Cells( int, int )", row, column );
QVariant value = cell->dynamicCall( "Value()" );
if (!value.toString().isEmpty() && isEmpty)
isEmpty = false;
dataRow.append(value);
}
if (isEmpty) //criteria to get out of cycle
break;
data->append(dataRow);
}
}

workbook->dynamicCall("Close()");
excel->dynamicCall("Quit()");



Note that, end user will need excel installed to read xls files in your application...

sakthi
30th August 2008, 11:16
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

galrub
13th July 2011, 20:16
Hi all, I'm getting memory access violation when from the example above in:

QVariant value = cell->dynamicCall( "Value()" );
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.


bool QAxBase::dynamicCallHelper(const char *name, void *inout, QList<QVariant> &vars, QByteArray &type)
{
if (isNull()) { //the callstack says here is the memory access violation....
qWarning("QAxBase::dynamicCallHelper: Object is not initialized, or initialization failed");
return false;
}
......


any idea?
Thanks

hmmm tried to call QAxObject* cell = sheet->querySubObject( "Cells( int, int )", row, 0);
(column 0)... first column is 1, not 0