[英]Changing sheet displayed with ClosedXML
I am dynamically creating an Excel workbook with multiple sheets using ClosedXML.我正在使用 ClosedXML 动态创建一个包含多个工作表的 Excel 工作簿。 I am struggling changing the sheet selected back to the first sheet in the workbook after I am done generating the content and cannot find anything in the documentation on how to change the sheet that is displayed.
生成内容后,我正在努力将选定的工作表更改回工作簿中的第一张工作表,并且在文档中找不到有关如何更改显示的工作表的任何内容。 I have tried:
我试过了:
wb.Worksheet(1).Select();
This selects all of the cells in the worksheet, but it doesn't change the view back.这将选择工作表中的所有单元格,但不会将视图更改回来。
wb.Worksheet(1).Cells(1,1).Value = wb.Worksheet(1).Cells(1,1).Value
seems to set the value, but again doesn't change the view.似乎设置了值,但同样不会改变视图。
I have tried hiding the other sheets in hopes that it would auto select the visible sheets, but that does not seem to work.我曾尝试隐藏其他工作表,希望它能自动选择可见工作表,但这似乎不起作用。 Any suggestions?
有什么建议?
使用 ClosedXML 将活动工作表设置为第一个工作表将使用以下代码:
wb.Worksheet(1).SetTabActive();
I found that Andrew K's solution did not deselect previously selected tabs.我发现 Andrew K 的解决方案没有取消选择以前选择的选项卡。 I wrote this extension to meet this requirement also:
我也写了这个扩展来满足这个要求:
public static class Extensions
{
public static void SetActiveSheet(this XLWorkbook workbook, IXLWorksheet sheetToSetActive)
{
foreach (var sheet in workbook.Worksheets)
{
var isCurrentSheet = sheet.Equals(sheetToSetActive);
sheet.SetTabActive(isCurrentSheet);
sheet.SetTabSelected(isCurrentSheet);
}
}
}
Use it as follows:使用方法如下:
var sheetToSetActive = wb.Worksheet(1);
wb.SetActiveSheet(sheetToSetActive);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.