直接使用selectiion typetext() 方法进行写数据,通过一定的缓存方法,一次typetext()较多的数据,处理10万条数据可以在5分钟。
在4核4G 2.5GHz PC上进行测试
使用selectiion typetext() 方法,http://www.cnblogs.com/joeblackzqq/archive/2010/11/07/1871374.html
导出10万条数据用时1分钟,11000页数据,
测试发现,word会对文件大小进行优化出错,同样的内容,拷贝到txt中,大小是docx的10倍左右
在网上找的另外一种方法,可以在1分钟类写100000次数据,这种方法有点问题,不能写word2007,执行也有些奇怪。
写2万7千多页数据,数据文件大小73MB,用时2分钟
http://blog.csdn.net/yangsh3002/article/details/3547297
// testWord.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "testWord.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 唯一的应用程序对象
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// 初始化 MFC 并在失败时显示错误
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: 更改错误代码以符合您的需要
_tprintf(_T("错误: MFC 初始化失败\n"));
nRetCode = 1;
}
else
{
// TODO: 在此处为应用程序的行为编写代码。
}
// ******************* Declare Some Variables ********************
// Variables that will be used and re-used in our calls
DISPPARAMS dpNoArgs = {NULL, NULL, 0, 0};
VARIANT vResult;
OLECHAR FAR* szFunction;
BSTR bstrTemp;
// IDispatch pointers for Word's objects
IDispatch* pDispDocs; //Documents collection
IDispatch* pDispSel; //Selection object
IDispatch* pDispActiveDoc; //ActiveDocument object
// DISPID's
DISPID dispid_Docs; //Documents property of Application object
DISPID dispid_DocsAdd; //Add method of Documents collection
//object
DISPID dispid_Sel; //Selection property of Applicaiton object
DISPID dispid_TypeText; //TypeText method of Selection object
DISPID dispid_TypePara; //TypeParagraph method of Selection object
DISPID dispid_ActiveDoc; //ActiveDocument property of Application
//obj
DISPID dispid_SaveAs; //SaveAs method of the Document object
DISPID dispid_Quit; //Quit method of the Application object
// ******************** Start Automation ***********************
//Initialize the COM libraries
::CoInitialize(NULL);
// Create an instance of the Word application and obtain the pointer
// to the application's IDispatch interface.
CLSID clsid;
CLSIDFromProgID(L"Word.Application", &clsid);
IUnknown* pUnk;
HRESULT hr = ::CoCreateInstance( clsid, NULL, CLSCTX_SERVER,
IID_IUnknown, (void**) &pUnk);
IDispatch* pDispApp;
hr = pUnk->QueryInterface(IID_IDispatch, (void**)&pDispApp);
// Get IDispatch* for the Documents collection object
szFunction = OLESTR("Documents");
hr = pDispApp->GetIDsOfNames (IID_NULL, &szFunction, 1,
LOCALE_USER_DEFAULT, &dispid_Docs);
hr = pDispApp->Invoke (dispid_Docs, IID_NULL, LOCALE_USER_DEFAULT,
DISPATCH_PROPERTYGET, &dpNoArgs, &vResult,
NULL, NULL);
pDispDocs = vResult.pdispVal;
// Invoke the Add method on the Documents collection object
// to create a new document in Word
// Note that the Add method can take up to 3 arguments, all of which
// are optional. We are not passing it any so we are using an empty
// DISPPARAMS structure
szFunction = OLESTR("Add");
hr = pDispDocs->GetIDsOfNames(IID_NULL, &szFunction, 1,
LOCALE_USER_DEFAULT, &dispid_DocsAdd);
hr = pDispDocs->Invoke(dispid_DocsAdd, IID_NULL, LOCALE_USER_DEFAULT,
DISPATCH_METHOD, &dpNoArgs, &vResult, NULL,
NULL);
// Get IDispatch* for the Selection object
szFunction = OLESTR("Selection");
hr = pDispApp->GetIDsOfNames (IID_NULL, &szFunction, 1,
LOCALE_USER_DEFAULT, &dispid_Sel);
hr = pDispApp->Invoke (dispid_Sel, IID_NULL, LOCALE_USER_DEFAULT,
DISPATCH_PROPERTYGET, &dpNoArgs, &vResult,
NULL, NULL);
pDispSel = vResult.pdispVal;
// Get the DISPIDs of the TypeText and TypeParagraph methods of the
// Selection object. We'll use these DISPIDs multiple times.
szFunction = OLESTR("TypeText");
hr = pDispSel->GetIDsOfNames(IID_NULL, &szFunction, 1,
LOCALE_USER_DEFAULT, &dispid_TypeText);
szFunction = OLESTR("TypeParagraph");
hr = pDispSel->GetIDsOfNames(IID_NULL, &szFunction, 1,
LOCALE_USER_DEFAULT, &dispid_TypePara);
// The TypeText method has and requires only one argument, a string,
// so set up the DISPPARAMS accordingly
VARIANT vArgsTypeText[1];
DISPPARAMS dpTypeText;
// bstrTemp = ::SysAllocString(OLESTR("OneTwo 健康kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk\ntestkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk"));
bstrTemp = ::SysAllocString(OLESTR("Two 健康kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk\ntestkkkkkkkkkkkkkkk灌灌灌灌灌fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\n\n\n\n\n\n\n\nkkkkkkkkkkkkkkkkkkkkkk"));
vArgsTypeText [0].vt = VT_BSTR;
vArgsTypeText [0].bstrVal = bstrTemp;
dpTypeText.cArgs = 1;
dpTypeText.cNamedArgs = 0;
dpTypeText.rgvarg = vArgsTypeText;
/*
//Invoke the first TypeText and TypeParagraph pair
hr = pDispSel->Invoke (dispid_TypeText, IID_NULL,
LOCALE_USER_DEFAULT, DISPATCH_METHOD,
&dpTypeText, NULL, NULL, NULL);
hr = pDispSel->Invoke (dispid_TypePara, IID_NULL,
LOCALE_USER_DEFAULT, DISPATCH_METHOD,
&dpNoArgs, NULL, NULL, NULL);
::SysFreeString(bstrTemp);
//Invoke the second TypeText and TypeParagraph pair
bstrTemp = ::SysAllocString(OLESTR("Two 健康kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk\ntestkkkkkkkkkkkkkkk灌灌灌灌灌fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\n\n\n\n\n\n\n\nkkkkkkkkkkkkkkkkkkkkkk"));
hr = pDispSel->Invoke (dispid_TypeText, IID_NULL,
LOCALE_USER_DEFAULT, DISPATCH_METHOD,
&dpTypeText, NULL, NULL, NULL);
hr = pDispSel->Invoke (dispid_TypePara, IID_NULL,
LOCALE_USER_DEFAULT, DISPATCH_METHOD,
&dpNoArgs, NULL, NULL, NULL);
::SysFreeString(bstrTemp);
*/
//Invoke the third TypeText and TypeParagraph pair
bstrTemp = ::SysAllocString(OLESTR("Threesdjjjjjjjjjjjjjjjjjjj\n\n\njjjjkkkkkkkkka\nkkkkkkkkkkkkkk\ndddddddddd\n\n\ndddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddkkkkkkkkkkkkkk"));
for (int i = 0; i < 100000; i++)
{
hr = pDispSel->Invoke (dispid_TypeText, IID_NULL,
LOCALE_USER_DEFAULT, DISPATCH_METHOD,
&dpTypeText, NULL, NULL, NULL);
hr = pDispSel->Invoke (dispid_TypePara, IID_NULL,
LOCALE_USER_DEFAULT, DISPATCH_METHOD,
&dpNoArgs, NULL, NULL, NULL);
}
::SysFreeString(bstrTemp);
// Get IDispatch* for the ActiveDocument object
szFunction = OLESTR("ActiveDocument");
hr = pDispApp->GetIDsOfNames (IID_NULL, &szFunction, 1,
LOCALE_USER_DEFAULT,
&dispid_ActiveDoc);
hr = pDispApp->Invoke (dispid_ActiveDoc, IID_NULL,
LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET,
&dpNoArgs, &vResult, NULL, NULL);
pDispActiveDoc = vResult.pdispVal;
//Set up the DISPPARAMS for the SaveAs method (11 arguments)
VARIANT vArgsSaveAs[11];
DISPPARAMS dpSaveAs;
dpSaveAs.cArgs = 11;
dpSaveAs.cNamedArgs = 0;
dpSaveAs.rgvarg = vArgsSaveAs;
BSTR bstrEmptyString;
bstrEmptyString = ::SysAllocString(OLESTR(""));
VARIANT vFalse;
vFalse.vt = VT_BOOL;
vFalse.boolVal = FALSE;
//TRY THIS:
//To see the error handler in action, change the following
//line to:
//
// bstrTemp = ::SysAllocString(OLESTR("c://badpath//doc1.doc"));
bstrTemp = ::SysAllocString(OLESTR("D:\\code\\data\\test.doc"));
//TRY THIS:
//To see the error handler in action, change the following
//line to:
//
// vArgsSaveAs[10].vt = VT_I4;
vArgsSaveAs[10].vt = VT_BSTR;
vArgsSaveAs[10].bstrVal = bstrTemp; //Filename
vArgsSaveAs[9].vt = VT_I4;
vArgsSaveAs[9].lVal = 0; //FileFormat
vArgsSaveAs[8] = vFalse; //LockComments
vArgsSaveAs[7].vt = VT_BSTR;
vArgsSaveAs[7].bstrVal = bstrEmptyString; //Password
vArgsSaveAs[6].vt = VT_BOOL;
vArgsSaveAs[6].boolVal = TRUE; //AddToRecentFiles
vArgsSaveAs[5].vt = VT_BSTR;
vArgsSaveAs[5].bstrVal = bstrEmptyString; //WritePassword
vArgsSaveAs[4] = vFalse; //ReadOnlyRecommended
vArgsSaveAs[3] = vFalse; //EmbedTrueTypeFonts
vArgsSaveAs[2] = vFalse; //SaveNativePictureFormat
vArgsSaveAs[1] = vFalse; //SaveFormsData
vArgsSaveAs[0] = vFalse; //SaveAsOCELetter
//Invoke the SaveAs method
szFunction = OLESTR("SaveAs");
hr = pDispActiveDoc->GetIDsOfNames(IID_NULL, &szFunction, 1,
LOCALE_USER_DEFAULT, &dispid_SaveAs);
EXCEPINFO excep;
hr = pDispActiveDoc->Invoke(dispid_SaveAs, IID_NULL,
LOCALE_USER_DEFAULT, DISPATCH_METHOD,
&dpSaveAs, NULL, &excep, NULL);
if (FAILED(hr))
{
//ErrHandler(hr, excep);
}
::SysFreeString(bstrEmptyString);
//Invoke the Quit method
szFunction = OLESTR("Quit");
hr = pDispApp->GetIDsOfNames(IID_NULL, &szFunction, 1,
LOCALE_USER_DEFAULT, &dispid_Quit);
hr = pDispApp->Invoke (dispid_Quit, IID_NULL, LOCALE_USER_DEFAULT,
DISPATCH_METHOD, &dpNoArgs, NULL, NULL, NULL);
//Clean-up
::SysFreeString(bstrTemp);
pDispActiveDoc->Release();
pDispSel->Release();
pDispDocs->Release();
pDispApp->Release();
pUnk->Release();
::CoUninitialize();
return nRetCode;
}