"After" Sheets.Add 方法的参数
"After" parameter of Sheets.Add method
我尝试使用以下代码在活动后添加 5 张:
Imports System
Imports Microsoft.Office.Interop
Module Program
Dim oxl As Excel.Application
Dim owbs As Excel.Workbooks
Dim owb As Excel.Workbook
Dim osheets As Excel.Worksheet
Sub Main(args As String())
Dim pos As Excel.Worksheet
oxl = CreateObject("Excel.Application")
oxl.DisplayAlerts = True
oxl.Visible = True
owb = oxl.Workbooks.Add
pos = owb.Worksheets("Sheet1")
Console.WriteLine("Enter key to add 5 worksheets")
Console.ReadLine()
'Adds 5 sheets after 'Sheet1' - default active sheet
osheets = owb.Worksheets.Add(, pos, 5,)
Console.ReadLine()
End Sub
End Module
我的问题:Sheet2 被添加到 Sheet1 的右侧。但是所有后续工作表都被添加到工作表 2 的左侧(见图)
因此工作表从L到R的顺序变为:Sheet1、Sheet6、Sheet5、Sheet4、Sheet3、Sheet2。订单(从 L 到 R)不应该是 Sheet1、Sheet2....Sheet6 吗?这是正常的还是我遗漏了什么?如果这是正确的,那么是否有一种直接的方法来插入工作表,以便从 L 到 R 它们读取为 Sheet1、Sheet2...Sheet6?
我想知道这样的事情是否可行,
Sub Main(args As String())
Dim pos As Excel.Worksheet
oxl = CreateObject("Excel.Application")
oxl.DisplayAlerts = True
oxl.Visible = True
owb = oxl.Workbooks.Add
pos = owb.Worksheets("Sheet1")
Console.WriteLine("Enter key to add 5 worksheets")
Console.ReadLine()
'Adds 5 sheets after 'Sheet1' - default active sheet
For x As Integer = 1 To 5
osheets = owb.Worksheets.Add(, pos, 1, )
Console.ReadLine()
pos = owb.Worksheets(owb.Worksheets.Count) 'last sheet?
Next
End Sub
下面显示了如何使用 Microsoft.Office.Interop.Excel 将工作表添加到 Excel 工作簿。
添加引用:Microsoft Excel xx.x 对象库(例如:Microsoft Excel 16.0 对象库)
添加导入语句:
Imports Excel = Microsoft.Office.Interop.Excel
Imports System.IO
AddSheets:
Private Sub AddSheets(filename As String)
'All indices in Excel (rowNumber, columnNumber, etc...) start with 1
Dim oMissing As Object = System.Reflection.Missing.Value
Dim oxl As Excel.Application = Nothing
Dim owb As Excel.Workbook = Nothing
Dim osheet As Excel.Worksheet = Nothing
Dim previouslyActiveSheet As Excel.Worksheet = Nothing
Try
'create new instance
oxl = New Excel.Application()
'suppress displaying alerts (such as prompting to overwrite existing file)
oxl.DisplayAlerts = False
'set Excel visibility
oxl.Visible = False
'disable user control while modifying the Excel Workbook
'to prevent user interference
'only necessary if Excel application Visibility property = true
'oxl.UserControl = False
'if writing/updating a large amount of data
'disable screen updating by setting value to false
'for better performance.
're-enable when done writing/updating data, if desired
'oxl.ScreenUpdating = False;
If File.Exists(filename) Then
'open existing
owb = oxl.Workbooks.Open(filename)
Else
'add Workbook
owb = oxl.Workbooks.Add()
End If
'get active worksheet
If owb.Sheets.Count > 0 Then
previouslyActiveSheet = DirectCast(oxl.ActiveSheet, Excel.Worksheet)
End If
'get last sheet
osheet = DirectCast(owb.Sheets(owb.Sheets.Count), Excel.Worksheet)
'add sheets
For i As Integer = 1 To 5
osheet = DirectCast(owb.Worksheets.Add(After:=osheet, Count:=1), Excel.Worksheet)
Next
'For Each ws As Excel.Worksheet In owb.Sheets
'Debug.WriteLine($"ws name: {ws.Name}")
'Next
If previouslyActiveSheet IsNot Nothing Then
'set active sheet to originally active sheet
previouslyActiveSheet.Activate()
End If
'save
owb.SaveAs(Filename:=filename)
'if previously disabled, re-enable
'oxl.UserControl = True
'if previously disabled, re-enable
'oxl.ScreenUpdating = True;
Catch ex As Exception
'ToDo: add desired code
Throw 're-throw exception
Finally
If owb IsNot Nothing Then
owb.Close()
End If
If oxl IsNot Nothing Then
oxl.Quit()
End If
End Try
End Sub
资源
我尝试使用以下代码在活动后添加 5 张:
Imports System
Imports Microsoft.Office.Interop
Module Program
Dim oxl As Excel.Application
Dim owbs As Excel.Workbooks
Dim owb As Excel.Workbook
Dim osheets As Excel.Worksheet
Sub Main(args As String())
Dim pos As Excel.Worksheet
oxl = CreateObject("Excel.Application")
oxl.DisplayAlerts = True
oxl.Visible = True
owb = oxl.Workbooks.Add
pos = owb.Worksheets("Sheet1")
Console.WriteLine("Enter key to add 5 worksheets")
Console.ReadLine()
'Adds 5 sheets after 'Sheet1' - default active sheet
osheets = owb.Worksheets.Add(, pos, 5,)
Console.ReadLine()
End Sub
End Module
我的问题:Sheet2 被添加到 Sheet1 的右侧。但是所有后续工作表都被添加到工作表 2 的左侧(见图)
因此工作表从L到R的顺序变为:Sheet1、Sheet6、Sheet5、Sheet4、Sheet3、Sheet2。订单(从 L 到 R)不应该是 Sheet1、Sheet2....Sheet6 吗?这是正常的还是我遗漏了什么?如果这是正确的,那么是否有一种直接的方法来插入工作表,以便从 L 到 R 它们读取为 Sheet1、Sheet2...Sheet6?
我想知道这样的事情是否可行,
Sub Main(args As String())
Dim pos As Excel.Worksheet
oxl = CreateObject("Excel.Application")
oxl.DisplayAlerts = True
oxl.Visible = True
owb = oxl.Workbooks.Add
pos = owb.Worksheets("Sheet1")
Console.WriteLine("Enter key to add 5 worksheets")
Console.ReadLine()
'Adds 5 sheets after 'Sheet1' - default active sheet
For x As Integer = 1 To 5
osheets = owb.Worksheets.Add(, pos, 1, )
Console.ReadLine()
pos = owb.Worksheets(owb.Worksheets.Count) 'last sheet?
Next
End Sub
下面显示了如何使用 Microsoft.Office.Interop.Excel 将工作表添加到 Excel 工作簿。
添加引用:Microsoft Excel xx.x 对象库(例如:Microsoft Excel 16.0 对象库)
添加导入语句:
Imports Excel = Microsoft.Office.Interop.Excel
Imports System.IO
AddSheets:
Private Sub AddSheets(filename As String)
'All indices in Excel (rowNumber, columnNumber, etc...) start with 1
Dim oMissing As Object = System.Reflection.Missing.Value
Dim oxl As Excel.Application = Nothing
Dim owb As Excel.Workbook = Nothing
Dim osheet As Excel.Worksheet = Nothing
Dim previouslyActiveSheet As Excel.Worksheet = Nothing
Try
'create new instance
oxl = New Excel.Application()
'suppress displaying alerts (such as prompting to overwrite existing file)
oxl.DisplayAlerts = False
'set Excel visibility
oxl.Visible = False
'disable user control while modifying the Excel Workbook
'to prevent user interference
'only necessary if Excel application Visibility property = true
'oxl.UserControl = False
'if writing/updating a large amount of data
'disable screen updating by setting value to false
'for better performance.
're-enable when done writing/updating data, if desired
'oxl.ScreenUpdating = False;
If File.Exists(filename) Then
'open existing
owb = oxl.Workbooks.Open(filename)
Else
'add Workbook
owb = oxl.Workbooks.Add()
End If
'get active worksheet
If owb.Sheets.Count > 0 Then
previouslyActiveSheet = DirectCast(oxl.ActiveSheet, Excel.Worksheet)
End If
'get last sheet
osheet = DirectCast(owb.Sheets(owb.Sheets.Count), Excel.Worksheet)
'add sheets
For i As Integer = 1 To 5
osheet = DirectCast(owb.Worksheets.Add(After:=osheet, Count:=1), Excel.Worksheet)
Next
'For Each ws As Excel.Worksheet In owb.Sheets
'Debug.WriteLine($"ws name: {ws.Name}")
'Next
If previouslyActiveSheet IsNot Nothing Then
'set active sheet to originally active sheet
previouslyActiveSheet.Activate()
End If
'save
owb.SaveAs(Filename:=filename)
'if previously disabled, re-enable
'oxl.UserControl = True
'if previously disabled, re-enable
'oxl.ScreenUpdating = True;
Catch ex As Exception
'ToDo: add desired code
Throw 're-throw exception
Finally
If owb IsNot Nothing Then
owb.Close()
End If
If oxl IsNot Nothing Then
oxl.Quit()
End If
End Try
End Sub
资源