ควบคุมการนำเข้า CSV ใน excel 2010 [ซ้ำกัน]


0

คำถามนี้มีคำตอบอยู่ที่นี่แล้ว:

ฉันมี csv ที่มีค่าเช่นนี้:

"ค่าที่ยาวมากพร้อมด้วยเครื่องหมายจุลภาค", "387621937291732193"

จำนวนแม้จะอยู่ในเครื่องหมายคำพูดจะกลายเป็นตัวเลขใน excel และปรากฏในสัญกรณ์ทางวิทยาศาสตร์ คุณจะป้องกัน excel จากการสมมติว่าทุกอย่างเป็นตัวเลขได้อย่างไร ทำไม excel ไม่แสดงตัวเลือกใด ๆ เมื่อเปิด CSV เช่นเดียวกับไฟล์. txt


คำตอบ:


0

คุณสามารถเพิ่ม=การเริ่มต้นของสตริงตัวเลข:

"a very long value, with a comma",="387621937291732193"

ปรับปรุง

หากคุณไม่สามารถเปลี่ยน CSV ได้คุณมีสองตัวเลือก คุณสามารถตั้งค่าคุณสมบัติคอลัมน์เพื่อแสดงในรูปแบบตัวเลขหลังจากโหลด CSV Format Cell...เพียงแค่คลิกขวาบนมือถือและเลือก จากนั้นเพียงแค่เลือกNumberในรายการ มันเป็นรายการที่สอง ปิดและมันควรจะดูดี

หากคุณต้องการไฟล์ที่จะนำเข้าสู่ Excel ถูกต้องแล้วคุณจะต้องดำเนินการกับไฟล์ด้วยตัวเอง คุณสามารถทำได้ในภาษาการเขียนโปรแกรมใด ๆ เพียงอ่านในฟิลด์และส่งออกตามที่คุณต้องการ

นี่คือตัวอย่าง vbscript:

' The columns begin at index 0, this array should include indexes for each column which should be treated literally.
' The script will add a = before these columns if it doesn't already exist.

' If you want you could add the ability to set this at the command line to make this more flexible.
literalColumns=Array(1)

'----------------------------------------------------------
' Nothing else should need to be changed.

IsOK=True
FileNotFound=False
CSVFileName=""
OutputFileName="output.csv" ' This is the default output file to use when none is given via the command line.

If WScript.Arguments.Count = 1 Or WScript.Arguments.Count = 2 Then
    CSVFileName = WScript.Arguments.Item(0)
    If WScript.Arguments.Count = 2 Then
        OutputFileName = WScript.Arguments.Item(1)
    End If
Else
    CSVFileName = InputBox("Enter a CSV file name to process:", "CSV Input File")
    If Len(CSVFileName) < 1 Then
        IsOK=False
        FileNotFound = True
    End If
End If

If IsOK Then
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.FileExists(CSVFileName) Then
        ProcCSV CSVFileName, OutputFileName
    Else
        IsOK = False
        FileNotFound = True
    End If
End If

If IsOK = False Then
    msg="Usage: PrepCSV.vbs CSVFileName [OutputFileName]"
    If FileNotFound Then
        msg = msg & vbCrLf & vbCrLf &"File Not Found."
    End If
    Wscript.Echo msg
    Wscript.Quit
End If

Sub ProcCSV(InFileName, OutFileName)
    ReDim ToInsert(0)
    Set FS = CreateObject("Scripting.FileSystemObject")
    Set InFile = FS.OpenTextFile(InFileName, 1, False, -2)
    Set OutFile = objFSO.CreateTextFile(OutFileName)
    Set Regex = CreateObject("VBScript.RegExp")
    Regex.Pattern = """[^""]*""|[^,]*"
    Regex.Global = True
    Do While Not InFile.AtEndOfStream
        ReDim ToInsert(0)
        CSVLine = InFile.ReadLine
        For Each Match In Regex.Execute(CSVLine)
            If Match.Length > 0 Then
                ColDX = UBound(ToInsert)
                ReDim Preserve ToInsert(ColDX + 1)
                If InArray(ColDX, literalColumns) And Left(Match.Value, 1) <> "=" Then
                    ToInsert(ColDX) = "=" & Match.Value
                Else
                    ToInsert(ColDX) = Match.Value
                End If
            End If
        Next
        CSVLine = Join(ToInsert, ",")
        OutFile.Write Left(CSVLine, Len(CSVLine) - 1) & vbCrLf
    Loop
    InFile.Close
    OutFile.Close
End Sub

Function InArray(item, arr)
    For i=0 To UBound(arr)
        If arr(i) = item Then
            InArray=True
            Exit Function
        End If
    Next
    InArray=False
End Function

PrepCSV.vbsจะใช้เพียงบันทึกข้อความข้างต้นเพื่อไฟล์ชื่อ จากนั้นคุณสามารถคลิกที่มันและป้อนชื่อไฟล์ที่จะประมวลผลหรือคุณสามารถเรียกมันได้จากบรรทัดคำสั่งเช่น:

PrepCSV.vbs inputfile.csv outputfile.csv

จะเป็นอย่างไรถ้าเราไม่สามารถเปลี่ยน CSV เป็นแบบไม่ได้มาตรฐาน
Swaroop

@Swaroop ดูการอัปเดต
krowe
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.