加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

vb.net – 如何将CSV文件重新格式化为Google日历格式?

发布时间:2020-12-17 07:17:04 所属栏目:百科 来源:网络整理
导读:因此,在做了一些研究后,我能够找到获取CSV文件所需的格式 Subject,Start Date,Start Time,End Date,End Time,All Day Event,Description,Location,Private 问题是,我正在使用的CSV导出格式或顺序不正确,收集该信息的最佳方式是什么?这是我的一些来源. 名称,
因此,在做了一些研究后,我能够找到获取CSV文件所需的格式

Subject,Start Date,Start Time,End Date,End Time,All Day Event,Description,Location,Private

问题是,我正在使用的CSV导出格式或顺序不正确,收集该信息的最佳方式是什么?这是我的一些来源.

名称,用户名,行类型,开始日期,开始时间,结束时间,结束日期,段开始日期,类型

“Smith,John J”,jjs,Shift,5/29 / 2011,9:30,17:30,5 / 29 / 2011,5 / 29/2011,Regular

“Smith,5/30 / 2011,13:30,5 / 30 / 2011,5 / 30/2011,Regular

Dim Name As String = ""
    Dim UserName As String = ""

    Dim Data As String = """Smith,John J"",jj802b,5/29/2011,9:30,17:30,Transfer"

    For r As Integer = 1 To 10
        Name = Data.Substring(0,Data.LastIndexOf(""""))
        Data = Data.Remove(0,Data.LastIndexOf(""""))
        UserName = Data.Substring(Data.LastIndexOf(""""),",")
    Next

解决方法

以下是解决方案

Dim Name As String = ""
Dim UserName As String = ""

Dim Data As String = """Smith,Transfer"

For r As Integer = 1 To 10
    Dim DataArr() As String = DecodeCSV(Data) 'Use DecodeCSV function to regex split the string 
    Name = DataArr(0) 'Get First item of array as Name
    UserName = DataArr(1)  'Get Second item of array as UserName 
Next

DecodeCSV by Tim的优秀代码

Public Shared Function DecodeCSV(ByVal strLine As String) As String()

    Dim strPattern As String
    Dim objMatch As Match

    ' build a pattern
    strPattern = "^" ' anchor to start of the string
    strPattern += "(?:""(?<value>(?:""""|[^""fr])*)""|(?<value>[^,fr""]*))"
    strPattern += "(?:,(?:[ t]*""(?<value>(?:""""|[^""fr])*)""|(?<value>[^,fr""]*)))*"
    strPattern += "$" ' anchor to the end of the string

    ' get the match
    objMatch = Regex.Match(strLine,strPattern)

    ' if RegEx match was ok
    If objMatch.Success Then
        Dim objGroup As Group = objMatch.Groups("value")
        Dim intCount As Integer = objGroup.Captures.Count
        Dim arrOutput(intCount - 1) As String

        ' transfer data to array
        For i As Integer = 0 To intCount - 1
            Dim objCapture As Capture = objGroup.Captures.Item(i)
            arrOutput(i) = objCapture.Value

            ' replace double-escaped quotes
            arrOutput(i) = arrOutput(i).Replace("""""","""")
        Next

        ' return the array
        Return arrOutput
    Else
        Throw New ApplicationException("Bad CSV line: " & strLine)
    End If

End Function

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读