如何将选定的列导出到CSV文件


24

我想将特定数量的列从excel导出到.csv文件。我身边有10列一样lnamefnamephoneaddressemail等。我应该怎么做才能只导出某些列,如lnameemail等等?

Answers:


23

只要以简单的方式做:

  1. 应用串联10列

    =CONCATENATE(A1,",",B1,",",C1,",",D1,",",E1,",",F1,",",G1,",",H1,",",I1,",",J1)
    
  2. 向下拖动最后一行的列表末尾

  3. 复制结果列
  4. 将其粘贴到记事本中
  5. 将其另存为.csv文件格式

CONCATENATE对我来说确实很棒。非常感谢你。
NedimŠabić2012年

3
简单......除非双引号可以在任何你列中找到...
主教

您是否不必用分号分隔连接的元素?= CONCATENATE(A1;“,”; B1;“,”; C1;“,”; D1;“,”; E1;“,”;“ F1;”,“; G1;”,“; H1;”,“ ; I1;“,”; J1)
mabho

9

选择所需的第一列。然后,在按住的同时<Ctrl>,选择所需的其余列。复制您的选择并将其粘贴到新的工作簿中。将新工作簿另存为.csv文件。

如果要经常执行此操作,请记录步骤的宏。这是我测试记录的宏。在我的示例中,列A为名称,列E为电子邮件。我还修改了宏,以便SaveAs文件名包含当前日期。


我将显示一个示例宏,但是由于任何原因,当我单击“保存编辑”时,超级用户都会出错。我稍后再试。


4

这是一个技术含量较低的解决方案:

  1. 将整个工作表的副本另存为.csv。
  2. 在Excel中仍打开时,删除不需要的列。
  3. 保存。

3

我作为插件编写了自己的VBA解决方案。它可以在这里 GitHub上。

示例视图(单击图像查看大图):

工具表格的屏幕截图

使用步骤为:

  • 安装加载项
  • 加载表单(当前已分配Ctrl+ Shift+ C以显示表单)
  • 突出显示您要导出的范围
  • 选择导出文件夹
  • 输入所需的文件名,数字格式和分隔符
  • 选择是追加还是覆盖
  • 点击“导出”

该表单是无模式的,因此您可以在选择其他范围或浏览工作表到工作表或工作簿到工作簿时将其保持打开状态。注意,“ at符号”(@)代表Excel的“常规”数字格式,用于诸如此类的输出操作。

的内容C:\test.csv从上面的例子:

13,14,15
14,15,16
15,16,17

2
Sub ExportSelectionAsCSV()
    ' MS Excel 2007
    ' Visual Basic for Applications
    '
    ' Copies the selected rows & columns
    ' to a new Excel Workbook. Saves the new 
    ' Workbook as Comma Separated Value (text) file.
    '
    ' The active workbook (the 'invoking' workbook - the 
    ' one that is active when this subroutine is called) 
    ' is unaffected.
    '
    ' Before returning from the subroutine, the invoking workbook
    ' is "set back to" (restored as) the active workbook.
    '
    ' Note: target filename is hard coded (code is simpler that way)

    ' Suspends screen updating (until ready to return)
    ' Warning: ScreenUpdating MUST be re-enabled before
    ' returning from this subroutine.
    '
    ' Note: Step through this subroutine line-by-line to prove
    ' to yourself that it is performing as promised.
    ' (Please step through the code at least once - use F8)
    Application.ScreenUpdating = False

    ' Gets the name of *this (the invoking) workbook
    ' so *this workbook can again be set active
    ' at the end of this subroutine.
    Dim CurrentFileName As String
    CurrentFileName = ActiveWorkbook.Name
    Debug.Print "Active File: " + CurrentFileName

    ' Copies the selected cells (to the clipboard).
    ' Precondition: Cells must be selected before 
    ' calling this subroutine.
    Selection.Copy

    ' Instantiates a (new) object instance of type Excel workbook.
    ' Side-effect: The new workbook instance is now
    ' the 'active' workbook. 
    Workbooks.Add Template:="Workbook"

    ' Selects the first cell of the 
    ' first worksheet of the new workbook.
    Range("A1").Select

    ' Pastes the clipboard contents to the new worksheet
    ' (of the new workbook)
    ActiveSheet.Paste

    ' Writes the new (active) Excel workbook to file.
    ' The format is Comma Separated Value
    ActiveWorkbook.SaveAs Filename:= _
    "C:\temp\data.csv" _
    , FileFormat:=xlCSV, _
    CreateBackup:=False

    ' Gets the filename of the new (active) workbook
    ' so the name can be logged.
    Dim NewFileName As String
    NewFileName = ActiveWorkbook.Name
    Debug.Print "Active File: " + NewFileName

    ' Closes the new CSV file
    Application.DisplayAlerts = False
    ActiveWorkbook.Close
    Application.DisplayAlerts = True

    ' Clears the clipboard contents.
    Application.CutCopyMode = False

    ' Restores the invoking workbook as the active
    ' Excel workbook. 
    Workbooks(CurrentFileName).Activate
    Range("A1").Select

    ' Re-Enables Excel screen display.
    Application.ScreenUpdating = True
End Sub


1

如果在Ron的编辑器中打开文件,则可以隐藏不需要的列,然后将生成的“视图”导出为Excel文件或其他任何格式。更好的是,您可以保存视图以供将来使用。非常快,非常容易。


1

另一个解决方案:

  1. 选择要导出的单元格
  2. 在表格周围包裹表格(例如,在Windows上按Control + T)
  3. 运行ExportTable宏

将表以新的CSV格式保存在活动工作表上(通过打开一个新工作簿并使用表名作为文件名从那里保存)。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.