0

わかりましたので、Excel ファイルから特定のセルを取得し、datagridview 列に入力するデータセットがあります。ただし、私が引っ張っているセルは、datagridview の通常の列ではなくヘッダーである必要があります。この列のデータをヘッダー テキストに変換する簡単な方法はありますか? 詳細を説明するために、コメントを含む以下のコードを提供しました。

' The following lines specify the exact cells I with to pull from the excel file and populates the first column of the datagridview

MyCommand1 = New OleDbDataAdapter("Select * from [myWorksheet$A15:B21]", MyConnection)


'Here is my dataset'
    ds1 = New System.Data.DataSet()
DataGridView1.DataSource = ds1.Tables(0).DefaultView
'So at this point I have a datagridview with a column of data from the exact cells 
' from the excel file that I want


'This last part is code I found on MSDN which will hide the column headers and will turn the first column into headertext. Essentially it is adding an additional column to the left and turning that into headertext. 

Private Sub DataGridView8_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DataGridView8.CellPainting
    Dim rowNumber As Integer = 1

    For Each row As DataGridViewRow In DataGridView8.Rows
        If row.IsNewRow Then Continue For
        row.HeaderCell.Value = "Row " & rowNumber
        rowNumber = rowNumber + 1
    Next
    DataGridView8.AutoResizeRowHeadersWidth( _
        DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders)
End Sub

'If anyone can find out a way for me to make the first column headertext it would make my day. 
4

3 に答える 3

0

確かではありませんが、接続文字列を変更するとうまくいくかもしれません。

connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Test.xlsx;Extended Properties=" + Convert.ToChar(34).ToString() + "Excel 12.0;HDR=Yes;IMEX=2" + Convert.ToChar(34).ToString() + ""

上記のコードでHDRは、ヘッダーを表しています。HDR = Yes最初の行をヘッダーとして表示し、最初HDR = Noの行を通常の行として表示するように設定します。

詳細情報は、asp.net で MS Excel からデータを取得できます。

于 2013-06-13T16:27:43.497 に答える