简体   繁体   中英

Importing Spreadsheet + Data manipulation and save to DB

Hi I have a requirement to import spreadsheets - validate data and save to DB.

The spreadsheet data comes from two sources. One can be imported directly (I have no problems with this). The data in the spreadsheet from the second source has to be manipulated before the data is saved to the DB.

I've created a web page which uploads the xls files and then imports to datatable. The datatable is then the data source for a gridview. I then do validation and save to DB.

This code worked great for the standard spreadsheet. I now need to amend the code so that it can cope with either type of source spreadsheet.

Example Data Standard Source

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

Source that needs manipulating

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

In the above example the Ref needs to be manipulated so the the last 4 digits are transfered to the YEAR column and the first part of the Ref up to the second underscore is trimed and the underscore is replaced with / eg XM3M/ZZ. The reference can be anything from 4+2suffix to 6+2suffix but will always be followed by _XXX_XXX_YYYY where X and Y are digits X are not required and YYYY is the year.

My plan was to import the spreadsheet to a temp datatable and the go through that table row by row cell by cell and copy to another datatable after making any alterations neccesary. This second table is then uses as the datasource for the gridview.

Here is my code...

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

End Sub

The trouble is I'm getting errors.... Am I approaching this from the right angle?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM