VB只考虑一个Excel列

时间:2017-09-26 10:17:52

标签: excel vb.net

我有一个如此划分的Excel文件:代码一列,描述一列,价格一列,大小一列。

在VB中,我必须选择每个字段并导出到由Access创建的数据库中。 如果我使用Description列,我遇到了一个问题:VB认为每行只有一行,所以我在每个db的一列中都有完整的描述。

enter image description here

在我上面发布的示例中,我可以尝试更好地解释我所说的内容。我想要的是为每种产品正确报告描述。

这是我写的代码:

Private Sub Importa_XLS(ByVal fileData As String, ByVal dbVuoto As String, ByVal dbDest As String)

    If My.Computer.FileSystem.FileExists(dbDest) Then My.Computer.FileSystem.DeleteFile(dbDest)
    My.Computer.FileSystem.CopyFile(dbVuoto, dbDest)

    Dim capitoli As New cCapitoli
    Dim paragrafi As New cParagrafi
    Dim voci As New cVoci
    Dim total As Integer
    Dim fileStream As FileStream = New FileStream(fileData, FileMode.Open)
    Dim file(fileStream.Length) As Byte

    fileStream.Read(file, 0, fileStream.Length)

    fileStream.Close()

    Dim ExcelEngine As ExcelEngine = New ExcelEngine()
    Dim application As IApplication = ExcelEngine.Excel
    Dim workbook As IWorkbook = application.Workbooks.Open(New MemoryStream(file), ExcelOpenType.Automatic)

    Dim gecc As New Syncfusion.GridExcelConverter.GridExcelConverterControl

    Dim grid As New GridModel
    gecc.ExcelToGrid(fileData, grid.Model)
    Dim r As Integer = 2
    Dim oldCap, oldPar, vett() As String
    Dim capitolo As New cCapitolo
    Dim paragrafo As New cParagrafo

    'For r As Integer = 196 To grid.RowCount = 5549
    For r = 2 To grid.RowCount - 1
        vett = Split(grid(r, 1).Text)
        total = UBound(Split(grid(r, 1).Text, "."))

        If total = 0 Then 'capitolo
            oldCap = capitolo.Cod
            oldPar = paragrafo.Cod
            If Left(vett(0), 1) >= Chr(65) And Left(vett(0), 1) <= Chr(90) Then
                capitolo.Cod = Left(vett(0), 1)
                If capitolo.Cod <> oldCap Then
                    capitoli.Add(capitolo)
                End If
            End If
            If Left(vett(0), 2) >= Chr(65) And Left(vett(0), 2) <= Chr(90) Then
                paragrafo.Cod = Left(vett(0), 2)
                If paragrafo.Cod <> oldPar Then
                    paragrafi.Add(paragrafo)
                End If
            End If
            If grid(r, 2).Text.Length > 255 Then
                capitolo.Descrizione = grid(r, 3).Text.ToString.Substring(0, 252) + "..."
            Else
                capitolo.Descrizione = grid(r, 3).Text.ToString
            End If
            'capitolo.Cod = grid(r, 1).Text.ToString
        End If

        If total = 1 Then 'voce
            Dim voce As New cVoce
            If grid(r, 1).Text.Length > 255 Then
                voce.Descrizione = grid(r, 2).Text.ToString.Substring(0, 252) + "..."
            Else
                voce.Descrizione = grid(r, 2).Text
            End If
            voci.Add(voce)
        End If

        If total = 2 Then 'sottovoce
            Dim sottovoce As New cVoce
            sottovoce.Descrizione = grid(r, 3).Text
            If grid(r, 1).Text.Length > 255 Then
                sottovoce.DescBreve = grid(r, 2).Text.ToString.Substring(0, 252) + "..."
            Else
                sottovoce.DescBreve = grid(r, 2).Text
            End If

            sottovoce.Prezzo1 = grid(r, 3).Text
            sottovoce.Prezzo2 = sottovoce.Prezzo1
            sottovoce.Prezzo3 = sottovoce.Prezzo1
            sottovoce.Prezzo4 = sottovoce.Prezzo1
            sottovoce.UniMi = grid(r, 2).Text
            sottovoce.Separatore = "."
        End If

    Next

    capitoli.Salva_DB(dbDest)
    paragrafi.Salva_DB(dbDest)
    voci.Salva_DB(dbDest)

End Sub


Public Sub Salva_DB(ByVal PathDB As String)

    Dim db As New cDB

    db.connetti_DB(PathDB)
    db.get_rs("DELETE * FROM Capitoli")
    db.get_rs("SELECT * FROM Capitoli")

    Dim rs As ADODB.Recordset = db.RecordSet
    For Each cap As cCapitolo In Me
        rs.AddNew()
        rs("Descrizione").Value = cap.Descrizione
        rs("Cod").Value = cap.Cod
        rs.Update()
    Next

    db.close_DB()

End Sub

 Public Sub Salva_DB(ByVal PathDB As String)

    Dim db As New cDB

    db.connetti_DB(PathDB)
    db.get_rs("DELETE * FROM Paragrafi")
    db.get_rs("SELECT * FROM Paragrafi")

    Dim rs As ADODB.Recordset = db.RecordSet
    For Each par As cParagrafo In Me
        rs.AddNew()

        rs("Cod_Capitolo").Value = par.Cod_Capitolo
        rs("Descrizione").Value = par.Descrizione
        rs("Cod").Value = par.Cod

        rs.Update()
    Next

    db.close_DB()

End Sub

Public Sub Salva_DB(ByVal PathDB As String)

    Dim db As New cDB

    db.connetti_DB(PathDB)
    db.get_rs("DELETE * FROM Voci")
    db.get_rs("SELECT * FROM Voci")

    Dim rs As ADODB.Recordset = db.RecordSet
    For Each v As cVoce In Me
        rs.AddNew()
        rs("Cod_Voce").Value = v.Cod_Voce
        rs("Cod_SottoVoce").Value = v.Cod_SottoVoce
        rs("Cod_Capitolo").Value = v.Cod_Capitolo
        rs("Cod_Paragrafo").Value = v.Cod_Paragrafo
        rs("Cod_SottoParagrafo").Value = v.Cod_SottoParagrafo
        'rs("Articolo").Value = v.Genera_Articolo
        rs("Descrizione").Value = v.Descrizione
        If v.Prezzo1 IsNot Nothing Then
            rs("Prezzo1").Value = Val(v.Prezzo1.Replace(",", "."))
        End If
        If v.Prezzo2 IsNot Nothing Then
            rs("Prezzo2").Value = Val(v.Prezzo2.Replace(",", "."))
        End If
        If v.Prezzo3 IsNot Nothing Then
            rs("Prezzo3").Value = Val(v.Prezzo3.Replace(",", "."))
        End If
        If v.Prezzo4 IsNot Nothing Then
            rs("Prezzo4").Value = Val(v.Prezzo4.Replace(",", "."))
        End If
        rs.Update()
    Next

    db.close_DB()

End Sub

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

 Private Sub btnOpenExcel_Click(sender As Object, e As RoutedEventArgs) Handles btnOpenExcel.Click
    Try
        ocIteam = New ObservableCollection(Of SLSerMain.proTradeItemMaster)
        Dim flDialog As New OpenFileDialog()
        flDialog.Filter = "Excel Files(*.xlsx)|*.xlsx"
        Dim res As Boolean = CBool(flDialog.ShowDialog())
        If res Then
            Dim fs As FileInfo = flDialog.File
            Dim fileName As String = fs.Name
            Dim objExcel As Object = AutomationFactory.CreateObject("Excel.Application")
            'Open the Workbook Here
            Dim objExcelWorkBook As Object = objExcel.Workbooks.Open(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "\" & fileName)
            'Read the Worksheet
            Dim objActiveWorkSheet As Object = objExcelWorkBook.ActiveSheet()
            'Cells to Read

            Dim enumvalues As ItemEntry.ItemColoumn

            Dim ItemName, Department, ItemCategory, Stock, Base, Purchase, Sales, Manufacture, HSNNo, CostRate, MRPRate, SalesRate As Object
            'Iterate through Cells
            For count As Integer = 1 To 158
                ItemName = objActiveWorkSheet.Cells(count, Convert.ToInt32(enumvalues.ItemName))
                Department = objActiveWorkSheet.Cells(count, Convert.ToInt32(enumvalues.Department))
                ItemCategory = objActiveWorkSheet.Cells(count, Convert.ToInt32(enumvalues.ItemCategory))
                Stock = objActiveWorkSheet.Cells(count, Convert.ToInt32(enumvalues.Stock))
                Base = objActiveWorkSheet.Cells(count, Convert.ToInt32(enumvalues.Base))
                Purchase = objActiveWorkSheet.Cells(count, Convert.ToInt32(enumvalues.Purchase))
                Sales = objActiveWorkSheet.Cells(count, Convert.ToInt32(enumvalues.Sales))
                Manufacture = objActiveWorkSheet.Cells(count, Convert.ToInt32(enumvalues.Manufacture))
                HSNNo = objActiveWorkSheet.Cells(count, Convert.ToInt32(enumvalues.HSNNo))
                CostRate = objActiveWorkSheet.Cells(count, Convert.ToInt32(enumvalues.CostRate))
                MRPRate = objActiveWorkSheet.Cells(count, Convert.ToInt32(enumvalues.MRPRate))
                SalesRate = objActiveWorkSheet.Cells(count, Convert.ToInt32(enumvalues.SalesRate))
                Dim departmentID As String = ""
                Dim pStockUnitID As String = ""
                Dim pBaseUnitID As String = ""
                Dim categoryID As String = ""
                Dim manufactureID As String = ""
                Dim pChargeID As String = ""
                Dim pSalesChargeID As String = ""
                Dim pHSNNo As String = ""
                Try
                    departmentID = ocDepartment.Where(Function(x) x.DepartmentName.Contains(Department.value.ToString.Trim)).FirstOrDefault.DepartmentID
                Catch ex As Exception
                End Try
                Try
                    categoryID = ocCategory.Where(Function(x) x.CategoryName.Contains(ItemCategory.value.ToString.Trim)).FirstOrDefault.CategoryID
                Catch ex As Exception
                End Try
                Try
                    pStockUnitID = ocUnit.Where(Function(x) x.UnitID.Contains(Stock.value.ToString.Trim)).FirstOrDefault.UnitID
                Catch ex As Exception
                End Try
                Try
                    pBaseUnitID = ocUnit.Where(Function(x) x.UnitID.Contains(Base.value.ToString.Trim)).FirstOrDefault.UnitID
                Catch ex As Exception
                End Try
                Try
                    manufactureID = ocManufactureMaster.Where(Function(x) x.Name.Contains(Manufacture.value.ToString.Trim)).FirstOrDefault.ManuFacture_ID
                Catch ex As Exception
                End Try
                Try
                    pChargeID = ocCharge.Where(Function(x) x.Name.Contains(Purchase.value.ToString.Trim)).FirstOrDefault.ChargeID
                Catch ex As Exception
                End Try
                Try
                    pSalesChargeID = ocCharge.Where(Function(x) x.Name.Contains(Sales.value.ToString.Trim)).FirstOrDefault.ChargeID
                Catch ex As Exception
                End Try
                Try
                    pHSNNo = HSNNo.value.ToString.Trim
                Catch ex As Exception
                End Try
                ocIteam.Add(New SLSerMain.proTradeItemMaster() _
                    With {
                            .Client_ID = gcClientID,
                            .Property_ID = gcPropertyID,
                            .ItemID = "",
                            .Name = ItemName.Value.ToString.Trim,
                            .SortName = ItemName.Value.ToString.Trim,
                            .DepartmentID = departmentID,
                            .CategoryID = categoryID,
                            .StockUnitID = pStockUnitID,
                            .BaseUnitID = pBaseUnitID,
                            .ChargeID = pChargeID,
                            .SalesChargeID = pSalesChargeID,
                            .MfgID = manufactureID,
                            .OpeningStock = 0.000,
                            .DefaultyQty = 1.0,
                            .Weight = 0.000,
                            .CostRate = Format(Convert.ToDouble(CostRate.value), ".00"),
                            .MrpRate = Format(Convert.ToDouble(MRPRate.value), "00"),
                            .SalesRate = Format(Convert.ToDouble(SalesRate.value), ".00"),
                            .CurrentStock = 0.000,
                            .AskForQuantity = 1,
                            .AskForRate = 1,
                            .IsAllowDecimalQty = 1,
                            .IsAlterPrice = 1,
                            .IsAlterQty = 1,
                            .IsAllowDiscount = 1,
                            .IsReadScal = 0,
                            .IsExpired = 0,
                            .IsVenderImplemented = 0,
                            .IsAllowZeroRate = 0,
                            .IsCustomerImplemented = 0,
                            .MiniStock = 1.0,
                            .MaxStock = 1.0,
                            .MiniOrderQty = 1.0,
                            .MaxOrderQty = 1.0,
                            .IsImported = 1,
                            .ClassCategory = "H",
                            .Movement = "F",
                            .ValMethod = "F",
                            .HSN_NO = pHSNNo
                           }
                         )
            Next count
            dgBillDetails.ItemsSource = ocIteam
            objExcel.Workbooks.close()
        End If
    Catch ex As Exception
    End Try
End Sub

Public Class ItemEntry
    Enum ItemColoumn
        ItemName = 1
        Department = 2
        ItemCategory = 3
        Stock = 4
        Base = 5
        Purchase = 6
        Sales = 7
        Manufacture = 8
        HSNNo = 9
        CostRate = 10
        MRPRate = 11
        SalesRate = 12
    End Enum
End Class

使用此图案