首页 新闻 会员 周边 捐助

访问execl文件出现Could not decrypt file(我的文件根本没有加密,有的文件可以正常运行)

0
悬赏园豆:20 [已解决问题] 解决于 2012-08-02 15:59

下面是我读取excel填充到datatable中的代码..

在网上看了很也没有找到答案,希望博客园高手给个答案

Error: strConn不能正常连接 则抛出Could not decrypt file
    '读取Excel中Sheet名称
    Private Function GetExcelSheetName(ByVal FilePath As String) As DataTable

        Dim FileextensionIndex = FilePath.LastIndexOf(".")
        Dim filetenName = FilePath.Substring(FileextensionIndex, FilePath.Length - FileextensionIndex)

        Dim conn As OleDbConnection = Nothing
        Dim strConn As String
        Try
            If filetenName = ".xls" Then
                strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FilePath + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1;'"
            ElseIf filetenName = ".xlsx" Then

                strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FilePath + ";Extended Properties=Excel 12.0;"
            Else
                txtMsg.Text = "Please choose execl files"


            End If
            ' Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FilePath + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1;'"
            'public static string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;"; --xlsx, use this:
            'Private Const connstring As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" + path + ";Extended Properties=""Excel 8.0;HDR=YES;"""  -xls, use:
            conn = New OleDbConnection(strConn)
            If conn.State = ConnectionState.Closed Then conn.Open()
            Return conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, Nothing, "TABLE"})
        Catch ex As Exception
            Throw ex
        Finally

            If conn Is Nothing Then conn.Close()
            If conn Is Nothing Then conn.Dispose()
        End Try
    End Function

 '读取指定Sheetname的数据
    Private Function GetExcelDataBySheetName(ByVal FilePath As String, ByVal SheetName As String) As DataSet

        Dim conn As OleDbConnection = Nothing
        Try

            Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FilePath + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1;'"
            conn = New OleDbConnection(strConn)
            If conn.State = ConnectionState.Closed Then conn.Open()
            Dim myCommand As OleDbDataAdapter = Nothing
            Dim ds As DataSet = Nothing
            Dim CmdText As String = "select * from [" + SheetName.Replace("'", "") + "]"
            myCommand = New OleDbDataAdapter(CmdText, strConn)
            ds = New DataSet
            myCommand.Fill(ds)
            Return ds

        Catch ex As Exception

            Throw ex

        Finally

            If conn Is Nothing Then conn.Close()
            If conn Is Nothing Then conn.Dispose()
        End Try

    End Function

 

'绑定数据
    Private Sub BindData(ByVal FileName As String)
        '读取sheetName
        Dim dtData As DataTable
        Dim dr As DataRow
        dtSizeLabel.Columns.Add("Size") '添加Size
        dtSizeLabel.Columns.Add("Quantity") '添加Size
        Dim dtSN As DataTable = GetExcelSheetName(FileName) '得到数据表名
        For i As Integer = 0 To dtSN.Rows.Count - 1  '表示循环取表名
            Dim strSheetName As String = dtSN.Rows(i)("TABLE_NAME").ToString().Replace("'", "") '取得表名的方法
            Dim strTableNameHz As String = strSheetName.Substring(strSheetName.Length - 1)
            If (strSheetName.Substring(strSheetName.Length - 1) = "$") Then  '得到后缀名必须是$
                dtData = GetExcelDataBySheetName(FileName, strSheetName).Tables(0) '得到数据
                Dim intColIndex As Integer = 0  '列索引从0开始
              

                For k As Integer = 0 To dtData.Rows.Count - 1 '开始循环表
                    Dim strValue As String = ConvertToString(dtData.Rows(k)(0)) '第i行第0列 找到Size
                    If strValue.IndexOf("Size") > -1 Then  '表示找到这一行
                      
                        For j As Integer = 1 To dtSN.Columns.Count - 1 '取列值

                            If ConvertToString(dtData.Rows(k)(j)) <> "" Then
                                'dtSizeLabel.Columns.Item(j).ColumnName = dtSN.Rows(k)(j).GetType().ToString()  '第k行第0列为Size
                                dr = dtSizeLabel.NewRow '创建行
                                dr("Size") = dtData.Rows(k)(j)
                                dr("Quantity") = dtData.Rows(k + 1)(j)
                                dtSizeLabel.Rows.Add(dr)
                            End If
                        Next
                    End If

                    '---------------------------------
                    'If strValue.IndexOf("Quantity") > -1 Then  '表示找到这一行
                    '    dtSizeLabel.Columns.Add("Quantity") '添加Size
                    '    For j As Integer = 1 To dtSN.Columns.Count - 1 '取列值

                    '        If ConvertToString(dtData.Rows(k)(j)) <> "" Then
                    '            dr2 = dtSizeLabel.NewRow
                    '            dr2("Quantity") = dtData.Rows(k)(j)
                    '            dtSizeLabel.Rows.Add(dr2)

                    '        End If
                    '    Next
                    'End If


                Next


            End If
        Next
        dgvSizeLabel.DataSource = dtSizeLabel
        dgvSizeLabel.DataBind()

    End Sub

KyrieYang的主页 KyrieYang | 菜鸟二级 | 园豆:205
提问于:2012-05-18 15:41
< >
分享
最佳答案
0

估计文件需要密码打开吧

参考 http://www.connectionstrings.com/excel 

收获园豆:20
2012 | 高人七级 |园豆:21645 | 2012-05-18 18:13
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册