【问题标题】:CSV to XLSX VB.NETCSV 到 XLSX VB.NET
【发布时间】:2017-01-09 15:55:22
【问题描述】:

enter image description hereenter image description here我正在学习 VB.NET,不幸的是,我的任务是做一些我不知道该怎么做的事情。

我需要创建一个基于 Windows 的快速应用程序来将 csv 文件导出到 XLSX 文件中。

是的,我知道其他帖子可能有类似的主题,但我认为这是独一无二的。

CSV 文件将有 5 个标题,`Line、Component、Picked、Placed 和 Missed。我们在第 2 列中有零件编号,它们将放在“组件”下。我从权力的角度理解,这个文件总结了总零件编号,即 0-5490045 和 JUKI 3 行,以及 Picked、Placed 和 Missed 零件的总数。我在下面提供了一个示例行。第一行是 csv 格式,第二行是输出。我不确定哪个循环最好是 FOR 循环、WHILE 循环等。我假设我需要某种循环来遍历 csv 文件中的所有数据。 我唯一的代码打开对话框并允许文件选择并尝试读入数据表。我正在尝试让这个工作,然后重组一些代码。

Imports Spire.Xls
Imports System.Windows.Forms
Imports System.Data

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim dialog As OpenFileDialog = New OpenFileDialog
        dialog.Filter="CSV document(*.csv)|*.csv"
        Dim result As DialogResult = dialog.ShowDialog
        If(result=DialogResult.OK) Then
            Dim csvFile As String = dialog.FileName
            Dim workbook As Workbook = New Workbook
            workbook.LoadFromFile(csvFile,",")
            Dim worksheet As Worksheet = workbook.Worksheets(0)
            Dim dt As DataTable=worksheet.ExportDataTable

            Me.dataGridView1.DataSource=dt
        End If
    End Sub


End Class



  JUKI 3    0-5490045 96    96  3

  Line  Component   Picked  Placed  Missed
  JUKI 3    0-5490045   99  96  3

【问题讨论】:

  • 您还没有真正描述您尝试过的内容或遇到的问题。你的问题具体是什么?如果您只是询问如何在 .NET 中创建 Excel 文件,在线上有相关的库/教程/示例。
  • 我建议您将问题拆分为较小的问题并调查每个问题,然后针对较小的问题发布特定问题。例如,你能打开一个文件吗?你能读一个文件吗?你会读csv吗?您可以存储 csv 内容吗?你能读取存储的内容吗? ...
  • Excel 会打开 CSV 文件,那么你的任务有什么意义呢?
  • 正如其他人建议的那样,您应该将问题分解为更小的可管理部分。读取csv 文件,写入excel,计算值。您的问题令人困惑,您需要完成什么。以 CSV 文件将有 3 个标题 开头的段落中的示例……没有多大意义。使用您的示例行示例更没有意义。第一行是来自csv 文件的数据?接下来的两行是您要显示的内容?除了“JUKI 3 机器”之外,它们似乎根本不匹配/总和/总计?甚至零件编号看起来也不同。
  • 您附加的 csv 链接也无济于事。它看起来像一个excel文件。你是如何将它读入你的程序的?您将必须指定您遇到问题的位置,读取文件,导出文件,计算无法正常工作......您将必须更具体并显示一些代码以获得答案;否则投机只是浪费时间。我建议你检查一下。 How to create a Minimal, Complete, and Verifiable example

标签: excel vb.net winforms csv


【解决方案1】:

我讨厌提出建议,而不是展示它是如何工作的。下面是一个使用名为Machine 的自定义对象来保存数据的示例。此类是对象的最低要求,仅用作入门的示例。它有一些字段可以在遍历列表进行计算时派上用场。您也可以在这里添加一些自定义函数/子项,以帮助完成一些涉及“机器”对象的任务。此外,您可以在此处添加一些比较功能,这些功能将使您能够对其他内容进行排序。将所有这些放在一起后,您应该会得到一个有效的 Machine 对象列表。

您可以使用此列表来帮助您继续执行任务的计算/删除重复部分。在计算数据的过程中,您可以创建Machine 对象的最终列表,您可以使用这些对象导出到带有标题的 Excel 或将其显示到 DataGridView。希望这会有所帮助。

机器类

Public Class Machine
  Private name As String
  Private partNumber As String
  Private inventoryIn As Integer
  Private inventoryOut As Integer
  Private inventoryMissing As Integer

  Public Sub New(inName As String, inPartNum As String, inInvIn As Integer, inInvOut As Integer, InInvMis As Integer)
    name = inName
    partNumber = inPartNum
    inventoryIn = inInvIn
    inventoryOut = inInvOut
    inventoryMissing = InInvMis
  End Sub

  Property GetName As String
    Get
      Return name
    End Get
    Set(value As String)
      name = value
    End Set
  End Property

  Public Overrides Function ToString() As String
    Return "Name: " + name + " #: " + partNumber + vbTab + " In:" + inventoryIn.ToString() + " Out:" + inventoryOut.ToString() + " Miss:" + inventoryMissing.ToString()
  End Function
End Class

现在您的问题是读取文件

我没有使用任何涉及 excel 的东西。由于您有一个简单的csv 文件,我们将使用它。我们还将使用上面的Machine 类。使用您的打开文件对话框,我们得到要读取的文件的名称。创建一个变量partsList 来保存读取文件时创建的Machine 对象。然后for each 循环遍历列表并将结果显示在表单上的文本框中。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  Dim dialog As OpenFileDialog = New OpenFileDialog
  dialog.Filter = "CSV document(*.csv)|*.csv"
  Dim result As DialogResult = dialog.ShowDialog
  If (result = DialogResult.OK) Then
    Dim csvFile As String = dialog.FileName
    Dim partsList As List(Of Machine) = ReadText(csvFile)
    For Each curMac As Machine In partsList
      TextBox1.AppendText(curMac.ToString() + Environment.NewLine)
    Next
  End If
End Sub

读取csv文件的功能

Private Function ReadText(filePath As String) As List(Of Machine)
  Dim fileReader As System.IO.StreamReader
  Dim data As List(Of Machine) = New List(Of Machine)
  fileReader = My.Computer.FileSystem.OpenTextFileReader(filePath)
  Dim curline As String = ""
  While (Not curline Is Nothing)
    curline = fileReader.ReadLine()
    '' need to check for valid data
    '' if anything is invalid simply ignore it... i.e. your bad rows
    '' keep in mind this will also ignore good rows that have a single piece of data bad
    If (StringOK(curline)) Then
      Dim newMac = GetMac(curline)
      data.Add(newMac)
    End If
  End While
  Return data
End Function

几个辅助函数来验证数据

Private Function StringOK(inString As String) As Boolean
  If (String.IsNullOrEmpty(inString)) Then
    Return False
  End If
  Dim splitArray() As String = inString.Split(",")
  Try
    If ((String.IsNullOrEmpty(splitArray(0))) Or (String.IsNullOrEmpty(splitArray(1)))) Then
      Return False
    End If
    Dim value As Integer
    If ((Not Integer.TryParse(splitArray(2), value)) Or
        (Not Integer.TryParse(splitArray(3), value)) Or
        (Not Integer.TryParse(splitArray(4), value))) Then
      Return False
    End If
    Return True
  Catch ex As Exception
    Return False
  End Try
End Function

Function GetMac(inString As String) As Machine
  Dim splitArray() As String = inString.Split(",")
  Dim value As Integer
  Dim name As String = splitArray(0)
  Dim number As String = splitArray(1)
  Integer.TryParse(splitArray(2), value)
  Dim invIn As Integer = value
  Integer.TryParse(splitArray(3), value)
  Dim invOut As Integer = value
  Integer.TryParse(splitArray(4), value)
  Dim invMis As Integer = value
  Return New Machine(name, number, invIn, invOut, invMis)
End Function

【讨论】:

    【解决方案2】:

    如果您正在尝试完成如何将数据导入数据表,下面是一种快速处理方法。这会将您的整个 csv 放入一个数据表中,然后您可以对其进行逻辑处理并创建您的 xlsx 文件。

    Friend Shared Function GetExcelFile(ByVal strFileName As String, ByVal strPath As String) As DataTable
    
    Try
    
        Dim dt As New DataTable
    
            Dim ConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strPath & ";Extended Properties=""Text;HDR=Yes;FMT=Delimited\"""
            Dim conn As New OleDb.OleDbConnection(ConStr)
            Dim da As New OleDb.OleDbDataAdapter("Select * from " & strFileName, conn)
            da.Fill(dt)
    
        Return dt
    
    Catch ex As Exception
        Return Nothing
    End Try
    
    End Function
    

    【讨论】:

    • 所以一旦它被导入,我就可以以编程方式总计不同的列值?一个人将如何处理诸如第一行中的数据?如果这台愚蠢的机器不添加所有未使用的废话,这会容易得多。
    • 你可以。不过,我会在另一个问题中问这个问题。但作为获取不同值的示例,您可以执行 data : tbl.AsDataView.ToTable(True, "Your Column").Rows.Count
    • JohnG - 有一个很好的观点。如果您唯一要做的是将 csv 文件作为 xlsx 文件。
    • 谢谢大家,感谢所有的帮助和建议。
    • 为什么还要使用按钮。使它成为一个带有批处理文件的控制台应用程序,并让它在 windows 中作为计划任务运行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-29
    • 1970-01-01
    • 1970-01-01
    • 2014-11-12
    • 1970-01-01
    • 2013-07-10
    相关资源
    最近更新 更多