在Excel

时间:2015-11-27 12:35:11

标签: excel oledb vb.net-2010 oledbcommand

我正在开发一个VB10应用程序,它将数据记录到MS Access数据库,并将在Excel中生成报告。我建议做的是每当用户点击"生成报告" 按钮时,将打开一个新的Excel文件,并且将在Excel中填充Access文件中的数据。用户可以选择是否保存Excel文件。

如何在每次用户生成报告时编写查询 exData 以填充新的Excel文件?

代码是:

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

    Public Class Form1
        Dim App As New excel.Application ' Data Logging - 19 Nov 15 - Prashant
        Dim WorkBook As excel.Workbook ' Data Logging - 19 Nov 15 - Prashant
        Dim WorkSheet As excel.Worksheet ' Data Logging - 19 Nov 15 - Prashant
        Dim provider As String
        Dim datafile As String
        Dim connString As String
        Dim myConnection As OleDbConnection = New OleDbConnection

    Public Sub ExportToExcelFromAccess()
        provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
        datafile = "C:\Database_Tutorial.accdb"
        connString = provider & datafile
        myConnection.ConnectionString = connString
        myConnection.Open()

    '########## Creates new excel file  - 27 Nov 15 - Prashant ##########
    WorkBook = App.Workbooks.Add()
    WorkSheet = WorkBook.Sheets("Sheet1")
    WorkSheet.Range("A1").Value = "DATE"
    WorkSheet.Range("B1").Value = "TIME"
    WorkSheet.Range("C1").Value = "LCK_DIM"
    WorkSheet.Range("D1").Value = "RET_DIM"
    WorkSheet.Range("I1").Value = "PRT_CD"

    WorkSheet.Range("A1:D1").Font.Bold = True
    WorkSheet.Range("A1:D1").Cells.Interior.Color = Color.LightGray
    WorkSheet.Range("A1:D1").Columns.AutoFit()
    WorkSheet.Activate()
    WorkSheet.Application.ActiveWindow.SplitRow = 1
    WorkSheet.Application.ActiveWindow.FreezePanes = True
    App.Visible = True

    '###### Imports data into excel file  - 27 Nov 15 - Prashant ######
    Dim exData As String
    exData = "select * INTO <<  >> from [Tutorial]"
    Dim ExtractData As New System.Data.OleDb.OleDbCommand(exData, myConnection)
    ExtractData.ExecuteNonQuery()
    myConnection.Close()
End Sub

0 个答案:

没有答案