大部分的代码框架是前辈们写的,稍微改改了改,姑且算是原创,请多指正。
这几天遇到一个把excel中的数据读取到一个结构体链表中,首先想到的是用标准的C++结构体格式,因为资料几乎都是基于MFC实现的,无法在win32控制台实现,无意中看到有用Qt实现的方法,最后实现了,感谢网上的各位程序猿们,下面我列出我找到的资料以及我用的Qt方法,积累有用的资料,怕以后忘了。
下面是基于MFC的资料,网址链接:
http://blog.csdn.net/fullsail/article/details/8449448
http://hi.baidu.com/dddad/item/a5eb7d080714878902ce1bb0
http://blog.csdn.net/qivan/article/details/7599924
http://blog.csdn.net/wazhl/article/details/7956646
http://zhaolihufox.blog.163.com/blog/static/264008262011124112938818/
下面是Qt的一些资料链接:
http://blog.csdn.net/ykm0722/article/details/7276752
http://blog.sina.com.cn/s/blog_4567bb800100rfsn.html
http://www.cnblogs.com/chanrom/p/3500264.html
http://www.oschina.net/code/snippet_98126_9942
http://blog.csdn.net/tingsking18/article/details/5677353
我用到的一段代码是:vs2008--Qt 4.8.6
void qex::readExcel()
{
QAxObject *excel = NULL;
QAxObject *workbooks = NULL;
QAxObject *workbook = NULL;
excel = new QAxObject("Excel.Application");
if (!excel)
{
QMessageBox::critical(this, "错误信息", "EXCEL对象丢失");
return;
}
excel->dynamicCall("SetVisible(bool)", false);
workbooks = excel->querySubObject("WorkBooks");
workbook = workbooks->querySubObject("Open(QString, QVariant)", QString(tr("e:\\test.xls")));
QAxObject * worksheet = workbook->querySubObject("WorkSheets(int)", 1);//打开第一个sheet
//QAxObject * worksheet = workbook->querySubObject("WorkSheets");//获取sheets的集合指针
//int intCount = worksheet->property("Count").toInt();//获取sheets的数量
QAxObject * usedrange = worksheet->querySubObject("UsedRange");//获取该sheet的使用范围对象
QAxObject * rows = usedrange->querySubObject("Rows");
QAxObject * columns = usedrange->querySubObject("Columns");
/*获取行数和列数*/
int intRowStart = usedrange->property("Row").toInt();
int intColStart = usedrange->property("Column").toInt();
int intCols = columns->property("Count").toInt();
int intRows = rows->property("Count").toInt();
/*获取excel内容,我的excel有209行4列,所以下面的内循环中有四段一样的代码*/
QList<mapInfo> slist;
mapPoint info;
QAxObject * cell;
for (int i = intRowStart; i < intRowStart + intRows; i++) //行
{
int j = intColStart;
while( j < intColStart + intCols )
{
cell = worksheet->querySubObject("Cells(int,int)", i, j );
info.name = cell->dynamicCall("Value2()").toString();
j++;
cell = worksheet->querySubObject("Cells(int,int)", i, j );
info.popname = cell->dynamicCall("Value2()").toString();
j++;
cell = worksheet->querySubObject("Cells(int,int)", i, j );
info.point_x = cell->dynamicCall("Value2()").toString();
j++;
cell = worksheet->querySubObject("Cells(int,int)", i, j );
info.point_y = cell->dynamicCall("Value2()").toString();
j++;
};
slist.append(info);//把所有的信息插入到slist中
// for (int j = intColStart; j < intColStart + intCols; j++) //列
// {
// QAxObject * cell = worksheet->querySubObject("Cells(int,int)", i, j ); //获取单元格
// // qDebug() << i << j << cell->property("Value"); //*****************************出问题!!!!!!
//qDebug() << i << j <<cell->dynamicCall("Value2()").toString(); //正确
// }
}
QList<mapInfo>::const_iterator riter;
riter = slist.begin();
for(; riter!=slist.end(); ++riter)
qDebug() << (*riter).name <<(*riter).popname << (*riter).point_x <<(*riter).point_y ;
workbook->dynamicCall("Close (Boolean)", false);
//同样,设置值,也用dynamimcCall("SetValue(const QVariant&)", QVariant(QString("Help!")))这样才成功的。。
//excel->dynamicCall("Quit (void)");
delete excel;//一定要记得删除,要不线程中会一直打开excel.exe
}
typedef struct mapInfo
{
QString name;
QString popname;
QString point_x;
QString point_y;
}mapPoint;
Q_DECLARE_METATYPE(mapInfo)