oldCodePage =sys(3101, 1251)
Set the oldCodePage back after using excelStringToUTF8(@utf8Out, m.stringIn, m.codepageIn) StringToUTF16(@m.utf8Out, m.stringIn, m.codepageIn) StringUTF16ToUTF8(@m.utf8Out, m.utf8Out)I have cut and pasted Рубінаў Анатоль Мікалаевіч into excel
function Do_it() local xlObj, sheetObj xlObj = createObject('Excel.Application') =xlObj.WorkBooks.Open('d:\tmp\book1.xlsx') sheetObj = m.xlObj.Sheets[1] =sys(3101, 1251) v = m.sheetObj.Cells(1,1).Value utf8 = '' assert StringToUTF8(@m.utf8, m.v, 1251) ?utf8 assert false endfunc *_______________________________________________________________________________Functions
&& StringConversion && Gregory Adam 2011 *=============================================================================== #include "FoxPro.h" *=============================================================================== #define true .T. #define false .F. *=============================================================================== *=============================================================================== function StringToUTF8(utf8Out, stringIn, codepageIn) local success success = true do case case !m.success case !StringToUTF16(@m.utf8Out, m.stringIn, m.codepageIn) assert false success = false case !StringUTF16ToUTF8(@m.utf8Out, m.utf8Out) assert false success = false endcase return m.success endfunc *_______________________________________________________________________________ *_______________________________________________________________________________ *_______________________________________________________________________________ #define CP_ACP 0 #define CP_MACCP 2 #define CP_OEMCP 1 #define CP_SYMBOL 42 #define CP_THREAD_ACP 3 #define CP_UTF7 65000 #define CP_UTF8 65001 #define MB_PRECOMPOSED 0x1 #define MB_COMPOSITE 0x2 #define MB_USEGLYPHCHARS 0x4 #define MB_ERR_INVALID_CHARS 0x8 #define WC_DEFAULTCHAR 0x00000040 #define WC_ERR_INVALID_CHARS 0x00000080 #define WC_NO_BEST_FIT_CHARS 0x00000400 *_______________________________________________________________________________ function StringToUTF16(utf16Out, stringIn, codepageIn) local success success = true do case case !m.success case empty(len(m.stringIn)) utf16Out = '' otherwise local lpWideCharStr, result lpWideCharStr = space(len(m.stringIn)*2) result = MultiByteToWideChar( ; evl(m.codepageIn, cpcurrent()), ; MB_ERR_INVALID_CHARS, ; @m.stringIn, ; len(m.stringIn), ; @m.lpWideCharStr, ; len(m.lpWideCharStr) ; ) do case case !m.success case empty(m.result) assert false success = false otherwise utf16Out = left(m.lpWideCharStr, m.result * 2) endcase endcase return m.success endfunc *_______________________________________________________________________________ function StringUTF16ToUTF8(utf8Out, utf16In) local success success = true do case case !m.success case empty(len(m.utf16In)) utf8Out = '' otherwise local lpMultiByteStr, lpUsedDefaultChar, result lpMultiByteStr = space(len(m.utf16In) * 2) lpUsedDefaultChar = 0 result = WideCharToMultiByte( ; CP_UTF8, ; WC_ERR_INVALID_CHARS, ; @m.utf16In, ; len(m.utf16In)/2, ; @m.lpMultiByteStr, ; len(m.lpMultiByteStr), ; null, ; @m.lpUsedDefaultChar ; ) do case case !m.success case empty(m.result) assert false success = false otherwise utf8Out = left(m.lpMultiByteStr, m.result) endcase endcase return m.success endfunc *_______________________________________________________________________________ function MultiByteToWideChar lparameters codepage, ; dwFlags, ; lpMultiByteStr, ; cbMultiByte, ; lpWideCharStr, ; cchWideChar local success success = true local result do case case !m.success otherwise try declare integer MultiByteToWideChar in Kernel32.dll ; long codepage, ; long dwFlags, ; string@ lpMultiByteStr, ; integer cbMultiByte, ; string@ lpWideCharStr, ; integer cchWideChar result = MultiByteToWideChar( ; m.codepage, ; m.dwFlags, ; @m.lpMultiByteStr, ; m.cbMultiByte, ; @m.lpWideCharStr, ; m.cchWideChar ; ) catch assert false success = false endtry endcase return iif(m.success, m.result, 0) endfunc *_______________________________________________________________________________ function WideCharToMultiByte lparameters codepage, ; dwFlags, ; lpWideCharStr, ; cchWideChar, ; lpMultiByteStr, ; cbMultiByte, ; lpDefaultChar, ; lpUsedDefaultChar local success success = true local result do case case !m.success otherwise try declare integer WideCharToMultiByte in Kernel32.dll ; long codepage, ; long dwFlags, ; string@ lpWideCharStr, ; integer cchWideChar, ; string@ lpMultiByteStr, ; integer cbMultiByte, ; string lpDefaultChar, ; integer @lpUsedDefaultChar result = WideCharToMultiByte ( ; m.codepage, ; m.dwFlags, ; @m.lpWideCharStr, ; m.cchWideChar, ; @m.lpMultiByteStr, ; m.cbMultiByte, ; m.lpDefaultChar, ; @m.lpUsedDefaultChar; ) catch assert false success = false endtry endcase return iif(m.success, m.result, 0) endfunc *_______________________________________________________________________________