把excel文件上傳並導入臨時表

導入excel文件
 Dim filepath As String = String.Empty
        Dim filename As String = String.Empty
        Dim filetype As String = String.Empty
        If File1.PostedFile.FileName <> "" Then
            filepath = File1.PostedFile.FileName
            filetype = filepath.Substring(filepath.LastIndexOf(".") + 1)
            filename = filepath.Substring(filepath.LastIndexOf("\") + 1)
            If ("xls" <> filetype) Then
                Me.lb_msg.Text = "只能選擇excel文件!!"
                Exit Sub
            Else
                Try
                    Dim path As String = Server.MapPath("../xml") + "\" + filename
                    If File.Exists("path") Then
                        File.Delete(path.Trim)
                    End If
                    File1.PostedFile.SaveAs(path.Trim)

                    Me.lb_msg.Text = "上傳成功!!"
                    '跳轉到導入數據頁面
                    Session("type") = Me.dr_checktype.SelectedItem.Value.Trim
                    Session("filename") = filename.Trim
                    Session("table_name") = Me.TextBox1.Text.Trim
                    '    Response.Redirect("Excel.aspx", True)first_bar.aspx
                    ' Response.Redirect("first_bar.aspx", True)Excle_to_Temp.aspx
                    Select Case Me.dr_checktype.SelectedValue
                        Case "1"
                            Response.Redirect("outside_temp.aspx", True)
                        Case "2"
                            Response.Redirect("in_temp.aspx", True)
                        Case "3"
                            Response.Redirect("re_temp.aspx", True)
                        Case "4"
                            Response.Redirect("rework_temp.aspx", True)
                        Case "5"
                            Response.Redirect("ship_temp.aspx", True)
                    End Select

                Catch ex As Exception
                    Me.lb_msg.Text = "上傳失敗!!" + ex.Message
                End Try
            End If


        End If

把excel記錄添加到臨時表中
 Function Excel_to_table() As Boolean
        Dim filename As String = String.Empty
        Dim tablename As String = String.Empty
        If Session("filename") Is Nothing Then
            Me.Label1.Text = "請先上傳要上傳數據的文件!!"
            Return False
            Exit Function
            '出錯了
        Else
            filename = Session("filename")
            tablename = Session("table_name")
        End If
        Dim path As String = Server.MapPath("../xml") + "\" + filename
        If File.Exists(path.Trim) Then
        Else
            Me.Label1.Text = "該上傳的文件不存在,請檢查!"
            Return False
            Exit Function
        End If
        Dim connstring As String = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + path.Trim + ";Extended Properties=ExceL 8.0"
        '   Me.Label1.Text = Server.MapPath("xml/A31.xls")
        Dim connection As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection(connstring)
        mysql = "SELECT * FROM [" + tablename.Trim + "$]"
        Dim ds As System.Data.DataSet = New Data.DataSet
        Try
            Dim adp As System.Data.OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter(mysql.Trim, connection)

            ds.Tables.Clear()
            connection.Open()
            adp.Fill(ds, "Book1")

            connection.Close()
            'Me.GridView1.DataSource = ds.Tables("Book1").DefaultView
            'Me.GridView1.DataBind()

        Catch ex As Exception
            connection.Close()
            Me.Label1.Text = "工作表不存在或者工作表名不正確,請檢查!!" + ex.Message
            Return False
            Exit Function
        End Try

        'If ds.Tables("Book1").Rows.Count = 0 Then
        '    Me.Label1.Text = "記錄不存在,可能工作表不存在或者工作表名不正確,請檢查!!"
        '    Return False
        '    Exit Function
        'End If

        Dim i As Int16 '= 0
        For i = 0 To ds.Tables("Book1").Rows.Count - 1

            mysql = " INSERT INTO Temp_Check (the_type ,the_custumer, the_date ,the_pno"

posted @ 2006-09-06 14:59  Nina  阅读(594)  评论(1)    收藏  举报