我正在尝试编写程序的最后一部分,我需要从 Access 文档中提取数据并将其打印到新的工作簿中。
首先,我将获取产品供应商的名称并创建一个包含每个供应商名称的工作表,然后我想遍历每张表并打印来自每个供应商的订购产品。
我真的很纠结如何打开一个新的工作簿并在我的信息中打印。
由于我之前的答案已被删除(被认为“不足”),我必须提供一个更好的答案。
如果要将数据从 Access 输出到 Excel,则必须按照以下步骤操作:
我将重点放在数据输出上,将格式化省略(数据部分是复杂的......格式化很容易)
首先,您需要在 Access 文件中启用 Excel 对象:工具菜单 > 参考。找到Microsoft Excel 12.0 对象库并激活复选框。现在您可以使用完整的 Excel 库了 :-)
现在是处理数据的时候了。我假设您需要创建一个新工作簿:
public sub createExcelFile()
dim XL as Excel.Application, WB as Excel.Workbook, WKS as Excel.Worksheet
dim db as DAO.database, rec as DAO.recordset, f as DAO.field
dim i as integer, j as integer
' Prepare your Excel stuff
Set XL = new Excel.Application
XL.Visible = True
Set WB = XL.Workbooks.Add
WB.Activate
Set WKS = WB.ActiveSheet ' Default: The first sheet in the newly created book
' Read your data here
set db = currentdb()
set rec = db.openrecordset("tblSampleData")
' A simple table that will show the data from rec
' i and j will be the coordiantes of the active cell in your worksheet
with rec
.movefirst
' The table headers
i = 1
j = 1
for each f in .fields
WKS.cells(i,j).value = f.name
j = j + 1
next f
' The table data
do
i = i+1
j = 1
for each f in .Fields
WKS.cells(i,j).value = f.value
j = j+1
next f
.moveNext
loop until .EOF
end with
end sub
如果要格式化单元格,可以使用WKS.cells(i,j)
(或WKS.range(...)
)属性。
看看我之前留下的链接(Siddarth Rout 很乐意将其移至评论)。
我希望这可以帮助你
Option Compare Database
Public Function format(filepath, sheetname)
Set xls = CreateObject("EXCEL.APPLICATION")
xls.screenupdating = False
xls.displayalerts = False
xls.Visible = True
xls.workbooks.Open filepath
Set xlsdd = xls.ActiveWorkbook
'删除标题
xls.Range("1:1").Select
xls.Selection.Delete Shift:=xlUp
'添加一列
xls.Columns("A:A").Select
xls.Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
'adding 5 rows
'ActiveWorkbook.Sheets("sheet1").Select
xls.Rows("1:5").Insert Shift:=xlDown
'从access中取出行并放入excel
strsql = "select top 5 " & sheetname & ".* into top5_records from " & sheetname
DoCmd.RunSQL strsql
outputFileName = "C:\Users\hp\Desktop\top5_records.xls"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "top5_records", outputFileName, True
'然后打开那个excel并复制行
Set xls2 = CreateObject("EXCEL.APPLICATION")
xls2.screenupdating = False
xls2.displayalerts = False
xls2.Visible = True
xls2.workbooks.Open outputFileName
Set xlsdd2 = xls.ActiveWorkbook
xls2.Rows("1:5").Select
xls2.Selection.Copy
xls.Cells(1, 1).Select
xls.activesheet.Paste
' Dim currdb As DAO.Database
' Dim rst As DAO.Recordset
'
' Set currdb = CurrentDb
' Set rst = currdb.OpenRecordset(strsql) '<<<Opens query recordset via DAO
' rst.MoveLast
' rowsToReturn = rst.RecordCount
' Set rng = xls.Cells(1, 1)
' 'copy specified number of records to worksheet
'
'rng.CopyFromRecordset rst, rowsToReturn '<<<Gets all records in recordset
'使第 6 行加粗
xls.Rows("6:6").Select
With xls.Selection.Font
.Bold = True
.Name = "Arial"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
End With
'自动拟合数据
xls.Sheets(sheetname).Cells.Columns.autofit
xls.CutCopyMode = False
With xlsdd
.Save
.Close
End With
xls.Visible = False
Set xlsdd = Nothing
Set xls = Nothing
End Function
你没有提供很多细节,所以我不能给你很多细节作为回报。但我会这样做:
现在 VBA 很简单
ThisWorkbook.RefreshAll
您将拥有一张可以更改供应商的表格,而不是每个供应商一张。这是您需要的技能
您应该能够找到所有这些的详细信息,但如果没有,请发布另一个问题。
您可以将列/行宽度定义为静态像素量或自动调整,例如粗体是预定义的
例子
Selection.Font.Bold = True
您还可以制作模板电子表格,将内容复制到模板中并另存为。您的帖子并未说明实际需要进行多少格式化。