How do I programatically interface an Excel spreadsheet?

后端 未结 6 819
半阙折子戏
半阙折子戏 2021-01-13 15:53

I have a request for some contract work from an organization that uses Excel as a database and wants to do some work on the Excel data via a real database. (Yeah, I know, ne

6条回答
  •  孤独总比滥情好
    2021-01-13 16:43

    You don't specify a language, so if you are language agnostic .Net gives you some very powerful classes for data handling:

    to open a csv file:

    Imports System.Data.OleDb, Imports Excel = Microsoft.Office.Interop.Excel

        Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DataFolder + "\;Extended Properties='text;HDR=Yes'"
    
        Dim conn As New System.Data.OleDb.OleDbConnection(ConnectionString)
        conn.Open()
    
        Dim CommandText As String = CommandText = "select * from [" + CSVFileName + "]"
        If Filter.Length > 0 Then
            CommandText += " WHERE " + Filter
        End If
    
        Dim daAsset As New OleDbDataAdapter(CommandText, conn)
        Dim dsAsset As New DataSet
        daAsset.Fill(dsAsset, "Asset")
    

    opening a sheet in a workbook is very similar - you specify the sheet name and can then fill a DataSet with the entire sheet - you can then access the Tables().Rows() of the DataSet to get each row and field, iterate over every row etc.

提交回复
热议问题