从文档文件读取表数据 [英] Reading table data from document file
本文介绍了从文档文件读取表数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想阅读其中有一些表的MS Word文档文件.我想从这些表中读取数据.我只能读取表格的第一列.
我想阅读表中的所有列以及其他文本.
用于打开文档Word文件的代码
I want to read a MS Word document file in which we have some tables. I want to read data from these tables. I can only read the first column of the table.
I want to read all of the columns in the tables as well as other text.
The code for Code to open a doc word file
char szFilter[] = "Word Files (*.*)|*.doc|Text Files (*.txt)|*.txt|All Files (*.*)|*.*||";
CFileDialog DataRead(TRUE, // TRUE for FileOpen, FALSE for FileSaveAs
NULL, NULL,
OFN_PATHMUSTEXIST|OFN_OVERWRITEPROMPT,
szFilter,
NULL);
int nFileRead = DataRead.DoModal();
if(IDOK == nFileRead)
{
//Get file name for opening Excel file
CString szFileName = DataRead.GetPathName();
if(szFileName.IsEmpty())
return;
//Do not make Word visible
CEzWordAutomation MsWord(FALSE);
MsWord.OpenWordFile(szFileName);
int i;
int nLineCount = MsWord.GetLineCount();
CString szLine;
CString szMessage;
for(i=1; i<=nLineCount; i++)
{
szLine = MsWord.GetLine(i);
szMessage = szMessage + szLine ;
}
MsWord.CloseDocument(FALSE);
MsWord.ReleaseWord();
MessageBox(szMessage);
}
代码逐行读取doc文件
Code read doc file line by line
CString CWordAutomation::GetLine(int nLine)
{
CString szLine = _T("");
if(NULL == m_pdispWordApp)
return szLine;
VARIANTARG varg1, varg2;
int wdGoToLine = 3; //MsWord constant
int wdGoToAbsolute = 1; //MsWord constant
int wdLine = 5; //MsWord constant
int wdExtend = 1; //MsWord constant
//Got to line
ClearAllArgs();
if (!WordInvoke(m_pdispWordApp, L"Selection", &varg1, DISPATCH_PROPERTYGET, 0))
return szLine;
ClearAllArgs();
AddArgumentInt2(L"What", 0, wdGoToLine);
AddArgumentInt2(L"Which", 0, wdGoToAbsolute);
AddArgumentInt2(L"Count", 0, nLine);
if (!WordInvoke(varg1.pdispVal, L"GoTo", NULL, DISPATCH_METHOD, 0))
return szLine;
//Selection.HomeKey Unit:=wdLine
ClearAllArgs();
AddArgumentInt2(L"Unit", 0, wdLine);
if (!WordInvoke(varg1.pdispVal, L"HomeKey", NULL, DISPATCH_METHOD, 0))
return szLine;
//Selection.EndKey Unit:=wdLine, Extend:=wdExtend
ClearAllArgs();
AddArgumentInt2(L"Unit", 0, wdLine);
AddArgumentInt2(L"Extend", 0, wdExtend);
if (!WordInvoke(varg1.pdispVal, L"EndKey", &varg2, DISPATCH_METHOD, 0))
return szLine;
ClearAllArgs();
if (!WordInvoke(varg1.pdispVal, L"Text", &varg2, DISPATCH_PROPERTYGET, 0))
return szLine;
//Get text from varg2
VARTYPE Type = varg2.vt;
switch (Type)
{
case VT_UI1:
{
unsigned char nChr = varg2.bVal;
szLine = nChr;
}
break;
case VT_I4:
{
long nVal = varg2.lVal;
szLine.Format("%i", nVal);
}
break;
case VT_R4:
{
float fVal = varg2.fltVal;
szLine.Format("%f", fVal);
}
break;
case VT_R8:
{
double dVal = varg2.dblVal;
szLine.Format("%f", dVal);
}
break;
case VT_BSTR:
{
BSTR b = varg2.bstrVal;
szLine = b;
}
break;
case VT_BYREF|VT_UI1:
{
//Not tested
unsigned char* pChr = varg2.pbVal;
szLine = *pChr;
}
break;
case VT_BYREF|VT_BSTR:
{
//Not tested
BSTR* pb = varg2.pbstrVal;
szLine = *pb;
}
case 0:
{
//Empty
szLine = _T("");
}
}
return szLine;
}
与GetLine()相关的其他函数
Other function related to GetLine()
BOOL CWordAutomation::AddArgumentInt2(LPOLESTR lpszArgName, WORD wFlags, int i)
{
AddArgumentCommon(lpszArgName, wFlags, VT_I2);
m_aVargs[m_iArgCount++].iVal = i;
return TRUE;
}
void CWordAutomation::ClearAllArgs()
{
int i;
for (i = 0; i < m_iArgCount; i++)
{
if (m_awFlags[i] & DISPARG_NOFREEVARIANT)
// free the variant's contents based on type
ClearVariant(&m_aVargs[i]);
else
ReleaseVariant(&m_aVargs[i]);
}
m_iArgCount = 0;
m_iNamedArgCount = 0;
}
void CWordAutomation::ClearVariant(VARIANTARG *pvarg)
{
pvarg->vt = VT_EMPTY;
pvarg->wReserved1 = 0;
pvarg->wReserved2 = 0;
pvarg->wReserved3 = 0;
pvarg->lVal = 0;
}
请给我一个可能的解决方案.
如何阅读单词而不是一行?
谢谢.
please give me a possible solution.
How can I read a word rather than a line?
Thank you.
推荐答案
假定您的GetLine函数可以正常工作,并以CString形式从文档中返回一行.现在,您可以将此字符串拆分为单个标记以提取单个单词.
Assuming that your GetLine Functions works correctly and returns a line from the document as a CString. You can now split this string into single tokens to extract the single words.
这篇关于从文档文件读取表数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文