Qt读取word文档

为了记录一下

CWordOperate::CWordOperate( const QString filename,QObject *parent /*= NULL*/ )
: QObject(parent)
{

	m_word = new QAxWidget("Word.Application");
	//m_doc->generateDocumentation (); //导出支持的函数以及相关属性

	m_document = m_word->querySubObject("Documents");
	m_document->dynamicCall("Open(const QString&)",filename);
	m_word->setProperty("Visible",QVariant(false));   //不显示word窗口
	m_doc = m_word->querySubObject("ActiveDocument");//获取当前工作簿
	
	bool ok2 = m_word->property("Visible").toBool();
	m_word->setProperty("Visible",QVariant(false));
	
	
	//getLine(0,13); //标题
	//getLine(14,34);  //单位
	QString s = getLine(79,87);
	qDebug() << s;
	
	QString alltext = getAllText();
	
	QString teststr = QString::fromUtf8("");
	alltext = alltext.remove(teststr);
	//qDebug() << alltext;
	QStringList liststr = alltext.split("\r");
	int start = 0;
	foreach (QString sttr, liststr)
	{
		sttr = sttr.remove(teststr);
		qDebug() << sttr;
	}
	
}


void CWordOperate::readTables()
{
	if (NULL == m_doc) return;
	QAxObject* tables = m_doc->querySubObject("Tables"); //获取所有表格
	//QAxBase::PropertyBag p = m_document->propertyBag();
	int tablecount = 1;
	if (NULL != tables)
	{
		tablecount = tables->dynamicCall("Count").toInt(); //获取表格个数
		delete tables;
		tables = NULL;
	}
	for (int i = 1; i < tablecount+1; ++i)
	{
		QAxObject *table = m_doc->querySubObject("Tables(int)",i); //获取某个表格
		if (NULL ==table) continue;

		int row = table->querySubObject("Rows")->dynamicCall("Count").toInt();
		int col = table->querySubObject("Columns")->dynamicCall("Count").toInt();

		QAxBase::PropertyBag p = table->propertyBag();
		
		for (int j = 0; j < row; ++j)
		{
			for (int z = 0; z < col+1; ++z)
			{
				QAxObject *cell = table->querySubObject("Cell(int,int)",j,z); //获取表格数据
				if (NULL ==cell) continue;
				QString sp = cell->querySubObject("Range")->property("Text").toString();
				qDebug() << j << " " << z << " " << sp;
				delete cell;
				cell = NULL;
			}
			
		}
		delete table;
		table = NULL;	
	}	
}

QString CWordOperate::getAllText()
{
	QString text;
	if (NULL == m_doc) 
		return text;

	QAxObject *pRange = m_doc->querySubObject("Range()");
	
	if (NULL != pRange)
	{
		text = pRange->property("Text").toString();
		delete pRange; //要释放内存,不然文档就被占用
		pRange = NULL;
	}
	return text;

}

QString CWordOperate::getLine(int start, int end )
{
	QString text;
	if (NULL == m_doc) 
		return text;

	QVariantList params;
	params << start << end;
	QAxObject *pRange = m_doc->querySubObject("Range(QVariant&, QVariant&)",params);
	text = pRange->property("Text").toString();
	delete pRange;
	pRange = NULL;
	return text;
}

QString CWordOperate::getTableItem( int tableindex,int row,int column )
{
	QString restr;
	if (NULL == m_doc) return restr;
	//QAxObject* tables = m_doc->querySubObject("Tables"); //获取所有表格
	QAxObject *table = m_doc->querySubObject("Tables(int)",tableindex); //获取某个表格
	if (NULL ==table) return restr;

	//int row = table->querySubObject("Rows")->dynamicCall("Count").toInt();
	//int col = table->querySubObject("Columns")->dynamicCall("Count").toInt();

	QAxObject *cell = table->querySubObject("Cell(int,int)",row,column); //获取表格数据
	if (NULL ==cell) return restr;
	restr = cell->querySubObject("Range")->property("Text").toString();

	return restr;

	
}

 

  • 6
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值