Background
Luacom is a very powerful module that allows us to apply various COM components, such as Word. However, there is a problem that the Chinese file name is not recognized. Why? Because the COM is Unicode, luacom requires that all inputs be UTF-8, And the luacom output is also UTF-8. Can this be swollen?
Iconv
GNU has a libiconv library. If there is one, we will not be afraid of it! Search on luaforge and find LUA-iconv. Install it!
Luarocks install LUA-iconv sorry, an error occurred! There are two reasons for the error:
- We have not installed the libiconv library.
- Lua-iconv does not provide Cl compilation methods
Compile LUA-iconv by yourself
- Download the compiled Windows Version
Libiconv
- Download
Lua-iconvSource code
- Decompress the libiconv-1.9.1.bin.woe32.zip file and add the include directory toInclude filesDirectory to add the lib directory toLibrary filesDirectory (see the previous article)
- Decompress the LUA-iconv SourceCode
- Create an empty Win32 DLL project in vs2008 (see the previous article), name it luaiconv, and add the luaiconv. c file to the project.
- Modify source code: int luaopen_iconv (lua_state * l)-> _ declspec (dllexport) int luaopen_luaiconv (lua_state * l)
- Note: The generated DLL file name must be the same as luaiconv in luaopen_luaiconv (see the previous article)
- Project properties-> linker-> input-> additional library file: lua51.lib iconv. Lib charset. Lib
- Compile and generate the luaiconv. dll file for release
- Copy the luaiconv.dllfile to the clibs directory of luaforwindows together with the iconv. dll file extracted by libiconv-1.9.1.bin.woe32.zip (you know why I want to change the luaopen_iconv function name ).
Test
Require "luacom" require "luaiconv" function createiconv (to, from) Local Cd = iconv. new (to, from) return function (txt) return CD: iconv (txt) endendl = createiconv ("UTF-8", "GBK") -- Note: running this file will modify C: \ export word.docx file, please note that backup wordapp = assert (luacom. createobject ("word. application ") wordapp. visible = trueworddocpath = l "C: \ export word.docx" if not pcall (function () worddoc = wordapp. documents: open (worddocpath) End) thenworddoc = wordapp. documents: add () endwordapp. selection: typetext (L "word") worddoc: saveas2 (worddocpath, wdformatdocument)
The above test code will create the c: \ mongoword.docx file during the first run. The file will be opened later, and the "Hello word" text will be entered each time you run it. If you are using Word 2003, change docx to Doc.
References
Http://hi.baidu.com/nivrrex/blog/item/17c231adad9e8a0f4b36d6ca.html
This eldest brother wrote a Conversion Function Using VC functions, but it was not encapsulated into a library. In addition, I think it is not concise enough. It is better to use the iconv library and it is not prone to errors.
Http://www.cppblog.com/darkdestiny/archive/2009/04/25/81055.html
This eldest brother realized the conversion with iconv and did not encapsulate it into a library. My "L" function is also borrowed from him. Thank you very much! However, I think it has two weaknesses compared to mine:
1. Each call to the L function must go through the iconv open, conversion, and Close processes. However, I only need to open one form of conversion (LUA-iconv implements ^_^)
2. If you want to implement reverse conversion, that is, UTF-8 to GBK, You have to modify the module, and I will not use it here (of course, it is also the ^_^ implemented by LUA-iconv)
Libiconv description
The solution here is not quite right, the end-level solution see http://www.cnblogs.com/windtail/archive/2012/09/06/2674260.html