1. 程式人生 > >Qt讀取word文件

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;

	
}