在 Windows 中使用命令行将 CSV 转换为 Excel(XLS 或 XLSX)
Windows 11/10中的命令行(convert CSV to XLS or XLSX using the command-line) 界面(interface)将 CSV 转换为 XLS 或 XLSX的教程。有多种方法可以将CSV文件转换为Excel格式(XLS、XLSX)。您可以使用Microsoft Excel、专用的免费转换器或在线工具来执行转换。但是,您知道您还可以使用Windows命令行界面将CVS转换为Excel吗?而且,您甚至不需要任何第三方工具吗?如果没有,本教程会让您感到惊喜。
在本文中,我们将向您展示如何将CSV文件转换为Excel工作簿。为此,我们将使用VBScript。让我们查看转换所需的脚本和步骤。
如何使用命令(Command)行将CSV转换为Excel
以下是在Windows 11或 Windows 10中使用命令行界面将(Command Line Interface)CSV转换为Excel的步骤:(Excel)
- 打开记事本。
- 编写一个 Visual Basic 脚本 (VBS)。
- 保存 VBS 文件。
- 在文件夹中打开命令提示符。
- 输入(Enter)CSV到Excel转换命令。
让我们详细讨论这些步骤!
首先,在 Windows 10 PC 上打开记事本(Notepad)应用程序。您现在必须编写带有相关命令的VBScript 。以下(Below)是您需要在记事本(Notepad)中输入的完整脚本;只需从此处复制并将其粘贴到记事本(Notepad)中:
'======================================
' Convert CSV to Excel
'
' arg1: source - CSV path\file
' arg2: target - Excel path\file
'======================================
srccsvfile = Wscript.Arguments(0)
tgtxlsfile = Wscript.Arguments(1)
'Create Spreadsheet
'Look for an existing Excel instance.
On Error Resume Next ' Turn on the error handling flag
Set objExcel = GetObject(,"Excel.Application")
'If not found, create a new instance.
If Err.Number = 429 Then '> 0
Set objExcel = CreateObject("Excel.Application")
End If
objExcel.Visible = false
objExcel.displayalerts=false
'Import CSV into Spreadsheet
Set objWorkbook = objExcel.Workbooks.open(srccsvfile)
Set objWorksheet1 = objWorkbook.Worksheets(1)
'Adjust width of columns
Set objRange = objWorksheet1.UsedRange
objRange.EntireColumn.Autofit()
'This code could be used to AutoFit a select number of columns
'For intColumns = 1 To 17
' objExcel.Columns(intColumns).AutoFit()
'Next
'Make Headings Bold
objExcel.Rows(1).Font.Bold = TRUE
'Freeze header row
With objExcel.ActiveWindow
.SplitColumn = 0
.SplitRow = 1
End With
objExcel.ActiveWindow.FreezePanes = True
'Add Data Filters to Heading Row
objExcel.Rows(1).AutoFilter
'set header row gray
objExcel.Rows(1).Interior.ColorIndex = 15
'-0.249977111117893
'Save Spreadsheet, 51 = Excel 2007-2010
objWorksheet1.SaveAs tgtxlsfile, 51
'Release Lock on Spreadsheet
objExcel.Quit()
Set objWorksheet1 = Nothing
Set objWorkbook = Nothing
Set ObjExcel = Nothing
输入上述脚本后,需要以.vbs文件扩展名保存文件。为此,请单击“File > Save As”选项。在“另存为(Save As)”对话框提示中,将“另存为类型(Save As Type)”设置为“所有文件”(All Files),然后输入文件名,后跟.vbs文件扩展名。之后,单击“保存(Save)”按钮以保存VBS文件。

现在,转到保存上述VBS脚本的文件夹并在此文件夹中打开命令提示符(open Command Prompt in this folder)。
接下来,您必须在CMD(CMD)中输入具有以下语法的命令:
VBSFilename [Input CSV file with path] [Output Excel file with path]
假设您已将VBS文件命名为“csv-to-excel”,那么您的命令将如下所示:
csv-to-excel C:\Users\KOMAL\Desktop\twc.csv C:\Users\KOMAL\Desktop\twc1.xlsx
如果您想使用旧文件扩展名(即XLS )创建(XLS)Excel文件,只需将xlsx替换为xls。这是一个例子:
csv-to-excel C:\Users\KOMAL\Desktop\twc.csv C:\Users\KOMAL\Desktop\twc1.xls
请记住,您必须提及输入CSV和输出Excel文件的完整路径。

当您输入上述命令时,您将获得输出Excel文件。

在上面的VBScript中,有调整列宽、冻结窗格、应用过滤器、使标题加粗等选项。您可以轻松地调整VBScript以自定义这些选项,然后通过CMD运行将(CMD)CSV转换为Excel的命令。如果您是程序员,您可以相应地进一步自定义脚本。
因此,这就是您可以在 Jeff 创建的(created by Jeff)VBScript的帮助下使用命令行将(Command-Line)CSV转换为 Excel的方法。
相关阅读:(Related reads:)
- 如何在 Windows 中将 DBF 转换为 Excel(How to convert DBF to Excel in Windows)
- How to convert a Text (TXT/CSV) file into an Excel file
- 免费批量 PDF 到 Excel 转换器软件。(Free Batch PDF to Excel Converter Software.)
Related posts
DISKPART命令的列表以及如何在Windows 11/10使用它们
如何使用Command-line使用FFmpeg在Windows 10中调整Video
如何在Windows 11/10 Task Manager中显示Command Line
如何在Windows 11/10中使用FINDSTR and Select-String Commands
Access FTP Server在Windows 10中使用Command Prompt
如何在Windows 11/10中使用Command Prompt恢复Registry
如何在 Excel 中冻结行和列
Windows 10上的Outlook Command Line Switches
如何使用 PowerShell 或命令行列出所有 Windows 服务
在 Excel 中将文本转换为数字的 5 种方法
Microsoft Excel在Windows 10运行时导致High CPU usage
如何在 Microsoft Excel 中创建箱线图
Convert CSV到GPX or KML使用Windows 11/10免费Converter工具
如何在Windows 10 PC上打开TSV文件
如何使用命令行杀死程序
如何删除、破解或破解忘记的 Excel XLS 密码
如何在 Microsoft Excel 中从单元格中删除值但保留公式
修复 Windows 10 中的 Excel stdole32.tlb 错误
如何在Windows Command Prompt上打开Auto Complete
如何将 CSV 或 TSV 插入 Excel 工作表
