版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chyuanrufeng/article/details/81157908
为了记录一下
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;
}