我喜欢@deathApril的想法,将工作表命名为1_Germany、2_UK、3_IRELAND。我也知道您需要为数百个工作表进行重命名的问题。如果您没有重命名工作表名称的问题,那么您可以使用此宏来为您完成。它只需要几秒钟就可以重命名所有工作表名称。不幸的是,ODBC、OLEDB按升序返回工作表名称。这是无法更改的。您必须使用COM或将名称重命名为按顺序排列。
Sub Macro1()
'
' Macro1 Macro
'
'
Dim i As Integer
For i = 1 To Sheets.Count
Dim prefix As String
prefix = i
If Len(prefix) < 4 Then
prefix = "000"
ElseIf Len(prefix) < 3 Then
prefix = "00"
ElseIf Len(prefix) < 2 Then
prefix = "0"
End If
Dim sheetName As String
sheetName = Sheets(i).Name
Dim names
names = Split(sheetName, "-")
If (UBound(names) > 0) And IsNumeric(names(0)) Then
'do nothing
Else
Sheets(i).Name = prefix & i & "-" & Sheets(i).Name
End If
Next
End Sub
更新:
在阅读@SidHoland有关BIFF的评论后,我想到了一个想法。以下步骤可以通过代码完成。不知道您是否真的想这样以获得相同顺序的工作表名称。如果您需要通过代码完成此操作,请告诉我需要帮助。
1. Consider XLSX as a zip file. Rename *.xlsx into *.zip
2. Unzip
3. Go to unzipped folder root and open /docprops/app.xml
4. This xml contains the sheet name in the same order of what you see.
5. Parse the xml and get the sheet names
更新:
另一个解决方案 - NPOI可能会有所帮助
http://npoi.codeplex.com/
FileStream file = new FileStream(@"yourexcelfilename", FileMode.Open, FileAccess.Read);
HSSFWorkbook hssfworkbook = new HSSFWorkbook(file);
for (int i = 0; i < hssfworkbook.NumberOfSheets; i++)
{
Console.WriteLine(hssfworkbook.GetSheetName(i));
}
file.Close();
这个解决方案适用于xls格式,我没有尝试过xlsx格式。
谢谢,
Esen