将工作表导出为 csv

Posted

技术标签:

【中文标题】将工作表导出为 csv【英文标题】:Exporting sheet as csv 【发布时间】:2020-11-16 16:48:33 【问题描述】:

我正在尝试将我的 Excel 文件中的工作表导出为 csv。

我收到了错误

对象工作簿的方法 SaveAs 失败`

在我的SaveAs 线上。

我注意到这段代码创建了一个新工作簿,它有几个空白的默认工作表标签,这会导致问题吗?

Public Sub ExportWorksheetAndSaveAsCSV()
    
    Dim wbkExport As Workbook
    Dim shtToExport As Worksheet
    
    Set shtToExport = ThisWorkbook.Worksheets("Load check data")     'Sheet to export as CSV
    Set wbkExport = Application.Workbooks.Add
    shtToExport.Copy Before:=wbkExport.Worksheets(wbkExport.Worksheets.Count)
    Application.DisplayAlerts = False 'Possibly overwrite without asking
    Path = ThisWorkbook.Sheets("Input").Range("B15") & "estload_" & ThisWorkbook.Sheets("Input").Range("F1") & ".csv"
    Debug.Print Path
    wbkExport.SaveAs Filename:=Path, FileFormat:=xlCSV, CreateBackup:=True
    Application.DisplayAlerts = True
    wbkExport.Close SaveChanges:=False
    
End Sub

编辑以排除 WbkExport 变量,替换为 ThisWorkbook 和 ActiveWorkbook。

Public Sub ExportWorksheetAndSaveAsCSV()
    
    Dim wbkExport As Workbook
    Dim shtToExport As Worksheet
    Dim Path As String
    
    Set shtToExport = ThisWorkbook.Worksheets("Load check data")     'Sheet to export as CSV
    'Set wbkExport = Application.Workbooks.Add
    shtToExport.Copy Before:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
    Application.DisplayAlerts = False 'Possibly overwrite without asking
    Path = ThisWorkbook.Sheets("Input").Range("B15") & "estload_" & ThisWorkbook.Sheets("Input").Range("F1") & ".csv"
    Debug.Print Path
    ActiveSheet.SaveAs Filename:=Path, FileFormat:=xlCSV, CreateBackup:=True
    Application.DisplayAlerts = True
    'wbkExport.Close SaveChanges:=False

【问题讨论】:

旁注:您不需要实际创建工作簿。不带参数的shtToExport.Copy 将创建一个新工作簿,即ActiveWorkbook 所以你是说我可以消除这条线并得到相同的结果? 是的,你可以去掉Set wbkExport = Application.Workbooks.Add,只做shtToExport.Copy,然后使用ActiveWorkbook 试过了,现在我似乎得到了application-defined or object defined error。如果你想看一下,我更新了上面的代码。 应该只是shtToExport.Copy。没有Before。然后ActiveWorkbook.SaveAs. 【参考方案1】:

前面的cmets是正确的。一个简单的过程是:

    Public Sub ExportWorksheetAndSaveAsCSV()
    
    'Simple copy of sheet content as csv file
    Dim NameSheet As String
    Dim PathNameCsv As Variant
           
    'Change names & Output Path
    NameSheet = "MySheet"
    PathNameCsv = "D:\Documents\MyCsv"
    
    Set shtToExport = ThisWorkbook.Worksheets(NameSheet)     'Sheet to export as CSV
    
    Application.DisplayAlerts = False 'Possibly overwrite without asking
    shtToExport.SaveAs Filename:=PathNameCsv, FileFormat:=xlCSV, CreateBackup:=True
    Application.DisplayAlerts = True
    
    'When saving the sheet as csv, _
    'by default its name is changed to the name of the csv.
    'Here you restore your name
    With shtToExport
    .Name = NameSheet '
    End With
  
End Sub

【讨论】:

以上是关于将工作表导出为 csv的主要内容,如果未能解决你的问题,请参考以下文章

Excel:将工作表导出为 CSV 文件的宏,无需离开我当前的 Excel 工作表

Python 多张表导出为 csv

python 此要点将从Excel工作簿中提取每个单独的工作表并将其导出为CSV。

有没有办法将 excel 工作簿中的单个工作表导出到使用 pandas 分隔 csv 文件?

使用Open Office从命令行将Excel电子表格的单个工作表导出为CSV

反应表中的导出到 CSV 按钮