MS Access OLEDBConnection to Excel Issue

So I have searched for a about 2 days now and while I can find a lot of examples of how to get an Excel worksheet into a gridView, none of them work for me.

This is the goal: I need to read an Excel file, has one worksheet in it and should always be one worksheet in it, into a GridView in ASP.NET website and I am using VB.Net in code behind file.

I tried one way trying to use the Schema of the table name (To get the sheet names) but no matter what the sheet name was it always came back as 'Algrip' of which there is NO sheet with that name in any of the workbooks I an testing with.

So I scrapped that and am now using the this code: (Which does get the job done, sort of)

    'Setup Variables
    Dim xlConnStr As String = ""
    Dim FileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
    Dim Extension As String = Path.GetExtension(FileUpload1.PostedFile.FileName)
    Dim FolderPath As String = ConfigurationManager.AppSettings("FolderPath")
    Dim sheetname As String = InputBox("Enter Sheetname: ", "Excel Worksheet name")

    'Adjust Sheetname
    sheetname = sheetname + "$"

    'Set Connection based on Excel File Extension
    Select Case Extension
        Case ".xls"
            'Excel  97-03
            xlConnStr = ConfigurationManager.ConnectionStrings("Excel03ConString").ConnectionString
        Case ".xlsx"
            'Excel 07-Forward
            xlConnStr = ConfigurationManager.ConnectionStrings("Excel07ConString").ConnectionString
    End Select

    xlConnStr = String.Format(xlConnStr, FolderPath & FileName)
    Dim connXL As New OleDb.OleDbConnection(xlConnStr)
    pnlFileInfo.Visible = True
    connXL.Open()
    Using xlCmd As New OleDbCommand
        xlCmd.CommandType = CommandType.Text
        xlCmd.CommandText = ("Select * From [" + sheetname + "]")
        xlCmd.Connection = connXL
        Using xlDS As New DataSet()
            Using xlDA As New OleDbDataAdapter(xlCmd)
                xlDA.Fill(xlDS)
                gvExcelFile.DataSource = xlDS
                gvExcelFile.DataBind()
            End Using
        End Using
    End Using
    connXL.Close()

Now my problem is this; On the follwoing line:

xlCmd.CommandText = ("Select * From [" + sheetname + "]")

If I do not have the left and right brackets, because the some sheet names have spaces in them, I get a query error. But if I add the brackets it prompts me twice for the sheet name. For the life of me I can not figure out why.

I have put a break point in on the line listed above and checked the value of the variable sheetname and it is correct but for some reason I get prompted again.

Does anyone have any idea as to why it is doing this? What am I missing? What I ideally wanted was to be able to read the sheet name and feed it to the line with the select statement so that there is no user action required but all I ever got that way was the same bad sheet name 'Algrip'.

The Excel sheet is an xls file but can be saved as xlsx if it would help.

I am open to re-doing the code if i can get it to read the sheet name dynamically.

Thanks for any help!!


尝试将整个命令存储在变量中,然后使用OleDbCommand构造函数将其分配给xlCmd.CommandText

Dim sheetname As String = InputBox("Enter Sheetname: ", "Excel Worksheet name")
sheetname = sheetname & "$"
Dim strQuery As String = "Select * From [" & sheetname & "]"

...

Using xlCmd As New OleDbCommand(strQuery ,connXL)

    Using xlDS As New DataSet()
        Using xlDA As New OleDbDataAdapter(xlCmd)
            xlDA.Fill(xlDS)
            gvExcelFile.DataSource = xlDS
            gvExcelFile.DataBind()
        End Using
    End Using

End Using
connXL.Close()
链接地址: http://www.djcxy.com/p/35754.html

上一篇: 将整个工作表复制到Excel 2010中的新工作表

下一篇: MS Access OLEDBConnection到Excel问题