Copy dữ liệu từ file Json sang Excel

Liên hệ QC

luu.nguyen21

Thành viên mới
Tham gia
30/9/21
Bài viết
7
Được thích
0
Thân chào anh/em,
Nhờ ae cao tay chỉ dùm mình cách copy dữ liệu từ file json sang file excel. (file mẫu trong tệp đính kèm) cám ơn ae rất nhiều!
 

File đính kèm

  • Import Json.zip
    13.1 KB · Đọc: 20
Thân chào anh/em,
Nhờ ae cao tay chỉ dùm mình cách copy dữ liệu từ file json sang file excel. (file mẫu trong tệp đính kèm) cám ơn ae rất nhiều!
Dùng sub này
Rich (BB code):
Sub GetFileText_utf8()

Dim st As Object
Dim sPathname As String, sText As String
Dim A() As String, Cline As Double, B() As String
Dim i As Long

    Set st = CreateObject("ADODB.Stream")
    sPathname = "D:\TestGPE\Import Json\" & "Customer_info.json"  'Sửa lại đường dẫn cho phù hợp
    st.Charset = "utf-8"
    st.Open
    st.LoadFromFile (sPathname)
    sText = st.ReadText(-1)  'Doc characters (Dung tham so -1 doc All file)
    A = Split(sText, vbLf)
    Cline = UBound(A) + 1
    ReDim B(1 To Cline, 1 To 1)
   
    Application.ScreenUpdating = False
    For i = 0 To Cline - 1
        B(i + 1, 1) = A(i)
    Next i
    Sheet2.Range("A1:A100000").ClearContents
    Sheet2.Range("A1").Resize(Cline, 1) = B
    st.Close
    Set st = Nothing
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
Dùng sub này
Rich (BB code):
Sub GetFileText_utf8()

Dim st As Object
Dim sPathname As String, sText As String
Dim A() As String, Cline As Double, B() As String
Dim i As Long

    Set st = CreateObject("ADODB.Stream")
    sPathname = "D:\TestGPE\Import Json\" & "Customer_info.json"  'Sửa lại đường dẫn cho phù hợp
    st.Charset = "utf-8"
    st.Open
    st.LoadFromFile (sPathname)
    sText = st.ReadText(-1)  'Doc characters (Dung tham so -1 doc All file)
    A = Split(sText, vbLf)
    Cline = UBound(A) + 1
    ReDim B(1 To Cline, 1 To 1)
  
    Application.ScreenUpdating = False
    For i = 0 To Cline - 1
        B(i + 1, 1) = A(i)
    Next i
    Sheet2.Range("A1:A100000").ClearContents
    Sheet2.Range("A1").Resize(Cline, 1) = B
    st.Close
    Set st = Nothing
    Application.ScreenUpdating = True

End Sub
Bạn ơi mình muốn import vào file excel sẽ hiển thị theo dòng và cột như bên file excel mẫu đính kèm
Nhờ bạn xem dùm mình. cám ơn bạn rất nhiều
1633681842013.png
 
Upvote 0
Gộp cả 2 việc. Tạm thời chưa có tiêu đề, dữ liệu chép ra ở H2
Nếu muốn tiêu đề thì làm tiếp, chừ tôi bận rồi
Rich (BB code):
Sub GetFileText_utf8()

Dim st As Object
Dim sPathname As String, sText As String
Dim A() As String, B() As String, Cline As Double
Dim i&, j&, k&, Dau&, Cuoi&

    Set st = CreateObject("ADODB.Stream")
    sPathname = "D:\TestGPE\Import Json\" & "Customer_info.json"
    st.Charset = "utf-8"
    st.Open
    st.LoadFromFile (sPathname)
    sText = st.ReadText(-1)  'Doc characters (Dung tham so -1 doc All file)
    A = Split(sText, vbLf)
    Cline = UBound(A) + 1
    ReDim B(1 To Cline, 1 To 7)
    j = 1
    For i = 0 To Cline - 1
        If InStr(1, A(i), """") Then
            k = k + 1
            Dau = InStr(1, A(i), ":") + 3: Cuoi = InStrRev(A(i), """")
            B(j, k) = Mid(A(i), Dau, Cuoi - Dau)
            If k = 7 Then k = 0: j = j + 1
        End If
    Next i
    st.Close
    Set st = Nothing
    Sheet2.Range("H2").Resize(j, 7) = B

End Sub
 
Upvote 0
Gộp cả 2 việc. Tạm thời chưa có tiêu đề, dữ liệu chép ra ở H2
Nếu muốn tiêu đề thì làm tiếp, chừ tôi bận rồi
Rich (BB code):
Sub GetFileText_utf8()

Dim st As Object
Dim sPathname As String, sText As String
Dim A() As String, B() As String, Cline As Double
Dim i&, j&, k&, Dau&, Cuoi&

    Set st = CreateObject("ADODB.Stream")
    sPathname = "D:\TestGPE\Import Json\" & "Customer_info.json"
    st.Charset = "utf-8"
    st.Open
    st.LoadFromFile (sPathname)
    sText = st.ReadText(-1)  'Doc characters (Dung tham so -1 doc All file)
    A = Split(sText, vbLf)
    Cline = UBound(A) + 1
    ReDim B(1 To Cline, 1 To 7)
    j = 1
    For i = 0 To Cline - 1
        If InStr(1, A(i), """") Then
            k = k + 1
            Dau = InStr(1, A(i), ":") + 3: Cuoi = InStrRev(A(i), """")
            B(j, k) = Mid(A(i), Dau, Cuoi - Dau)
            If k = 7 Then k = 0: j = j + 1
        End If
    Next i
    st.Close
    Set st = Nothing
    Sheet2.Range("H2").Resize(j, 7) = B

End Sub
cám ơn bạn rất nhiều để mình thử xem
 
Upvote 0
Có thêm tiêu đề
Rich (BB code):
Sub GetFileText_utf8()

Dim st As Object
Dim sPathname As String, sText As String
Dim A() As String, B() As String, aTtl() As String, Cline As Double
Dim i&, j&, k&, Dau&, Cuoi&

    Set st = CreateObject("ADODB.Stream")
    sPathname = "D:\TestGPE\Import Json\" & "Customer_info.json"
    st.Charset = "utf-8"
    st.Open
    st.LoadFromFile (sPathname)
    sText = st.ReadText(-1)  'Doc characters (Dung tham so -1 doc All file)
    A = Split(sText, vbLf)
    Cline = UBound(A) + 1
    ReDim B(1 To Cline, 1 To 7)
    ReDim aTtl(1 To 7)
    j = 1
    For i = 0 To Cline - 1
        If InStr(1, A(i), """") Then
            k = k + 1
            Dau = InStr(1, A(i), ":") + 3: Cuoi = InStrRev(A(i), """")
            If j = 1 Then aTtl(k) = Mid(A(i), InStr(1, A(i), """") + 1, Dau - 5 - InStr(1, A(i), """"))
            B(j, k) = Mid(A(i), Dau, Cuoi - Dau)
            If k = 7 Then k = 0: j = j + 1
        End If
    Next i
    st.Close
    Set st = Nothing
    Sheet2.Range("H2").Resize(j, 7) = B
    Sheet2.Range("H1").Resize(1, 7) = aTtl
    Sheet2.Range("H1").Resize(1, 7).Font.Bold = True
End Sub
 
Upvote 0
Có thêm tiêu đề
Rich (BB code):
Sub GetFileText_utf8()

Dim st As Object
Dim sPathname As String, sText As String
Dim A() As String, B() As String, aTtl() As String, Cline As Double
Dim i&, j&, k&, Dau&, Cuoi&

    Set st = CreateObject("ADODB.Stream")
    sPathname = "D:\TestGPE\Import Json\" & "Customer_info.json"
    st.Charset = "utf-8"
    st.Open
    st.LoadFromFile (sPathname)
    sText = st.ReadText(-1)  'Doc characters (Dung tham so -1 doc All file)
    A = Split(sText, vbLf)
    Cline = UBound(A) + 1
    ReDim B(1 To Cline, 1 To 7)
    ReDim aTtl(1 To 7)
    j = 1
    For i = 0 To Cline - 1
        If InStr(1, A(i), """") Then
            k = k + 1
            Dau = InStr(1, A(i), ":") + 3: Cuoi = InStrRev(A(i), """")
            If j = 1 Then aTtl(k) = Mid(A(i), InStr(1, A(i), """") + 1, Dau - 5 - InStr(1, A(i), """"))
            B(j, k) = Mid(A(i), Dau, Cuoi - Dau)
            If k = 7 Then k = 0: j = j + 1
        End If
    Next i
    st.Close
    Set st = Nothing
    Sheet2.Range("H2").Resize(j, 7) = B
    Sheet2.Range("H1").Resize(1, 7) = aTtl
    Sheet2.Range("H1").Resize(1, 7).Font.Bold = True
End Sub
cám ơn bạn rất rất nhiều luôn
 
Upvote 0
Web KT

Bài viết mới nhất

Back
Top Bottom