将xlxs文件转换为csv

时间:2015-12-27 04:01:11

标签: excel bash shell scripting

我在Excel文件中有 N 张数。现在我想将所有工作表转换为.csv格式使用bash shell脚本在将xlxs文件转换为.csv之前,我需要为每个工作表添加额外的列,并在结束。请帮忙

1 个答案:

答案 0 :(得分:3)

我同意其他评论,这可以通过excel完成吗?

示例解决方案,如果您选择此方法:

' Forces all variables to be declared
Option Explicit

Sub WorksheetLoop()

' Define variable types
Dim WS_Count As Integer
Dim I As Integer
Dim currentWorkSheet As String
Dim PathName As String

' Stop screen from flickering while code is running
Application.ScreenUpdating = False

' Set WS_Count equal to the number of worksheets in the active workbook
WS_Count = ActiveWorkbook.Worksheets.Count

' Begin the loop.
For I = 1 To WS_Count

   ' Find name of the current sheet
   currentWorkSheet = ActiveWorkbook.Worksheets(I).Name

   ' Go to the current sheet
   Worksheets(currentWorkSheet).Activate

   ' Add extra column on that sheet

   ' Select rows in the extra column where the formula is to be added
   Range("D1:D10").Select

   ' Add the formula (example formula, multiplying previous column by 1)
   Selection.FormulaR1C1 = "=RC[-1]*1"

   ' Export to CSV

   ' Copy the current worksheet
   ActiveWorkbook.Sheets(currentWorkSheet).Copy after:=ActiveWorkbook.Sheets(currentWorkSheet)

   ' Rename the current worksheet
   Sheets(ActiveSheet.Name).Name = currentWorkSheet & "_export"

   ' Export the excel to csv

   ' Create path and name for export
   PathName = "" & ThisWorkbook.Path & ActiveSheet.Name & ".csv"

   ' Move sheet to seperate workbook
   Sheets(ActiveSheet.Name).Move

   ' Save as CSV file
   ActiveWorkbook.SaveAs Filename:=PathName, FileFormat:=xlCSV

   ' Close that CSV with the SAVE warnings surpressed
   Application.DisplayAlerts = False
   ActiveWorkbook.Close
   Application.DisplayAlerts = True

Next I

Application.ScreenUpdating = True

End Sub

要运行代码,请使用vba编辑器(快捷方式打开编辑器ALT + F11)。打开一个新的宏,粘贴代码,然后你可以使用F8逐步完成每一行

相关问题