最近很多小伙伴都在问使用ExcelOleDb在SHEETORDER中获取工作表名称和excel获取当前工作表名称这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展C#NPOIExce
最近很多小伙伴都在问使用Excel OleDb在SHEET ORDER中获取工作表名称和excel获取当前工作表名称这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展C# NPOI Excel 跨工作薄Workbook复制工作表Sheet、C# 获取Excel工作薄中Sheet页(工作表)名集合、delphi – 如何在XE2中获取TExcelWorksheet(64位版本)?、excel 将一个工作表根据条件拆分成多个 sheet 工作表与合并多个 sheet 工作表等相关知识,下面开始了哦!
本文目录一览:- 使用Excel OleDb在SHEET ORDER中获取工作表名称(excel获取当前工作表名称)
- C# NPOI Excel 跨工作薄Workbook复制工作表Sheet
- C# 获取Excel工作薄中Sheet页(工作表)名集合
- delphi – 如何在XE2中获取TExcelWorksheet(64位版本)?
- excel 将一个工作表根据条件拆分成多个 sheet 工作表与合并多个 sheet 工作表
使用Excel OleDb在SHEET ORDER中获取工作表名称(excel获取当前工作表名称)
我正在使用OleDb从具有很多工作表的excel工作簿中进行阅读。
我需要读取工作表名称,但需要按电子表格中定义的顺序来使用它们。所以如果我有一个看起来像这样的文件;
|_____|_____|____|____|____|____|____|____|____||_____|_____|____|____|____|____|____|____|____||_____|_____|____|____|____|____|____|____|____|\__GERMANY__/\__UK__/\__IRELAND__/
那我要拿字典
1="GERMANY", 2="UK", 3="IRELAND"
我尝试使用OleDbConnection.GetOleDbSchemaTable()
,这会给我提供名称列表,但是会按字母顺序对它们进行排序。Alpha排序意味着我不知道特定名称对应于哪个工作表编号。所以我得到了;
GERMANY, IRELAND, UK
更改了UK
和的顺序IRELAND
。
我需要对它进行排序的原因是,我必须让用户按名称或索引选择数据范围。他们可以要求“从德国到爱尔兰的所有数据”或“从表1到表3的数据”。
任何想法将不胜感激。
如果我可以使用office互操作类,这将很简单。不幸的是,我不能这样做,因为互操作类在非交互环境(例如Windows服务和ASP.NET站点)中无法可靠地工作,因此我需要使用OLEDB。
答案1
小编典典在实际的MSDN文档中找不到此内容,但论坛的主持人说
恐怕OLEDB不会像在Excel中那样保留工作表顺序
工作表顺序中的Excel工作表名称
似乎这是一个足够普遍的要求,那就是要有一个不错的解决方法。
C# NPOI Excel 跨工作薄Workbook复制工作表Sheet
跨工作薄复制Sheet,并不是单纯的将Sheet的数据复制到新Sheet中,需要将数据、公式等包括数据格式(DataFormat),单元格的风格(CellStyle)等等都复制到新Sheet中。
NPOI目前的版本为2.5.1,其Excel处理已经可以较好的支持XSSF(2007及以上)与HSSF(2003及以下)各自的工作薄间的Sheet拷贝,但XSSF工作薄与HSSF工作薄间的Sheet拷贝仍未实现。而2.4.1版的HSSF的Sheet拷贝也并未完善,虽然大部分功能已实现,颜色上有异常,2.5.1版已正常。为了处理各种情况下的Sheet的拷贝,网上有比较多的示例,比较成功的示例虽然有所不同但处理方式大同小异,有些处理考虑也有不到的地方。可参考:NPOI中如何复制Sheet,poi操作excel,复制sheet,复制行,复制单元格。由于某些原因,此处只考虑NPOI 2.4.1的版本下的处理,某些不同的版本可能会有些不同。
处理思路:Sheet的复制主要考虑的就是单元格Cell,分Cell的数据与风格。数据处理网上有许多成功示例,请自寻不赘述。风格处理包括:单元格的各种情况,其中重点是字体与颜色的处理(私设:这也是目前NPOI未解决得好的原因吧 :-p )。本人参考了许多示例,Sheet复制已经能够基本解决,但一直未能处理好颜色情况(包括字体颜色),注意:此处指HSSF与XSSF间的相互或自我复制的不同情况。以下出现的代码示例中,有关颜色部分被屏蔽,呵呵,虽未成功但一直努力中……如有哪位能够解决并提供代码的话,那么基本上NPOI的Sheet复制基本上就成功了。当然,仍然有其它的方面需要解决(如:Chart、Picture……),但对于一般的代码应用,估计差不多了吧。
不多说了……翠花,上酸菜——
public static class NPOIExt{ /// <summary> /// 跨工作薄Workbook复制工作表Sheet /// </summary> /// <param name="sSheet">源工作表Sheet</param> /// <param name="dWb">目标工作薄Workbook</param> /// <param name="dSheetName">目标工作表Sheet名</param> /// <param name="clonePrintSetup">是否复制打印设置</param> public static ISheet CrossCloneSheet(this ISheet sSheet, IWorkbook dWb, string dSheetName, bool clonePrintSetup) { ISheet dSheet; dSheetName = string.IsNullOrEmpty(dSheetName) ? sSheet.SheetName : dSheetName; dSheetName = (dWb.GetSheet(dSheetName) == null) ? dSheetName : dSheetName + "_拷贝"; dSheet = dWb.GetSheet(dSheetName) ?? dWb.CreateSheet(dSheetName); CopySheet(sSheet, dSheet); if (clonePrintSetup) ClonePrintSetup(sSheet, dSheet); dWb.SetActiveSheet(dWb.GetSheetIndex(dSheet)); //当前Sheet作为下次打开默认Sheet return dSheet; } /// <summary> /// 跨工作薄Workbook复制工作表Sheet /// </summary> /// <param name="sSheet">源工作表Sheet</param> /// <param name="dWb">目标工作薄Workbook</param> /// <param name="dSheetName">目标工作表Sheet名</param> public static ISheet CrossCloneSheet(this ISheet sSheet, IWorkbook dWb, string dSheetName) { bool clonePrintSetup = true; return CrossCloneSheet(sSheet, dWb, dSheetName, clonePrintSetup); } /// <summary> /// 跨工作薄Workbook复制工作表Sheet /// </summary> /// <param name="sSheet">源工作表Sheet</param> /// <param name="dWb">目标工作薄Workbook</param> public static ISheet CrossCloneSheet(this ISheet sSheet, IWorkbook dWb) { string dSheetName = sSheet.SheetName; bool clonePrintSetup = true; return CrossCloneSheet(sSheet, dWb, dSheetName, clonePrintSetup); } private static IFont FindFont(this IWorkbook dWb, IFont font, List<IFont> dFonts) { //IFont dFont = dWb.FindFont(font.Boldweight, font.Color, (short)font.FontHeight, font.FontName, font.IsItalic, font.IsStrikeout, font.TypeOffset, font.Underline); IFont dFont = null; foreach (IFont currFont in dFonts) { //if (currFont.Charset != font.Charset) continue; //else //if (currFont.Color != font.Color) continue; //else if (currFont.FontName != font.FontName) continue; else if (currFont.FontHeight != font.FontHeight) continue; else if (currFont.IsBold != font.IsBold) continue; else if (currFont.IsItalic != font.IsItalic) continue; else if (currFont.IsStrikeout != font.IsStrikeout) continue; else if (currFont.Underline != font.Underline) continue; else if (currFont.TypeOffset != font.TypeOffset) continue; else { dFont = currFont; break; } } return dFont; } private static ICellStyle FindStyle(this IWorkbook dWb, IWorkbook sWb, ICellStyle style, List<ICellStyle> dCellStyles, List<IFont> dFonts) { ICellStyle dStyle = null; foreach (ICellStyle currStyle in dCellStyles) { if (currStyle.Alignment != style.Alignment) continue; else if (currStyle.VerticalAlignment != style.VerticalAlignment) continue; else if (currStyle.BorderTop != style.BorderTop) continue; else if (currStyle.BorderBottom != style.BorderBottom) continue; else if (currStyle.BorderLeft != style.BorderLeft) continue; else if (currStyle.BorderRight != style.BorderRight) continue; else if (currStyle.TopBorderColor != style.TopBorderColor) continue; else if (currStyle.BottomBorderColor != style.BottomBorderColor) continue; else if (currStyle.LeftBorderColor != style.LeftBorderColor) continue; else if (currStyle.RightBorderColor != style.RightBorderColor) continue; //else if (currStyle.BorderDiagonal != style.BorderDiagonal) continue; //else if (currStyle.BorderDiagonalColor != style.BorderDiagonalColor) continue; //else if (currStyle.BorderDiagonalLineStyle != style.BorderDiagonalLineStyle) continue; //else if (currStyle.FillBackgroundColor != style.FillBackgroundColor) continue; //else if (currStyle.FillBackgroundColorColor != style.FillBackgroundColorColor) continue; //else if (currStyle.FillForegroundColor != style.FillForegroundColor) continue; //else if (currStyle.FillForegroundColorColor != style.FillForegroundColorColor) continue; //else if (currStyle.FillPattern != style.FillPattern) continue; else if (currStyle.Indention != style.Indention) continue; else if (currStyle.IsHidden != style.IsHidden) continue; else if (currStyle.IsLocked != style.IsLocked) continue; else if (currStyle.Rotation != style.Rotation) continue; else if (currStyle.ShrinkToFit != style.ShrinkToFit) continue; else if (currStyle.WrapText != style.WrapText) continue; else if (!currStyle.GetDataFormatString().Equals(style.GetDataFormatString())) continue; else { IFont sFont = sWb.GetFontAt(style.FontIndex); IFont dFont = dWb.FindFont(sFont, dFonts); if (dFont == null) continue; else { currStyle.SetFont(dFont); dStyle = currStyle; break; } } } return dStyle; } private static IFont CopyFont(this IFont dFont, IFont sFont, List<IFont> dFonts) { //dFont.Charset = sFont.Charset; //dFont.Color = sFont.Color; dFont.FontHeight = sFont.FontHeight; dFont.FontName = sFont.FontName; dFont.IsBold = sFont.IsBold; dFont.IsItalic = sFont.IsItalic; dFont.IsStrikeout = sFont.IsStrikeout; dFont.Underline = sFont.Underl
C# 获取Excel工作薄中Sheet页(工作表)名集合
#region 获取Excel工作薄中Sheet页(工作表)名集合 /// <summary> /// 获取Excel工作薄中Sheet页(工作表)名集合 /// </summary> /// <param name="excelFile">Excel文件名及路径,EG:C:\Users\JK\Desktop\导入测试.xls</param> /// <returns>Sheet页名称集合</returns> private String[] GetExcelSheetNames(string fileName) { OleDbConnection objConn = null; System.Data.DataTable dt = null; try { string connString=string.Empty; string FileType =fileName.Substring(fileName.LastIndexOf(".")); if (FileType == ".xls") connString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + fileName + ";Extended Properties=Excel 8.0;"; else//.xlsx connString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + fileName + ";" + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\""; // 创建连接对象 objConn = new OleDbConnection(connString); // 打开数据库连接 objConn.Open(); // 得到包含数据架构的数据表 dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); if (dt == null) { return null; } String[] excelSheets = new String[dt.Rows.Count]; int i = 0; // 添加工作表名称到字符串数组 foreach (DataRow row in dt.Rows) { string strSheetTableName = row["TABLE_NAME"].ToString(); //过滤无效SheetName if (strSheetTableName.Contains("$")&&strSheetTableName.Replace("'", "").EndsWith("$")) { excelSheets[i] = strSheetTableName.Substring(0, strSheetTableName.Length - 1); } i++; } return excelSheets; } catch (Exception ex) { MessageBox.Show(ex.ToString()); return null; } finally { // 清理 if (objConn != null) { objConn.Close(); objConn.Dispose(); } if (dt != null) { dt.Dispose(); } } } #endregion
以上就是C# 获取Excel工作薄中Sheet页(工作表)名集合的内容。
delphi – 如何在XE2中获取TExcelWorksheet(64位版本)?
如何安装此组件的64位版本?
某处的下载链接会很酷,但是Excel 2010 64位的导入也可能有用吗?
解决方法
excel 将一个工作表根据条件拆分成多个 sheet 工作表与合并多个 sheet 工作表
本例介绍在 excel 中如何将一个工作表根据条件拆分成多个工作表。 注意:很多朋友反映 sheets (i).delete 这句代码出错,要注意下面第一个步骤,要拆分的数据工作表名称为 “数据源”, 而不是你新建工作簿时的 sheet1 这种。手动改成 “数据源” 即可。或者是把代码中得 "数据源" 改为你得源工作表 “Sheet1” 也行
Sub CFGZB()
Dim myRange As Variant
Dim myArray
Dim titleRange As Range
Dim title As String
Dim columnNum As Integer
myRange = Application.InputBox(prompt:="请选择标题行:", Type:=8)
myArray = WorksheetFunction.Transpose(myRange)
Set titleRange = Application.InputBox(prompt:="请选择拆分的表头,必须是第一行,且为一个单元格,如:“姓名”", Type:=8)
title = titleRange.Value
columnNum = titleRange.Column
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim i&, Myr&, Arr, num&
Dim d, k
For i = Sheets.Count To 1 Step -1
If Sheets(i).Name <> "Sheet1" Then
Sheets(i).Delete
End If
Next i
Set d = CreateObject("Scripting.Dictionary")
Myr = Worksheets("Sheet1").UsedRange.Rows.Count
Arr = Worksheets("Sheet1").Range(Cells(2, columnNum), Cells(Myr, columnNum))
For i = 1 To UBound(Arr)
d(Arr(i, 1)) = ""
Next
k = d.keys
For i = 0 To UBound(k)
Set conn = CreateObject("adodb.connection")
conn.Open "provider=microsoft.jet.oledb.4.0;extended properties=excel 8.0;data source=" & ThisWorkbook.FullName
Sql = "select * from [Sheet1$] where " & title & " = ''" & k(i) & "''"
Worksheets.Add after:=Sheets(Sheets.Count)
With ActiveSheet
.Name = k(i)
For num = 1 To UBound(myArray)
.Cells(1, num) = myArray(num, 1)
Next num
.Range("A2").CopyFromRecordset conn.Execute(Sql)
End With
Sheets(1).Select
Sheets(1).Cells.Select
Selection.Copy
Worksheets(Sheets.Count).Activate
ActiveSheet.Cells.Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Next i
conn.Close
Set conn = Nothing
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
1. 将要合并的文件放在同一文件夹下,复制过来就好(ps:最好不要直接操作原数据文件,避免操作失败,数据丢失)
2. 在这个目录下创建一个 “合并.xlsx”
3. 双击打开 “合并.xlsx”
4. 同时按 ALT + F11
Option Explicit
Sub mergeonexls() ''合并多工作簿中指定工作表
On Error Resume Next
Dim x As Variant, x1 As Variant, w As Workbook, wsh As Worksheet
Dim t As Workbook, ts As Worksheet, l As Integer, h As Long
Application.ScreenUpdating = False
Application.DisplayAlerts = False
x = Application.GetOpenFilename(FileFilter:="Excel文件 (*.xls; *.xlsx),*.xls; *.xlsx,所有文件(*.*),*.*", Title:="Excel选择", MultiSelect:=True)
Set t = ThisWorkbook
Set ts = t.Sheets(1) ''指定合并到的工作表,这里是第一张工作表
l = ts.UsedRange.SpecialCells(xlCellTypeLastCell).Column
For Each x1 In x
If x1 <> False Then
Set w = Workbooks.Open(x1)
Set wsh = w.Sheets(1) ''指定所需合并工作表,这里是第一张工作表
h = ts.UsedRange.SpecialCells(xlCellTypeLastCell).Row
If l = 1 And h = 1 And ts.Cells(1, 1) = "" Then
wsh.UsedRange.Copy ts.Cells(1, 1)
Else
wsh.UsedRange.Copy ts.Cells(h + 1, 1)
End If
w.Close
End If
Next
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Sub mergeeveryonexls() ''将多个工作簿下的工作表依次对应合并到本工作簿下的工作表,即第一张工作表对应合并到第一张,第二张对应合并到第二张……
On Error Resume Next
Dim x As Variant, x1 As Variant, w As Workbook, wsh As Worksheet
Dim t As Workbook, ts As Worksheet, i As Integer, l As Integer, h As Long
Application.ScreenUpdating = False
Application.DisplayAlerts = False
x = Application.GetOpenFilename(FileFilter:="Excel文件 (*.xls; *.xlsx),*.xls; *.xlsx,所有文件(*.*),*.*", Title:="Excel选择", MultiSelect:=True)
Set t = ThisWorkbook
For Each x1 In x
If x1 <> False Then
Set w = Workbooks.Open(x1)
For i = 1 To w.Sheets.Count
If i > t.Sheets.Count Then t.Sheets.Add After:=t.Sheets(t.Sheets.Count)
Set ts = t.Sheets(i)
Set wsh = w.Sheets(i)
l = ts.UsedRange.SpecialCells(xlCellTypeLastCell).Column
h = ts.UsedRange.SpecialCells(xlCellTypeLastCell).Row
If l = 1 And h = 1 And ts.Cells(1, 1) = "" Then
wsh.UsedRange.Copy ts.Cells(1, 1)
Else
wsh.UsedRange.Copy ts.Cells(h + 1, 1)
End If
Next
w.Close
End If
Next
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
来源:https://blog.csdn.net/qq_38545713/article/details/82500483
关于使用Excel OleDb在SHEET ORDER中获取工作表名称和excel获取当前工作表名称的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于C# NPOI Excel 跨工作薄Workbook复制工作表Sheet、C# 获取Excel工作薄中Sheet页(工作表)名集合、delphi – 如何在XE2中获取TExcelWorksheet(64位版本)?、excel 将一个工作表根据条件拆分成多个 sheet 工作表与合并多个 sheet 工作表等相关内容,可以在本站寻找。
本文标签: