vb.net 将 CSV 转换为 XLS 的脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17931293/
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-17 14:26:49  来源:igfitidea点击:

Script to convert CSV to XLS

vb.netexcelvbscriptssis

提问by Wenceslao Ponce

Have a need to automate conversion of CSV files to XLS. The CSV files (which contain text qualifiers) will be automatically downloaded from a SFTP site and pushed to fixed width files for AS400 integration. The customer currently has someone manually log into SFTP site, download file(s), open in Excel and save as XLS. They have no control of original format or naming so the script must be able to convert files regardless of file name and/or number of files. Am looking to automate the manual process or remove text qualifiers to help manage column mapping discrepancy. Any advice would be greatly appreciated!

需要自动将 CSV 文件转换为 XLS。CSV 文件(包含文本限定符)将自动从 SFTP 站点下载并推送到固定宽度的文件以进行 AS400 集成。客户目前有人手动登录 SFTP 站点,下载文件,在 Excel 中打开并另存为 XLS。它们无法控制原始格式或命名,因此无论文件名和/或文件数量如何,脚本都必须能够转换文件。我正在寻求自动化手动过程或删除文本限定符以帮助管理列映射差异。任何建议将不胜感激!

The customer uses SSIS as their development tool. I apologize if the post is not clear. Am a newbie. Thanks!

客户使用 SSIS 作为他们的开发工具。如果帖子不清楚,我深表歉意。我是新手。谢谢!

回答by ARich

Looks like you're looking for something like this:

看起来你正在寻找这样的东西:

Sub SaveAsXLSX()

Dim wbSource As Workbook
Dim vrtSelectedItem As Variant

'Allows you to pick the CSV file from wherever it's been saved.
With Application.FileDialog(msoFileDialogOpen)
    .InitialFileName = "C:\Users\YourUsername\Documents" & "\"
    .AllowMultiSelect = False
    .Show
        For Each vrtSelectedItem In .SelectedItems
            Set wbSource = Workbooks.Open(vrtSelectedItem)
        Next
End With

'Saves the file as an .xlsx file.
wbSource.SaveAs Filename:="Random Name.xlsx", FileFormat:=51

End Sub

Just remember to change the .InitialFileNameto the file path of wherever you're saving these downloads.

请记住.InitialFileName将保存这些下载的位置更改为文件路径。