首先让我解释一下我的情况。
我有一个Windows Server以及PaperCut免费版。输出为“ papercut-print-log-all-time.csv”。该输出始终由该服务打开,每次有人打印输出时,该服务都会计数并记录执行该操作的人以及许多其他信息。
现在,我希望创建某种类型的excel表,每次我打开excel时都会“自动将所有新寄存器添加到新表单中,以制作一些有关用法的图形。
诀窍在哪里?我不想每次打开时都必须下拉公式来手动更新新寄存器。
我想将需要制作某种宏(以前从未做过,但是知道c#和vb),但仍然不知道是否使用动态表和一些棘手的公式可以做到这一点。
“ papercut-print-log-all-time.csv”格式的示例是:
Time User Pages Copies Printer FileName Client PaperFormat Lenguaje Duplex GrayScale Format
11/27/2012 11:29 Mberiguette 2 1 Printer1 Microsoft Office Outlook - Memorando client-pc1 Letter PCL6 NOT DUPLEX GRAYSCALE 120kb
11/27/2012 11:30 mabreu 1 1 Printer1 PDF Print client-pc2 Letter PCL6 NOT DUPLEX GRAYSCALE 58kb
11/27/2012 11:30 mabreu 1 1 Printer1 PrintTest client-pc2 Letter PCL6 NOT DUPLEX GRAYSCALE 21kb
11/27/2012 11:35 mabreu 1 1 Printer2 PrintTest client-pc2 Letter PCL6 NOT DUPLEX GRAYSCALE 21kb
11/27/2012 11:35 Mberiguette 1 2 Printer2 Microsoft Word - Entrega factyras de Proveedor.doc client-pc1 Letter PCL6 NOT DUPLEX GRAYSCALE 51kb
感谢您的支持。
通过在Workbook_Open()
模块中的ThisWorkbook
例程中添加代码,可以在打开工作簿时使VBA例程运行。 (按Alt-F11打开VBA IDE,将代码添加到模块中。)
下面的代码:
Scripting.TextStream
对象以逐行读取CSV文件String
变量中String
分成由逗号分隔的字段,结果存储在Array
中Array
复制到电子表格中Option Explicit
Private Sub Workbook_Open()
Call UpdateFromCSV("your path here\papercut-print-log-all-time.csv")
End Sub
Private Sub UpdateFromCSV(filePath As String)
Dim ts As Object 'Scripting.TextStream
Dim s As String
Dim v As Variant
Dim i As Long
Set ts = CreateObject("Scripting.FileSystemObject").GetFile(filePath).OpenAsTextStream(ForReading, TristateUseDefault)
For i = 1 To Application.ActiveSheet.UsedRange.SpecialCells(xlLastCell).Row
ts.SkipLine
Next i
Do While Not ts.AtEndOfStream
s = ts.ReadLine
v = Split(s, ",")
Application.ActiveSheet.Range(Application.ActiveSheet.Cells(i, 1), Application.ActiveSheet.Cells(i, 12)) = v
i = i + 1
Loop
ts.Close
Set ts = Nothing
End Sub
但是,如果您只希望拥有具有CSV文件中所有值的写访问权的电子表格,则可以尝试执行以下操作。它只是以只读方式打开CSV文件并将其另存为xlsx文件。
Option Explicit
Public Sub OpenSaveCSVasXlsx()
Dim wb As Workbook
Set wb = Application.Workbooks.Open("your path here\papercut-print-log-all-time.csv", False, True, 2, , , True, , , , False, , False)
wb.SaveAs "your path here\papercut-print-log-all-time.xlsx", xlOpenXMLWorkbook, , , , , , xlUserResolution
Set wb = Nothing
End Sub
您可以详细了解Scripting.TextStream
对象here,Workbooks.Open
方法here和Workbook.SaveAs
方法here。