简体   繁体   English

如何将Excel工作簿中的所有工作表另存为一个文本文件?

[英]How to save all worksheets in Excel workbook as one text file?

I have an Excell workbook with multiple worksheets. 我有一个带有多个工作表的Excell工作簿。 I want to export the text of all worksheets to one text file. 我想将所有工作表的文本导出到一个文本文件。 I've seen multiple solutions (primarily using VBA - example below) to export each worksheet as its own text file, but have yet to find a solution to export all worksheets as one text file. 我已经看到了多种解决方案(主要使用VBA-下面的示例)将每个工作表导出为自己的文本文件,但是还没有找到一种将所有工作表导出为一个文本文件的解决方案。 Any ideas as to how to accomplish this using VBA (or perhaps Python)? 关于如何使用VBA(或Python)完成此操作的任何想法?

Sub ExportSheetsToText()
Dim xWs As Worksheet
Dim xTextFile As String
For Each xWs In Application.ActiveWorkbook.Worksheets
    xWs.Copy
    xTextFile = CurDir & "\" & xWs.Name & ".txt"
    Application.ActiveWorkbook.SaveAs Filename:=xTextFile, FileFormat:=xlText
    Application.ActiveWorkbook.Saved = True
    Application.ActiveWorkbook.Close
Next
End Sub

After you generate those text files, you can easily merge many text files into one using the Windows Command Prompt . 生成这些文本文件后,您可以使用Windows Command Prompt轻松地将许多文本文件合并为一个。 Suppose for instance that all your file name have some pattern in their names. 例如,假设您的所有文件名的名称都有某种模式。 ie *.txt but I recommend you give them some additional pattern in their name, such as temp_something.txt: *.txt但我建议您给它们添加一些其他名称,例如temp_something.txt:

xTextFile = CurDir & "\temp_" & xWs.Name & ".txt"

Then you merge them into one text file ine the command prompt like this: 然后将它们合并为一个文本文件,并在命令提示符下显示如下:

type temp_*.txt >> mergedFile.txt

or 要么

copy temp_*.txt mergedFile.txt

If you want to fully automate the operation inside the VBA routine, you can integrate the command inside it, like so: 如果要完全自动化VBA例程中的操作,可以将命令集成到其中,如下所示:

For Each xWs In Application.ActiveWorkbook.Worksheets
    xWs.Copy
    xTextFile = CurDir & "\temp_" & xWs.Name & ".txt"
    Application.ActiveWorkbook.SaveAs Filename:=xTextFile, FileFormat:=xlText
    Application.ActiveWorkbook.Saved = True
    Application.ActiveWorkbook.Close
Next

Dim shl As Object: Set shl = CreateObject("WScript.shell")
shl.CurrentDirectory = CurDir
shl.Run "cmd /C copy temp_*.txt " & ThisWorkbook.name & ".txt" ' merge the temporary text files

shl.Run "cmd /C del temp_*.txt" ' clean up the temporary text files

At the end you get a text file with the same name of you workbook plus the extension .txt . 最后,您将获得一个与您的工作簿同名的文本文件,以及扩展名.txt

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 Excel 工作表保存在单个工作簿中作为工作簿及其数据 - How can I save Excel worksheets in a single workbook as workbooks with their data 将 Excel 工作簿中的所有工作表转换为 csv 格式 - Converting all worksheets in an Excel workbook to csv format Python: How to copy Excel worksheet from multiple Excel files to one Excel file that contains all the worksheets from other Excel files - Python: How to copy Excel worksheet from multiple Excel files to one Excel file that contains all the worksheets from other Excel files Python:如何读取多个文件夹中的所有文本文件内容并将其保存到一个excel文件中 - Python: How to read all text file contents in multiple folders and save them into one excel file 如何使用 python 将 Excel 文件中的所有工作表合并或合并到一个工作表中? - How can combine or merge all worksheets within an Excel file into one worksheet using python? 如何将多个 excel 工作簿组合成具有多个工作表的单个工作簿 - How to combine multi excel workbook into single workbook with multiple worksheets 如何使用 Xlwings 和 Python 合并同一个 Excel 工作簿中的所有工作表 - How to Merge all worksheets within the same excel workbook using Xlwings & Python 如何在for循环中编写同一工作簿中的单独Excel工作表? - How to write to separate excel worksheets within the same workbook in a for loop? 如何调用文件夹中的所有 excel 文件并逐个运行 python 并将结果保存在一个 excel 文件中? - How to call all the excel file in a folder and run through the pythons one by one and have the results save in one excel file? Python/Openpyxl:将工作簿中所有 excel 工作表中的单元格 A1 更改为等于工作表选项卡的名称 - Python/Openpyxl: change cell A1 in all excel worksheets within a workbook to equal the name of the worksheet tab
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM