您的位置:首页 > 编程语言 > Qt开发

qt 读excel的一些问题

2013-06-14 09:01 330 查看
网上看到了这些代码

QApplication a(argc,argv);

QAxObject* excel = new QAxObject("Excel.Application");

excel->setProperty("Visible", false);

QAxObject* workbooks = excel->querySubObject("Workbooks");

workbooks->dynamicCall("Open (const QString&)", QString("c:\\a.xls")); //filename

QAxObject* workbook = excel->querySubObject("ActiveWorkBook");

QAxObject* worksheets = workbook->querySubObject("WorkSheets");

QAxObject* worksheet = workbook->querySubObject("Worksheets(int)", 1); //worksheet number

QAxObject* usedrange = worksheet->querySubObject("UsedRange");

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();

QAxObject * cell;

for (int i = intRowStart; i < intRowStart + intRows; i++)

{

for (int j = intColStart; j < intColStart + intCols; j++)

{

cell = excel->querySubObject("Cells(Int, Int)", i, j );

QVariant cellValue = cell->dynamicCall("value"); //这里是关键 要不读不出来
// cell->dynamicCall("SetValue2(const QVariant&)",3);//设置单元格的值 这样可以写进去

}

}

excel->setProperty("DisplayAlerts", 0);

workbook->dynamicCall("Save(void)");

workbook->dynamicCall("Close (Boolean)", false);

excel->setProperty("DisplayAlerts",1);

delete excel;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: