从Excel导入数据,单元格为

时间:2016-04-01 12:37:00

标签: database vb.net excel datagridview

我正在创建一个数据库,它从excel文件中收集数据,导入过程相当容易,但导入时的单元格颜色占用了太多时间,因为我正在逐个设置单元格的颜色。

有没有办法绕过这个并导入颜色,字体,forecolor等数据作为数据集以及数据表以便在dgv中显示。

我到目前为止的代码是:

obooks = oexcel.ActiveWorkbook
path = obooks.FullName
osheet = obooks.Worksheets(e.Node.Text)
osheet.Activate()
sheetname = "[" + e.Node.Text + "$]"
myconnection = New  System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + " ;Extended Properties='Excel 8.0;HDR=No;IMEX=1'")
    mycommand = New System.Data.OleDb.OleDbDataAdapter("select * from " & sheetname, myconnection)
    dataset = New System.Data.DataSet
    mycommand.Fill(dataset)
    workspace_data.DataSource = dataset.Tables(0)
    'workspace_data.DataSource = databind.
    myconnection.Close()
    For Me.i = 0 To workspace_data.Rows.Count - 1
        For Me.j = 0 To workspace_data.Columns.Count - 1
            With workspace_data.Rows(i).Cells(j)
                .Style.BackColor = ColorTranslator.FromOle(osheet.UsedRange.Cells(i + 1, j + 1).Interior.color)
            End With
        Next
    Next

0 个答案:

没有答案