0

您好我需要导入电子表格 - 验证数据并保存到数据库。

电子表格数据来自两个来源。一个可以直接导入(这个我没问题)。在将数据保存到数据库之前,必须对来自第二个来源的电子表格中的数据进行处理。

我创建了一个网页,它上传 xls 文件,然后导入数据表。然后数据表是网格视图的数据源。然后我进行验证并保存到数据库。

此代码适用于标准电子表格。我现在需要修改代码,以便它可以处理任何一种类型的源电子表格。

示例数据标准源

Unique ID   Ref       PartName           New_SH Year  ProductType   Location  Qty  TopSeller
39564       SD9PP/SS  Micra Water Pump   N      2012  Cooling       CD19       12  Y

需要操纵的来源

Unique ID   Ref                   PartName          New_SH  Year  ProductType   Location    Qty  TopSeller
IS-4564     XM3M_ZZ_001_001_1982  180 wiper motor   S             Wiper Motor   CD19          3  N

在上面的示例中,需要对 Ref 进行操作,以便将最后 4 位数字传输到 YEAR 列,并且修剪 Ref 的第一部分直到第二个下划线,并将下划线替换为 / 例如 XM3M/ZZ。引用可以是从 4+2suffix 到 6+2suffix 的任何内容,但后面总是跟着 _XXX_XXX_YYYY,其中 X 和 Y 是数字 X 不是必需的,YYYY 是年份。

我的计划是将电子表格导入临时数据表,然后逐行逐个单元格地浏览该表,并在进行任何必要的更改后复制到另一个数据表。然后将第二个表用作 gridview 的数据源。

这是我的代码...

Protected Sub ReadExcelFile(filename As String, worksheet As String)

Try
' Fetch Data from Excel data is based on the following
'Unique ID  Ref         PartName        New_SH  Year    ProductType Location    Qty TopSeller
'IS-4564    XM3M_ZZ_001_001_1982    180 wiper motor     S       Wiper Motor CD19            3   N
'35667      SD9PP/SS        Micra water pump    N   2012    Cooling     CA4     14  Y

Dim MyConnection As New OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; data source='" & filename & "'; Extended Properties='Excel 8.0; HDR=Yes; IMEX=1'")
Dim MyCommand As New OleDbDataAdapter("select * from [" & worksheet & "]", MyConnection)
Dim dtPreview As New DataTable()
Dim dtTemp As New DataTable()


    'Place data in temp datatable
    MyCommand.Fill(dtTemp)

    'Go through tem[ datatable and transfer cell values to dtPreview
    If dtTemp.Columns.Count < 9 Then
    'We do not have enough columns
    lb_Error.Text = "Spreadsheet does not contain enough columns
    Exit Sub

    Else
    'We have at least 9 columns - we are only interested in the first 9
    If dtTemp.Rows.Count > 0 Then
        'we have at least one row

        'Add Column Headers
        For c = 0 To dtTemp.Columns.Count - 1
        dtPreview.Columns.Add(dtTemp.Columns(c).ColumnName, dtTemp.Columns(c).DataType)
        Next

        'Add Row Data
        Dim l As Integer = 0
        For Each drTempRow As DataRow In dtTemp.Rows


        Dim newRow As DataRow = dtPreview.NewRow()

        'Unique ID
        If Left(dtTemp.Rows(0).ToString, 3) = "IS-" Then
            newRow(0) = Mid(dtTemp.Columns(0).ToString.ToUpper.Trim, 4, 8)
        Else
            newRow(0) = dtTemp.Columns(0).ToString.ToUpper.Trim
        End If

        'Ref
        If Left(dtTemp.Rows(0).ToString, 3) = "IS-" Then
            l = Len(dtTemp.Columns(1).ToString.Trim)
            newRow(1) = Left(dtTemp.Columns(1).ToString.Replace("_", "/").ToUpper.Trim, l - 13)
        Else
            newRow(1) = dtTemp.Columns(1).ToString.ToUpper.Trim
        End If

        'Part Name
        newRow(2) = dtTemp.Columns(2).ToString.ToUpper.Trim

        'New / Secondhand
        newRow(3) = dtTemp.Columns(3).ToString.ToUpper.Trim

        'Year
        If Left(dtTemp.Columns(0).ToString, 3) = "IS-" Then
            newRow(4) = Right(dtTemp.Columns(0).ToString, 4)
        Else
            newRow(4) = dtTemp.Columns(4).ToString
        End If

        'Product Type
        newRow(5) = dtTemp.Columns(5).ToString.ToUpper.Trim

        'Location
        newRow(6) = dtTemp.Columns(6).ToString.ToUpper.Trim

        'Quantity in Stock
        newRow(7) = dtTemp.Columns(7).ToString

        'Top Seller
        newRow(8) = dtTemp.Columns(8).ToString.ToUpper.Trim

        dtPreview.Rows.Add(newRow)

        newRow = Nothing
        Next

    End If

    'Add additional columns
    dtPreview.Columns.Add("RecdBy", GetType(System.Int16), Session("UserNo").ToString())

    If ddl_FileSource.SelectedValue.ToString() = "1" Then
        dtPreview.Columns.Add("IntExt", GetType(System.Int16), "1")
    Else
        dtPreview.Columns.Add("IntExt", GetType(System.Int16), "0")
    End If

    dtTemp = Nothing

End If

'Bind DataTable to Gridview
Session("dtgv") = dtPreview
gv_StockData.DataSource = dtPreview
gv_StockData.DataBind()
MyConnection.Close()

Catch msg As Exception
lb_Error.Text = msg.Message

End Try

结束子

问题是我遇到了错误......我是否从正确的角度接近这个?

4

1 回答 1

0

排序...循环遍历数据表中每个单元格的代码有缺陷...将在今天晚些时候发布工作版本。

于 2012-09-19T12:33:34.957 回答