vba 将单个 Excel 工作表另存为 CSV

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7331624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:50:55  来源:igfitidea点击:

Save individual Excel sheets as CSV

excelvbaexcel-vba

提问by Marthinus

I need to parse Excel work sheets. Now I save each individual work sheet as .csv and it works great. I use OpenCSV to parse the files etc. but to create those .csv files are a pain.

我需要解析 Excel 工作表。现在我将每个单独的工作表保存为 .csv 并且效果很好。我使用 OpenCSV 来解析文件等,但创建那些 .csv 文件很痛苦。

What would be the easiest and quickest way to save individual work sheets as .csv in Excel? I am assuming some kind of VBA macro would do the job, but since I am not a VBA programmer I have no idea how to do it. Maybe I can just record a macro somehow?

在 Excel 中将单个工作表另存为 .csv 的最简单快捷的方法是什么?我假设某种 VBA 宏可以完成这项工作,但由于我不是 VBA 程序员,我不知道该怎么做。也许我可以以某种方式录制一个宏?

回答by Fionnuala

Very roughly,

很粗略,

Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets
    ws.SaveAs "C:\docs\" & ws.Name & ".csv", xlCSV
Next

This does not include any error coding nor does it make allowances for sheet names that will lead to illegal file names. It all depends on how robust you need the whole thing to be.

这不包括任何错误编码,也不考虑会导致非法文件名的工作表名称。这一切都取决于您需要整个事物的稳健程度。

回答by JMax

As a first answer, here is the code used to save a file to CSV:

作为第一个答案,以下是用于将文件保存为 CSV 的代码:

ActiveWorkbook.SaveAs "C:\Marthinus.csv", fileformat:=6

More info about SaveAs

关于另存为的更多信息