ªð¦^¦Cªí ¤W¤@¥DÃD µo©«

sheetÂàutf8 TXT °ÝÃD

¥»©«³Ì«á¥Ñ Joforn ©ó 2016-1-15 09:02 ½s¿è

µLªk¤U¸üªþ¥ó¡A¥u¯à«ö¦Û¤vªº·Qªk¼g¤F¡A½Ð°Ñ¦Ò¤U¼Óªº¥N½X¡C
¥@¬É¨º»ò¤j¡A¥i§Ú·Q¥h­þ¡H

TOP

¦^´_ 1# lionliu
¤£¦n·N«ä¡A2#ªº¥N½X¦³¿ù»~¡A©Ò¥H¦b3#­«·sªþ¤W·sªº¥N½X¡C½Ð·s¼W¤@­Ó¼Ò¶ô¡A±N¤U­±ªº¥N½XÖ߶K¨ì·sªº¼Ò¶ô¸Ì¡G
  1. #If VBA7 Then
  2.   Private Declare PtrSafe Function WideCharToMultiByte Lib "kernel32" _
  3.         (ByVal CodePage As Long, ByVal dwFlags As Long, _
  4.         ByVal lpWideCharStr As LongPtr, ByVal cchWideChar As Long, _
  5.         ByVal lpMultiByteStr As LongPtr, ByVal cchMultiByte As Long, _
  6.         Optional ByVal lpDefaultChar As Long = 0, Optional ByVal lpUsedDefaultChar As Long = 0) As Long
  7. #Else
  8.   Private Declare Function WideCharToMultiByte Lib "kernel32" _
  9.         (ByVal CodePage As Long, ByVal dwFlags As Long, _
  10.         ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, _
  11.         ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, _
  12.         Optional ByVal lpDefaultChar As Long = 0, Optional ByVal lpUsedDefaultChar As Long = 0) As Long
  13. #End If

  14. Public Sub TestSave()
  15.   Dim FileName  As Variant
  16.   
  17.   FileName = Application.GetSaveAsFilename(FileFilter:="UTF-8 Text File,*.TXT", FilterIndex:=1)
  18.   If VarType(FileName) = vbString Then
  19.     If Len(Dir(FileName, vbHidden Or vbReadOnly Or vbSystem)) Then
  20.       If MsgBox("¤åÀɤw¦s¦b¡A¬O§_´_¼g¦¹¤åÀÉ¡H", vbYesNo) = vbNo Then Exit Sub
  21.       SetAttr FileName, vbNormal
  22.       Kill FileName
  23.       If Len(Dir(FileName, vbHidden Or vbReadOnly Or vbSystem)) Then
  24.         MsgBox "¿ù»~¡G" & vbCrLf & "µLªk«O¦s¬°«ü©wªº¤å¥ó¦W¡A¤å¥ó³Q¦û¥Î©ÎÅv­­¤£¤¹³\¡C", vbCritical
  25.         Exit Sub
  26.       End If
  27.     End If
  28.     MsgBox "·í«e¤u§@ªí""" & ActiveSheet.Name & """¥t¦s¬°UTF8 Text¤åÀÉ" & IIf(SaveAsUTF8Text(FileName), "¦¨¥\¡I", "¥¢±Ñ¡I"), vbInformation
  29.   End If
  30. End Sub

  31. Public Function SaveAsUTF8Text(ByVal FileName As String) As Boolean
  32.   Dim FileTemp  As String
  33.   Dim wkIndex   As Long
  34.   Dim lFile     As Long
  35.   Dim bytArr()  As Byte
  36.   Dim bytUTF8() As Byte
  37.   Dim WB        As Workbook
  38.   Dim DisplayAlerts As Boolean, ScreenUpdating As Boolean
  39.   
  40.   On Error Resume Next
  41.    
  42.   DisplayAlerts = Application.DisplayAlerts: ScreenUpdating = Application.ScreenUpdating
  43.   Application.DisplayAlerts = False: Application.ScreenUpdating = False
  44.   wkIndex = ActiveSheet.Index
  45.   FileTemp = GetTempFileName(FileName, "*.XLS")
  46.   ThisWorkbook.SaveCopyAs FileTemp
  47.   Set WB = Workbooks.Open(FileTemp)
  48.   If Not (WB Is Nothing) Then
  49.     WB.Sheets(wkIndex).SaveAs FileName:=FileName, FileFormat:=xlUnicodeText
  50.     WB.Close SaveChanges:=False
  51.     Kill FileTemp
  52.     lFile = FileLen(FileName)
  53.     If lFile > 0 Then
  54.       If lFile > 2 Then
  55.         ReDim bytArr(0 To lFile - 3)
  56.         lFile = FreeFile
  57.         Open FileName For Binary As lFile
  58.         Get lFile, 3, bytArr()
  59.         Close lFile
  60.         Kill FileName
  61.         lFile = WideCharToMultiByte(65001, 0, VarPtr(bytArr(0)), (UBound(bytArr) + 1) \ 2, 0, 0)
  62.         ReDim bytUTF8(0 To 2 + IIf(lFile > 0, lFile, 0))
  63.         If lFile > 0 Then WideCharToMultiByte 65001, 0, VarPtr(bytArr(0)), (UBound(bytArr) + 1) \ 2, VarPtr(bytUTF8(3)), lFile
  64.       Else
  65.         ReDim bytUTF8(0 To 2)
  66.       End If
  67.       bytUTF8(0) = &HEF: bytUTF8(1) = &HBB: bytUTF8(2) = &HBF
  68.       lFile = FreeFile
  69.       Open FileName For Binary As lFile
  70.       Put lFile, , bytUTF8()
  71.       Close lFile
  72.       SaveAsUTF8Text = True
  73.     End If
  74.   End If
  75.   Application.DisplayAlerts = DisplayAlerts: Application.ScreenUpdating = ScreenUpdating
  76. End Function

  77. Private Function GetTempFileName(ByVal FileName As String, Optional ByVal FileType As String) As String
  78.   Dim I As Long
  79.   
  80.   If Len(FileType) Then
  81.     I = InStrRev(FileType, ".")
  82.     If I > 0 Then
  83.       If I = Len(FileType) Then
  84.         FileType = ".XLS"
  85.       Else
  86.         FileType = Mid$(FileType, I)
  87.       End If
  88.     Else
  89.       FileType = "." & FileType
  90.     End If
  91.   Else
  92.     FileType = ".XLS"
  93.   End If
  94.   I = InStrRev(FileName, ".")
  95.   If I > 0 Then FileName = Left$(FileName, I - 1)
  96.   I = 1
  97.   Do While Len(Dir(FileName & I & FileType, vbReadOnly Or vbHidden Or vbSystem Or vbDirectory))
  98.     I = I + 1
  99.   Loop
  100.   GetTempFileName = FileName & I & FileType
  101. End Function
½Æ»s¥N½X
¹B¦æ§»"TestSave"±N·|§â·í«eªº¤u§@ªí«O¦s¬°«ü©wªº¤åÀÉ¡C
¥@¬É¨º»ò¤j¡A¥i§Ú·Q¥h­þ¡H

TOP

¦^´_ 5# lionliu
3#ªº¥N½X¬Û¤ñ§A§ä¨ìªº¥N½X¦Ó¨¥§ó¦X¾A¡C
·íµM¤F¡A¦pªG§A¤£¦Ò¼{¨t²Î­Ý®e©Ê©M¹B¦æ³t«×ªº¸Ü´N¥Î§A»{¬°¦nªº¥N½X§Y¥i¡C
§A§ä¨ìªº¥N½X©M§ÚÖߥX¨Óªº¥N½X¦b¤j³°ªººô¯¸¤W´X¦~«e¦³¤£¤Ö°Q½×ªº¡A³o¸Ì§Ú´N¤£­«´_¤F¡C
¥@¬É¨º»ò¤j¡A¥i§Ú·Q¥h­þ¡H

TOP

        ÀR«ä¦Û¦b : ¤H¥Í¨S¦³©Ò¦³Åv¡A¥u¦³¥Í©Rªº¨Ï¥ÎÅv¡C
ªð¦^¦Cªí ¤W¤@¥DÃD