如何将多张 Excel 文件转换为一组 TSV 文件?
How to convert Excel file with multiple sheets to a set of TSV files?
在下面的代码中,要添加什么附加 clde 到 cconvert excel 具有多个 sheet 的文件到 Tsv 文件。
注意:下面的代码将带有一个 sheet 的 excel 文件转换为 Tsv.It 不处理带有多个 [=20] 的 excel sheet =]
Public Sub Main()
Dim oExcel As Object
Dim oBook As Object
Dim sFileName As String
Dim sFileNameOnly As String
Dim sXlsPath As String
Dim sTsvPath As String
sFileName = CStr(Dts.Variables("User::Xls_File_Name").Value)
sXlsPath = "H:\Xls_Files\" + sFileName
sFileNameOnly = Path.GetFileNameWithoutExtension(sFileName)
sTsvPath = "H:\Xls_Files\" + sFileNameOnly + ".Txt"
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Open(sXlsPath)
oBook.SaveAs(sTsvPath, -4158)
oBook.Close(False)
enter code here
oExcel.Quit()
Dts.TaskResult = ScriptResults.Success
End Sub
您可以使用 Microsoft.Office.Interop.Excel
完成该任务(请注意,您需要先引用该命名空间)。
@stuartd 建议您可以创建一个摘要工作表,其中将根据您的工作表结构包含所有工作表(请参阅我在代码中的评论)
或者(如果它适合您的需要)您可以使用 For Each
循环将每个工作表保存在不同的 tsv 文件中。
这是一个显示其原理的测试示例:
Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.Excel
Public Sub Main()
Dim xlApp As New Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
xlApp.Visible = False
' create a new workbook (you can open existing workbooks using the Open() method) '
xlWorkBook = xlApp.Workbooks.Add()
' create sheet 1 '
xlWorkSheet = xlWorkBook.Worksheets.Add()
xlWorkSheet.Name = "ws1"
xlWorkSheet.Range("a1").Value = "hello from worksheet1"
' create sheet 2 '
xlWorkSheet = xlWorkBook.Worksheets.Add()
xlWorkSheet.Name = "ws2"
xlWorkSheet.Range("a1").Value = "hello from worksheet2"
' worksheet that contain both worksheets '
xlWorkSheet = xlWorkBook.Worksheets.Add()
xlWorkSheet.Name = "summary"
' paste all sheets data inside the summary sheets (i dont know how your excel sheets are structured but you can select entire ranges and arrange them on the summary worksheet) '
Dim counter As Int16 = 1
For Each w As Worksheet In xlWorkBook.Worksheets
If w.Name <> "summary" Then
xlWorkSheet.Cells(counter, 1) = w.Range("a1")
counter += 1
End If
Next
xlApp.DisplayAlerts = False
' save as tsv file '
xlWorkBook.SaveAs("c:\xxx\xxx\Book4.tsv", XlFileFormat.xlTextWindows, CreateBackup:=False)
End Sub
在下面的代码中,要添加什么附加 clde 到 cconvert excel 具有多个 sheet 的文件到 Tsv 文件。
注意:下面的代码将带有一个 sheet 的 excel 文件转换为 Tsv.It 不处理带有多个 [=20] 的 excel sheet =]
Public Sub Main()
Dim oExcel As Object
Dim oBook As Object
Dim sFileName As String
Dim sFileNameOnly As String
Dim sXlsPath As String
Dim sTsvPath As String
sFileName = CStr(Dts.Variables("User::Xls_File_Name").Value)
sXlsPath = "H:\Xls_Files\" + sFileName
sFileNameOnly = Path.GetFileNameWithoutExtension(sFileName)
sTsvPath = "H:\Xls_Files\" + sFileNameOnly + ".Txt"
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Open(sXlsPath)
oBook.SaveAs(sTsvPath, -4158)
oBook.Close(False)
enter code here
oExcel.Quit()
Dts.TaskResult = ScriptResults.Success
End Sub
您可以使用 Microsoft.Office.Interop.Excel
完成该任务(请注意,您需要先引用该命名空间)。
@stuartd 建议您可以创建一个摘要工作表,其中将根据您的工作表结构包含所有工作表(请参阅我在代码中的评论)
或者(如果它适合您的需要)您可以使用 For Each
循环将每个工作表保存在不同的 tsv 文件中。
这是一个显示其原理的测试示例:
Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.Excel
Public Sub Main()
Dim xlApp As New Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
xlApp.Visible = False
' create a new workbook (you can open existing workbooks using the Open() method) '
xlWorkBook = xlApp.Workbooks.Add()
' create sheet 1 '
xlWorkSheet = xlWorkBook.Worksheets.Add()
xlWorkSheet.Name = "ws1"
xlWorkSheet.Range("a1").Value = "hello from worksheet1"
' create sheet 2 '
xlWorkSheet = xlWorkBook.Worksheets.Add()
xlWorkSheet.Name = "ws2"
xlWorkSheet.Range("a1").Value = "hello from worksheet2"
' worksheet that contain both worksheets '
xlWorkSheet = xlWorkBook.Worksheets.Add()
xlWorkSheet.Name = "summary"
' paste all sheets data inside the summary sheets (i dont know how your excel sheets are structured but you can select entire ranges and arrange them on the summary worksheet) '
Dim counter As Int16 = 1
For Each w As Worksheet In xlWorkBook.Worksheets
If w.Name <> "summary" Then
xlWorkSheet.Cells(counter, 1) = w.Range("a1")
counter += 1
End If
Next
xlApp.DisplayAlerts = False
' save as tsv file '
xlWorkBook.SaveAs("c:\xxx\xxx\Book4.tsv", XlFileFormat.xlTextWindows, CreateBackup:=False)
End Sub