2016-01-18 430 views
0

我正在執行以下步驟並遇到UTF-8字符問題:
- 讀取CSV文件(用「|」分隔)包含UTF-8字符。
- 解析該文件並保存基於特定條件的新文件(消除在同一Remove_ROW文本的標準之一行)VBA - 用utf-8讀取CSV並用utf-8寫出另一個CSV

保存的文件,我有不保存UTF-8字符。它只是用一些亂碼字符保存它。

Set tdaywb = Workbooks.Open(lbltoday.Caption) 'lbltoday.Caption has the filename 
Set tdaySht = tdaywb.Sheets(1) 
tdayLastRow = tdaySht.Range("A" & Rows.Count).End(xlUp).Row 

For x = 2 To tdayLastRow 
    If x > tdayLastRow Then 
     Exit For 
    End If 
    If InStr(1, tdaySht.Cells(x, 1), "Remove_ROW") > 0 Then 
     tdaySht.Rows(x).EntireRow.Delete 
     remCount = remCount + 1 
     tdayLastRow = tdayLastRow - 1 
    End If 
Next x 

tdaySht.Activate 

With ActiveWorkbook 
    .SaveAs "C:\test.csv" 
    .Close 0 
End With 

我將不知道如何保存這個UTF-8字符保存的幫助。

問候, AYUSH

+0

我發現幾個環節後,一些四處尋找在互聯網上..將嘗試發佈一個答案 – ayushku

回答

0

後這裏的一些研究是我發現:

Sub OpenTextFile() 
strSheetName = ReadUTF8CSVToSheet("C:\file1.csv") 
WriteCSV 
End Sub 

Function ReadUTF8CSVToSheet(file As String) 
Dim ws As Worksheet 
Dim strText As String 
' read utf-8 file to strText variable 
    With CreateObject("ADODB.Stream") 
    .Open 
    .Type = 1 ' Private Const adTypeBinary = 1 
    .LoadFromFile file 
    .Type = 2 ' Private Const adTypeText = 2 
    .Charset = "utf-8" 
    strText = .ReadText(-1) ' Private Const adReadAll = -1 
End With 

' parse strText data to a sheet 
Set ws = Sheets.Add() 
intRow = 1 
For Each strLine In Split(strText, Chr(10)) 
    If strLine <> "" Then 
     With ws 
      .Cells(intRow, 1) = strLine 
      .Cells(intRow, 1).TextToColumns Destination:=Cells(intRow, 1), DataType:=xlDelimited, _ 
       TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _ 
       Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar:="|" 
     End With 

     intRow = intRow + 1 
    End If 
Next strLine 
ReadUTF8CSVToSheet = ws.Name 
End Function 

Public Sub WriteCSV() 
Set wkb = ActiveSheet 

Dim fileName As String 
Dim MaxCols As Integer 
Dim lMaxCol, lMaxRow As Double 
fileName = Application.GetSaveAsFilename("", "CSV File (*.csv), *.csv") 

If fileName = "False" Then 
End 
End If 

On Error GoTo eh 
Const adTypeText = 2 
Const adSaveCreateOverWrite = 2 

Dim BinaryStream 
Set BinaryStream = CreateObject("ADODB.Stream") 
BinaryStream.Charset = "UTF-8" 
BinaryStream.Type = adTypeText 
BinaryStream.Open 

C = 1 
lMaxCol = 0 
While Not Len(wkb.Cells(1, C).Value) = 0 'wkb.Cells(row, column).Value 
    s = s & wkb.Cells(1, C).Value & "|" 
    C = C + 1 
Wend 
BinaryStream.WriteText s, 1 
lMaxCol = C - 1 

r = 1 
While Not Len(wkb.Cells(r + 1, 1).Value) = 0 'wkb.Cells(row, column).Value 
    r = r + 1 
Wend 
    lMaxRow = r - 1 
For r = 1 To lMaxRow 
s = "" 
For C = 1 To lMaxCol 
    s = s & wkb.Cells(r + 1, C).Value & "|" 
Next C 
BinaryStream.WriteText s, 1 
Next r 

BinaryStream.SaveToFile fileName, adSaveCreateOverWrite 
BinaryStream.Close 

MsgBox "CSV generated successfully" 

eh: 

End Sub 
+0

這並不工作對我來說..我仍然需要添加我的條件規則。但是我發現這是讀取帶有UTF-8字符的csv的一種方式,它將UTF-8字符保存下來(注意:寫出一個管道分隔文件,但這就是我正在尋找的內容,您可以更改它根據您的需要,如果您想要更改WriteCSV例程。 – ayushku