txt内容通过另存为方式导入到word中
来源:互联网 发布:mac桌面隐藏硬盘 编辑:程序博客网 时间:2024/05/16 12:11
之前的程序可以把word内容通过另存为方式导入到txt中
直接使用这个程序,txt -> doc ,也可以导入到doc文件中,但是txt->docx , 导入到word2007就有问题
这时,改变保存参数设置就可以
vArgsSaveAs[10].vt = VT_BSTR;
vArgsSaveAs[10].bstrVal = bstrSaveFile; //Filename
vArgsSaveAs[9].vt = VT_I4;
vArgsSaveAs[9].lVal = 16; //FileFormat(wdFormatText = 2)
要设置好这个vArgsSaveAs[9].lVal = 16;
性能:初步测试,转换处理10万行,大小15MB的txt文件约用时20秒,和机器配置有关
需要注意的是字符编码,采用这个程序来处理ANSI编码的txt,中文会出现乱码,使用UTF-8编码的没有问题
另外,新生成的word使用的是默认模板,如果需要使用特定的模板可以到相应的路径下修改默认模板
附:完整程序
#include <ole2.h>#include <stdio.h>void DocToTxt(BSTR bstrOpenFile, BSTR bstrSaveFile);void DocToTxt(BSTR bstrOpenFile, BSTR bstrSaveFile){// ******************* Declare Some Variables ********************// Variables that will be used and re-used in our callsDISPPARAMS m_dpNoArgs = {NULL, NULL, 0, 0};VARIANT m_vResult;OLECHAR FAR* m_szFunction;// IDispatch pointers for Word's objectsIDispatch* m_pDispDocs; //Documents collectionIDispatch* m_pDispActiveDoc; //ActiveDocument object// DISPID'sDISPID m_dispid_Docs; //Documents property of Application objectDISPID m_dispid_ActiveDoc; //ActiveDocument property of ApplicationDISPID m_dispid_SaveAs; //SaveAs method of the Document objectDISPID m_dispid_Quit; //Quit method of the Application objectDISPID m_dispid_Open; //Open method of the Application objectBSTR m_bstrEmptyString ;// ******************** 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 m_clsid;CLSIDFromProgID(L"Word.Application", &m_clsid);IUnknown* m_pUnk;HRESULT m_hr = ::CoCreateInstance( m_clsid, NULL, CLSCTX_SERVER,IID_IUnknown, (void**) &m_pUnk);IDispatch* m_pDispApp;m_hr = m_pUnk->QueryInterface( IID_IDispatch, (void**)&m_pDispApp);// Get IDispatch* for the Documents collection objectm_szFunction = OLESTR("Documents");m_hr = m_pDispApp->GetIDsOfNames (IID_NULL, &m_szFunction, 1,LOCALE_USER_DEFAULT, &m_dispid_Docs);m_hr = m_pDispApp->Invoke (m_dispid_Docs, IID_NULL, LOCALE_USER_DEFAULT,DISPATCH_PROPERTYGET, &m_dpNoArgs, &m_vResult,NULL, NULL);m_pDispDocs = m_vResult.pdispVal;// Query id of method openm_szFunction = OLESTR("Open");m_hr = m_pDispDocs->GetIDsOfNames(IID_NULL, &m_szFunction,1,LOCALE_USER_DEFAULT, &m_dispid_Open);// Prepare parameters for open methodVARIANT vArgsOpen[6];DISPPARAMS dpOpen;dpOpen.cArgs = 6;dpOpen.cNamedArgs = 0;dpOpen.rgvarg = vArgsOpen;VARIANT vFalse, vTrue;vFalse.vt = VT_BOOL;vFalse.boolVal = FALSE;vTrue.vt = VT_BOOL;vTrue.boolVal = TRUE;m_bstrEmptyString = ::SysAllocString(OLESTR("")) ;vArgsOpen[5].vt = VT_BSTR;vArgsOpen[5].bstrVal = bstrOpenFile;vArgsOpen[4] = vFalse;vArgsOpen[3] = vTrue;vArgsOpen[2] = vFalse;vArgsOpen[1].vt = VT_BSTR;vArgsOpen[1].bstrVal = m_bstrEmptyString;vArgsOpen[0].vt = VT_BSTR;vArgsOpen[0].bstrVal = m_bstrEmptyString;//Execute open methodm_hr=m_pDispDocs->Invoke(m_dispid_Open,IID_NULL,LOCALE_USER_DEFAULT,DISPATCH_METHOD,&dpOpen,NULL,NULL,NULL);//Query activedocument interfacem_szFunction = OLESTR("ActiveDocument");m_hr = m_pDispApp->GetIDsOfNames(IID_NULL, &m_szFunction, 1,LOCALE_USER_DEFAULT,&m_dispid_ActiveDoc);m_hr = m_pDispApp->Invoke(m_dispid_ActiveDoc,IID_NULL,LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET,&m_dpNoArgs,&m_vResult,NULL,NULL);m_pDispActiveDoc = m_vResult.pdispVal;//Prepare arguments for save as .txtVARIANT vArgsSaveAs[11];DISPPARAMS dpSaveAs;dpSaveAs.cArgs = 11;dpSaveAs.cNamedArgs = 0;dpSaveAs.rgvarg = vArgsSaveAs;//wdFormatText 2, //wdFormatDocument 0 Microsoft Office Word formatvArgsSaveAs[10].vt = VT_BSTR;vArgsSaveAs[10].bstrVal = bstrSaveFile; //FilenamevArgsSaveAs[9].vt = VT_I4;vArgsSaveAs[9].lVal = 16; //FileFormat(wdFormatText = 2)vArgsSaveAs[8] = vFalse; //LockCommentsvArgsSaveAs[7].vt = VT_BSTR;vArgsSaveAs[7].bstrVal = m_bstrEmptyString; //PasswordvArgsSaveAs[6].vt = VT_BOOL;vArgsSaveAs[6].boolVal = TRUE; //AddToRecentFilesvArgsSaveAs[5].vt = VT_BSTR;vArgsSaveAs[5].bstrVal = m_bstrEmptyString; //WritePasswordvArgsSaveAs[4] = vFalse; //ReadOnlyRecommendedvArgsSaveAs[3] = vFalse; //EmbedTrueTypeFontsvArgsSaveAs[2] = vFalse; //SaveNativePictureFormatvArgsSaveAs[1] = vFalse; //SaveFormsDatavArgsSaveAs[0] = vFalse; //SaveAsOCELetter// Query and execute SaveAs methodm_szFunction = OLESTR("SaveAs");m_hr = m_pDispActiveDoc->GetIDsOfNames(IID_NULL,&m_szFunction,1,LOCALE_USER_DEFAULT,&m_dispid_SaveAs);m_hr = m_pDispActiveDoc->Invoke(m_dispid_SaveAs, IID_NULL,LOCALE_USER_DEFAULT,DISPATCH_METHOD,&dpSaveAs,NULL,NULL,NULL);// Invoke the Quit methodm_szFunction = OLESTR("Quit");m_hr = m_pDispApp->GetIDsOfNames(IID_NULL, &m_szFunction, 1,LOCALE_USER_DEFAULT, &m_dispid_Quit);m_hr = m_pDispApp->Invoke(m_dispid_Quit, IID_NULL, LOCALE_USER_DEFAULT,DISPATCH_METHOD, &m_dpNoArgs, NULL, NULL, NULL);//Clean-up::SysFreeString(bstrOpenFile) ;::SysFreeString(bstrSaveFile) ;::SysFreeString(m_bstrEmptyString) ;m_pDispActiveDoc->Release();m_pDispDocs->Release();m_pDispApp->Release();m_pUnk->Release();::CoUninitialize();}int main(int argc, char* argv[]){BSTR str1,str2 ;str1 = ::SysAllocString(OLESTR("D:\\code\\data\\cvUTF.txt"));str2 = ::SysAllocString(OLESTR("D:\\code\\data\\cvtxt1113.docx"));DocToTxt(str1,str2);//DocToTxt(::SysAllocString(OLESTR("D:\\code\\data\\cvtxt.doc")), ::SysAllocString(OLESTR("D:\\code\\data\\cvtxt22.docx")));return 0;}
参数意义见下面定义
http://blog.csdn.net/youthon/article/details/7019388
以下为文件格式的定义
wdFormatDocument0Microsoft Office Word format.wdFormatDOSText4Microsoft DOS text format.wdFormatDOSTextLineBreaks5Microsoft DOS text with line breaks preserved.wdFormatEncodedText7Encoded text format.wdFormatFilteredHTML10Filtered HTML format.wdFormatHTML8Standard HTML format.wdFormatRTF6Rich text format (RTF).wdFormatTemplate1Word template format.wdFormatText2Microsoft Windows text format.wdFormatTextLineBreaks3Windows text format with line breaks preserved.wdFormatUnicodeText7Unicode text format.wdFormatWebArchive9Web archive format.wdFormatXML11Extensible Markup Language (XML) format.wdFormatDocument970Microsoft Word 97 document format.wdFormatDocumentDefault16Word default document file format. For Microsoft Office Word 2007, this is the DOCX format.wdFormatPDF17PDF format.wdFormatTemplate971Word 97 template format.wdFormatXMLDocument12XML document format.wdFormatXMLDocumentMacroEnabled13XML document format with macros enabled.wdFormatXMLTemplate14XML template format.wdFormatXMLTemplateMacroEnabled15XML template format with macros enabled.wdFormatXPS18XPS format.
- txt内容通过另存为方式导入到word中
- 通过模板将数据导入到Word中
- txt文档内容导入到sql
- matlab中矩阵另存为txt
- 如何实现数据从 txt/word 自动导入到excel中
- SQLServer中创建外键和将txt中的内容导入到数据表中
- 从Word中读取内容&将word转换成txt
- 使用企业管理器将txt文件的内容导入到数据库中
- 将TXT文件中的内容正确导入到Excel表格中不混乱的方法
- 将GridView中内容导入到Excel(或Word)中
- TXT 文本文导入到access中
- txt文件导入到mysql中
- txt内容显示到datagrid中
- 爬虫 爬内容存到txt中
- jsp 导出内容到 txt 中。
- php 追加内容到txt文件中
- php 追加内容到txt文件中
- 将本地txt文档内容导入到NSMutableDictionary(动态字典)
- minimal rebuild failure, reverting to normal build
- 使用gem安装rails,付解决It seems your ruby installation is missing psych (for YAML output).
- C笔试题(判断大端小端模式)
- 第十四章 多线程编程
- poj2353 数装数组
- txt内容通过另存为方式导入到word中
- ubuntu 10.4 下安装samba
- 第十七章 java I/O系统
- 【白话系列】二分图匹配
- SplitContainerControl控件的使用
- 理解Active Directory复制
- Fedora17安装GEOS和在eclipse配置
- 单飞所需要素
- java 面试题