返回首页

我想读一个MS Word文档的文件中,我们有一些表。我想从这些表中读取数据。我只能读表的第一列。

我想读的所有表中的列以及其他文字。

我到MFC。

- 感谢

doc文件读的代码是:

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

}

 

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;

 

}

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;

 

}

请给我一个可行的解决方案....我怎么能读一个字,而不是一条线。
谢谢:8172875 |会员

回答

评论会员: 时间:2
C