cell轉word

cell提供的與 Excel 互轉的方法ExportExcelFile(cell-->xls),ImportExcelFile(xls-->cll),但沒有提供與word相關的方法,可是工做中有時候須要把cell轉爲word,我找到的辦法是把cll先轉爲htm,而後在由htm轉爲doc,可是這個方法能夠會丟格式(表格邊框線消失,隱藏等無效,並且內容不自適應窗口),若是你有更好的辦法,歡迎交流。ui

bool ExportCellToWord(CString &strDocFilePathName)
{
	//因爲word不處理隱藏,顯示等格式,故定義臨時cell,把須要隱藏的刪除
	CString strCellPathName = CAppUtility::GetWorkProjectPath() + _T("aaaaa") + _T(".xls");
	if (m_cell.ExportExcelFile(strCellPathName) != 1)
	{
		HHTalkBox(_T("文件保存失敗!"));
		return false;
	}

	CCell2000 *pCell = new CCell2000;
	pCell->Create(_T("臨時cell"),WS_CHILD, CRect(0,0,1,1), this,100);
	pCell->ImportExcelFile(strCellPathName, _T(""));
	pCell->DeleteRow(Row,col,0);

	long lColWidth;
	for (int nIndex = 0; nIndex < pCell->GetCols(0); ++nIndex)
	{
		lColWidth = pCell->GetColWidth(1,nIndex,0);

		pCell->SetColWidth(1,lColWidth*0.685,nIndex,0);
	}

	CString strName	= _T("建議書");
	//把cell轉爲htm
	CString strPathName = CAppUtility::GetWorkProjectPath();
	CString strHtm = strPathName + strName + _T(".htm");//htm
	if (!pCell->ExportHtmlFile(strHtm) )
	{
		HHTalkBox(_T("cell轉化htm失敗!"));
		return false;
	}

	//再把htm轉爲word
	CMsWps word;
	if (!word.OpenFile(strHtm, FALSE, (short)Word::wdOpenFormatWebPages))
	{
		HHTalkBox(_T("打開word失敗!"));
		return false;
	}

	//保存到指定路徑
	strDocFilePathName = strPathName + strName + _T(".doc");
	if (!word.SaveAs(strDocFilePathName))
	{
		strDocFilePathName = _T("");
		return false;
	}

	//刪除臨時的cell,htm,xls文件
	if (NULL != pCell)
	{
		delete pCell;
		pCell = NULL;
	}

	DeleteFile(strCellPathName);
	
	return true;
}

此處還有一個CMsWps類,.h以下this

#ifndef MSWPS_H_
#define MSWPS_H_

#include "MsWord.h"
#include "wps\CApplication.h"
#include "wps\CDocument0.h"

class CMsWps : public CMsWord
{
public:
	CMsWps(void);
public:
	~CMsWps(void);
public:
	//打開Word文件
	bool OpenFile(CString strFilepath,BOOL bIsShow, COleVariant fomart = (short)Word::wdOpenFormatAuto);

	bool SaveAs(LPCTSTR strFilePath, COleVariant fomart = (short)Word::wdFormatDocument);

	// 保存當前文檔
	bool Save();

	// 是否爲wps
	bool isWps();

	// 是否只讀
	bool isReadOnly();

	// 獲取當前文檔標題
	CString getCaption();

	//關閉文檔
	HRESULT ColseFile();

	//檢查WordApp的狀態
	HRESULT GetAppWorkStatus();

	HRESULT SetVisiable(bool bVisiable);
private:
	CApplication m_wpsApp;
	CDocument0 m_doc;
	bool m_bHasWps;
	bool m_bSucOpen;
	bool m_bUseWps;
};
#endif

.cpp實現以下:spa

#include "StdAfx.h"
#include "wps\CDocuments.h"
#include "wps\CWindow0.h"
#include "MsWps.h"
using namespace Word;


CMsWps::CMsWps(void)
: CMsWord()
, m_bHasWps(false)
, m_bSucOpen(false)
, m_bUseWps(false)
{
	
}

CMsWps::~CMsWps(void)
{
	try
	{
		m_doc.ReleaseDispatch();
		m_wpsApp.ReleaseDispatch();
	}
	catch(...)
	{
	}
}

bool CMsWps::OpenFile( CString strFilepath,BOOL bIsShow, COleVariant fomart )
{
	try
	{
		if (!m_bHasWps)
		{
			if (m_bUseWps)
				m_bHasWps = m_wpsApp.CreateDispatch(_T("KWPS.Application"));
			if (!m_bHasWps)
			{
				if (__super::OpenFile(strFilepath, bIsShow, fomart))
					return true;
				else
				{
					m_bHasWps = m_wpsApp.CreateDispatch(_T("KWPS.Application"));
					if (!m_bHasWps)
						return false;
				}
			}
		}

		m_doc.Close(COleVariant((long)0),COleVariant((long)0), COleVariant((long)0));
		m_wpsApp.put_Visible(bIsShow);

		CDocuments docs = m_wpsApp.get_Documents();

		LPDISPATCH lpd = docs.Open(
			strFilepath,			// FileName 
			True,				// Confirm ConversistrFilepathon. 
			FALSE,				// ReadOnly. 
			FALSE,				// AddToRecentFiles. 
			_T(""),			// PasswordDocument. 
			_T(""),			// PasswordTemplate. 
			FALSE,			// Revert. 
			_T(""),			// WritePasswordDocument. 
			_T(""),			// WritePasswordTemplate. 
			0,			    // Format. // Last argument for Word 97 
			0,			// Encoding 
			True,			// Visible 
			FALSE,			// OpenAndRepair 
			0,			// DocumentDirectionwdDocumentDirectionLeftToRight 
			FALSE			// NoEncodingDialog
			);
		m_bSucOpen = lpd != NULL;
		docs.ReleaseDispatch();
		if (!m_bSucOpen)
		{
			CString strMessage;

			strMessage.Format(_T("打開文檔《%s》失敗"),strFilepath.GetBuffer());

			HHTalkBox(strMessage, MB_OK|MB_ICONWARNING); 
			return  false; 
		}
		m_doc = lpd;

		return true;
	}
	catch(...)
	{
	}

	return false;
}

bool CMsWps::SaveAs( LPCTSTR strFilePath, COleVariant fomart /*= (short)Word::wdFormatDocument*/ )
{
	if (!m_bHasWps)
		return __super::SaveAs(strFilePath, fomart);

	if (!m_bSucOpen)
		return m_bSucOpen;

	try
	{
		m_doc.SaveAs(strFilePath, fomart, FALSE, _T(""), True, _T(""), False, False, False, False, False, 0, False, False, 0, False);
	}
	catch (...)
	{

	}
	return true;
}

bool CMsWps::Save()
{
	if (!m_bHasWps)
		return __super::Save();

	if (!m_bSucOpen)
		return m_bSucOpen;

	try
	{
		m_doc.Save();
		return true;
	}
	catch(...)
	{
	}

	return false;
}

HRESULT CMsWps::ColseFile()
{
	if (!m_bHasWps)
		return __super::ColseFile();

	if (!m_bSucOpen)
	{
		//m_wpsApp.Quit(COleVariant((long)0),COleVariant((long)0), COleVariant((long)0));
		return m_bSucOpen;
	}

	try
	{
		m_doc.Save();
		m_doc.Close(COleVariant((long)0),COleVariant((long)0), COleVariant((long)0));

		m_wpsApp.Quit(COleVariant((long)0),COleVariant((long)0), COleVariant((long)0));
		m_bSucOpen = false;
		m_bHasWps = false;

		return S_OK;
	}
	catch(...)
	{
	}

	return S_FALSE;
}

HRESULT CMsWps::GetAppWorkStatus()
{
	if (!m_bHasWps)
		return __super::GetAppWorkStatus();

	try
	{
		m_wpsApp.Activate();
		return S_OK;
	}
	catch(...)
	{
	}

	return S_FALSE;
}

HRESULT CMsWps::SetVisiable( bool bVisiable )
{
	if (!m_bHasWps)
		return __super::SetVisiable(bVisiable);

	try
	{
		if (bVisiable)
			m_wpsApp.put_Visible(VARIANT_TRUE);
		else
			m_wpsApp.put_Visible(VARIANT_FALSE);
		return S_OK;
	}
	catch(...)
	{
	}

	return S_FALSE;
}

bool CMsWps::isWps()
{
	return m_bHasWps;
}

CString CMsWps::getCaption()
{
	if (!m_bHasWps)
		return __super::getCaption();

	return _T("WPS 文字");
}

bool CMsWps::isReadOnly()
{
	if (!m_bHasWps)
		return __super::isReadOnly();

	try
	{
		return m_doc.get_ReadOnly();
	}
	catch(...)
	{
	}
	return false;
}
相關文章
相關標籤/搜索