访问表数据到Excel

我有一个问题,我卡住了。

我想将我的Access表导出到Excel文件。 目前,我使用DoCmd.TransferSpreadsheet来做这DoCmd.TransferSpreadsheet ,但是我想要对导出的数据进行一些格式化。 我可以格式化我发送给Excel的数据吗?还是必须在Excel中编写一个macros,将数据从Access导出后格式化?

Excelmacros从您的MS Access数据库中检索数据:

 Sub Makro1() '' Const sDB = "c:\db1.mdb" Const sSQL = "SELECT * FROM Table1" With ActiveSheet.QueryTables.Add(Connection:= _ "ODBC;DSN=MS Access-database;DBQ=" + sDB + ";FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;" _ , Destination:=Range("A1")) .CommandText = Array(sSQL) .Name = "Query1" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .BackgroundQuery = True .RefreshStyle = xlInsertDeleteCells .SavePassword = True .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .PreserveColumnInfo = True .Refresh BackgroundQuery:=False End With End Sub 

我们有一个通用的function,将我们的连续forms直接导出到Excel。 您需要在可用的工具中添加Microsoft Excel Library以使其可以从您的代码中运行。 这个function可以让你的所有表单都可以使用。 如果你想从表中直接导出,你可以很容易地适应它。

 Public Function ExportToExcel(x_frm as Form) Dim ctl As Control, _ xlApp As Excel.Application, _ xlBook As Excel.Workbook, _ xlSheet As Excel.Worksheet, _ columnName() As String, _ columnCount as Integer 'suppose Excel is not opened. You can add an extra control for that' Set xlApp = CreateObject("Excel.Application") 'create an Excel workbook, declare the first sheet' Set xlBook = xlApp.Workbooks.Add Set xlSheet = xlBook.Worksheets(1) xlApp.Visible = False columnCount = fc().section(0).Controls.Count 'use array columnName() to collect the column names of detail section in tabbed order' ReDim columnName(columnCount) For Each ctl In fc().section(0).Controls columnName(ctl.TabIndex) = ctl.Name Next ctl 'This function will add a title to the excel sheet with my favorite formating' 'I can for example decide this title to be bold/Arial/16 on cell(B2)' addTitleToExcelSheet xlSheet, x_frm.Name 'This function will add the column names to my Excel sheet with my favorite formating' 'for example column names will be added on row 4, columns B to B + columnCount' addColumnNameToExcelSheet xlSheet, columnName() 'This function will add the column values to my Excel sheet with specific format (date, number, etc)' 'Data will be added to the range defined by ' 'row 5 to row 5 + x_frm.recordset.recordcount, ' 'columns B to B + columnCount.' 'Recordset values will have to be read according to tab order' 'exported data can depend on recordset filter and orderBy property: your choice' addColumnValueToExcelSheet xlSheet, columnName(), x_frm.Recordset 'The Excel sheet is made visible and saved under the current folder with the forms name' xlApp.Visible = True xlBook.SaveAs x_frm.Name & ".xls" Set xlBook = Nothing Set xlSheet = Nothing Set xlApp = Nothing End Function 

这是结果的一个视图。 左边是Access表格,右边是Excel导出到Excel的结果。 希望你喜欢。

替代文字

我对这个域没有任何经验,但请注意,Excel的行数限制比Access小。

从预先configuration格式的Excel电子表格开始,可能会更容易一些。 从excel中使用VBA也可能更容易,并从Access(并格式化)而不是从Access推送数据。 Excel VBA会更好地为您做Excel格式的准备。 其中一个就像另一个一样简单。 如果你想单独使用Mac来完成这个工作,使用Excelmacros比使用Accessmacros可能更容易。

如果使用DoCmd.TransferSpreadsheet并创build一个原始文件,然后对其进行编辑以使格式正确,则可以再次运行DoCmd.TransferSpreadsheet ,并使用值更新该文件,但保留格式。

但是,如果人类通过添加新选项卡或添加计算等来编辑文件,那么DoCmd.TransferSpreadsheet将不再工作,并将失败并显示一个丑陋的错误消息。 因此,我们在环境中执行的操作是将DoCmd.TransferSpreadsheet转换为格式化的原始文件,然后将该文件复制到用户桌面,然后打开该副本,以便用户不会打扰原始文件。

这种方法是一个最小的代码,干净,易于维护的解决scheme。 但它确实需要一个额外的“源”或原始文件挂起。 在Access 2007中工作。