Skip to content Skip to sidebar Skip to footer

How To Dump The Data From File To An Excel Sheet

I want to dump [3-4 lines together] some data to an excel sheet. I could able to dump single line based on some criteria [like if line is getting start with // or /* ], but in case

Solution 1:

import re

fileopen = open("test.c")

# Convert file to a string
source_code = ""
for var in fileopen:
    source_code += var

# Find the first comment from the source code
pattern = r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"'
var1 = re.search(pattern, source_code, re.DOTALL | re.MULTILINE).group() # first comment

var1 = unicode(var1, errors='ignore')
worksheet.write(i, 5, var1, cell_format)

Solution 2:

This VBA reads a text file and dumps all comment lines to the first worksheet

Public Sub test()
    Dim iFN As Integer
    Dim sLine As String
    Dim iMultiple As Integer
    Dim sComment As String
    Dim iRow As Integer

    iFN = FreeFile()
    iMultiple = 0
    sComment = ""
    iRow = 1

    'Change this path as required
    Open "d:\temp\xl.txt" For Input As #iFN

    While Not EOF(iFN)
        sLine = ""
        Line Input #iFN, sLine
        If iMultiple = 1 Then
            sComment = sComment & sLine
            If Left(sLine, 2) = "*/" Then
                iMultiple = 0
            End If
        Else
            If Left(sLine, 2) = "//" Then
                sComment = sLine
            ElseIf Left(sLine, 2) = "/*" Then
                sComment = sLine
                iMultiple = 1

            End If
        End If
        If iMultiple = 0 And Trim(sComment) <> "" Then
            ThisWorkbook.Worksheets(1).Cells(iRow, 1).Value2 = sComment
            iRow = iRow + 1
            sComment = ""
        End If
    Wend
    Close #iFN
    MsgBox "Done!"
End Sub

Post a Comment for "How To Dump The Data From File To An Excel Sheet"