CSV到XLSX VB.NET

时间:2017-01-09 15:55:23

标签: excel vb.net winforms csv

enter image description here enter image description here我正在学习VB.NET,不幸的是我已经完成了一些我不知道怎么做的任务。

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

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

CSV文件将有5个标题,“Line,Component,Picked,Placed和Missed”。我们在第2列中有部件编号放在Component下面。我从权力的角度理解,这个文件总和了零件号,即0-5490045和JUKI 3号线,以及拾取,放置和未接部分的总和。我在下面提供了一个示例行。第一行是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

2 个答案:

答案 0 :(得分:1)

我讨厌提出建议,而不是说明它是如何运作的。下面是一个使用名为Machine的自定义对象来保存数据的示例。此类是对象的最小值,仅用作示例以帮助您入门。它有一些字段,在循环通过列表进行计算时会派上用场。它也可以在这里添加一些自定义函数/ subs来帮助处理涉及“机器”对象的任务。此外,您还可以添加一些比较函数,这些函数可以让您对其他内容进行排序。将所有这些放在一起后,您应该得到一个有效的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

答案 1 :(得分:0)

如果您正在尝试完成如何将数据导入下面的数据表,则可以快速处理该数据。这将把你的整个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