简体   繁体   English

如何将电子表格中单元格的内容输出到txt文件?

[英]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.

相关问题 如何将Excel语句的大小减小为txt文件并跳过没有数据的空单元格 - How can I reduce the size of my statement for Excel to txt file and skip empty cells with no data 将图像放在电子表格(Aspose Cells)上时,如何防止图像尺寸改变? - How can I prevent my image from changing size when placing it on a spreadsheet (Aspose Cells)? 当电子表格 window 最小化时,如何获取我的 Excel VSTO 插件以更新单元格? - How can I get my Excel VSTO Add-in to update cells when the spreadsheet window is minimized? 如何使用EPPlus将电子表格单元格的内容设置为会计格式? - How can I set contents of a spreadsheet cell to Accounting format with EPPlus? 如何在电子表格范围(Aspose Cells)周围添加边框? - How can I add borders around a spreadsheet range (Aspose Cells)? 如何在不提示替换(空)目标单元格(EPPlus)的内容的情况下将图像添加到工作表中? - How can I add an image to my sheet without being prompted to replace contents of the (empty) destination cells (EPPlus)? 如何将2个单元格的内容合并为1个并在Excel中移动其余列? - How can I merge contents of 2 cells into 1 and shift the remaining columns in Excel? 如何从 txt 文件中获取数据并将其发送到 Excel 电子表格?(我只想要 txt 文件中“:”之后的所有内容)? - How do i get data from a txt file and send it to excel spreadsheet?(I only want everything after the “:” in the txt file)? 如何以编程方式填充和获取 Excel 电子表格的 PDF 输出? - How can I fill and get the PDF output of a Excel spreadsheet programmatically? 如何将打印输出转换为csv,然后将其上传到Google Spreadsheet? - How can I convert print output to a csv and then upload it to a Google Spreadsheet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM