[英]How can I output the contents of cells in my spreadsheet to a txt file?
I have a spreadsheet looking like this with three columns A, B and C: 我有一个像这样的电子表格,其中包含三列A,B和C:
1 41309.5 の
2 23509.54 に
3 22216.8 は
What I need to do is to create a text file where each line of the file looks like this: 我需要做的是创建一个文本文件,其中文件的每一行如下所示:
Insert into freqleeds (id, freq, text) values ( 1, 41309.5, 'の')
..
Can someone give me some idea as to how I can do this? 有人可以给我一些有关如何执行此操作的想法吗?
Without using VBA or even complex functions, you can create a support column at column D, with the following formula that will Concatenate()
your template with the variables: 无需使用VBA或什至不使用复杂的函数,您就可以在D列创建一个支持列,并使用以下公式将模板与变量进行
Concatenate()
:
="Insert into freqleeds (id, freq, text) values (" & A1 & "," & B1 & "," & C1 & ")"
After pasting this formula in your D1 cell, you can drag it down, to create the specific db statements for each row. 在将此公式粘贴到D1单元格中之后,可以将其向下拖动,以为每一行创建特定的db语句。
After that, you should select the whole D column and copy it: 之后,您应该选择整个D列并复制它:
Then you should paste the content into a notepad. 然后,您应该将内容粘贴到记事本中。
Done. 完成。
Ps: If you want to automate this job, you can create a macro to apply this concatenate function to each line with content in your spreadsheet, filter them according to your needs and export this specific column to a txt file . 附:如果要自动执行此作业,可以创建一个宏,以将此连接功能应用于电子表格中包含内容的每一行,根据需要对其进行过滤,然后将此特定列导出到txt文件 。
Try: 尝试:
Sub test()
Dim Ws As Worksheet, i As Long, Fr As Long, Lr As Long, St0 As String, St As String
Set Ws = ActiveSheet
St0 = "Insert into freqleeds (id, freq, text) values ( "
Fr = 1 'First row
Lr = Ws.Cells(Ws.Rows.Count, "A").End(xlUp).Row
For i = Fr To Lr
St = St & St0 & Ws.Cells(i, 1) & ", " & Ws.Cells(i, 2) & ", '" & Ws.Cells(i, 3) & "')" & vbNewLine
Next
With CreateObject("Scripting.FileSystemObject")
.CreateTextFile("d:\00000.txt", True, True).Write St ' change the file name
End With
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.