如何将网格线添加到一个 sheet 而不是另一个(C# Excel Interop)?
How can I add gridlines to one sheet and not the other (C# Excel Interop)?
类似于某些答案 here,我用这种方式关闭 Excel 文件中的网格线:
private ApplicationClass _xlApp;
. . .
_xlApp = new ApplicationClass { UserControl = true };
_xlApp.ActiveWindow.DisplayGridlines = false;
但是,在我的工作簿中,我创建了两个 sheet,第二个需要显示网格线。如何在 Worksheet 级别切换网格线的显示?
我试过这个:
private ApplicationClass _xlApp;
private ApplicationClass _xlApp2;
. . .
_xlApp = new ApplicationClass { UserControl = true };
_xlApp.ActiveWindow.DisplayGridlines = false;
. . .
_xlApp2 = new ApplicationClass { UserControl = true };
_xlApp2.ActiveWindow.DisplayGridlines = true;
...但是它发出了一封电子信函,在运行时通知我“Object 引用未设置为 object 的实例”上面显示的最后一行。
所以我可以将一个 sheet 设置为网格线,另一个不设置网格线,还是我必须自己处理问题并为第二个 sheet 添加通用边框?
更新
David Tansey 的 link 很有趣,但它没有提供任何具体的 - 甚至是抽象的 - 如何使用 Worksheet 视图 object 的示例。所以我狂饮(爆炸?)“c# excel interop worksheetview displaygridlines example”并找到 this.
然后我推断出这个 "Virtual Buffoonery" 代码:
Dim wsv As WorksheetView
Set wsv = wnd.SheetViews(1)
' Display formulas and zeros, but hide
' gridlines, headings, and outlines:
wsv.DisplayFormulas = True
wsv.DisplayGridlines = False
wsv.DisplayHeadings = False
wsv.DisplayOutline = False
wsv.DisplayZeros = True
...然后 C# 将其定义为:
// existing:
private ApplicationClass _xlApp;
_xlApp = new ApplicationClass { UserControl = true };
// new / extrapolated:
WorksheetView wsv = _xlApp.ActiveWindow.SheetViews(2);
wsv.DisplayGridlines = true;
wsv.DisplayZeros = true;
...但遇到了 compile-time 指手画脚,“Non-invocable 成员 'Microsoft.Office.Interop.Excel.Window.SheetViews' 不能像方法一样使用。”
所以,我尝试了这个:
WorksheetView wsv = (WorksheetView)_xlApp.Sheets[2];
wsv.DisplayGridlines = true;
wsv.DisplayZeros = true;
它编译了,但在运行时我非常失望(引用 Humperdinck 的话甚至被淘汰)“无法将 COM object 类型 'System.__ComObject' 转换为接口输入 'Microsoft.Office.Interop.Excel.WorksheetView.'...不支持此类接口"
那么有没有办法在 C# 中执行此操作,或者这是 Virus Bits 在 C# 中拥有它的领域之一?
更新 2
这个主题的变化引起了电子精神的相同反应:
_xlSheetDelPerf = (Worksheet)_xlSheets.Item[2];
WorksheetView wsv = (WorksheetView)_xlSheetDelPerf;
wsv.DisplayGridlines = true;
wsv.DisplayZeros = true;
我找不到简单的方法,所以我 "brute forced it" 喜欢这样:
// Add borders to the sheet
var delPerfDataRange =
_xlSheetDelPerf.Range[_xlSheetDelPerf.Cells[1, _xlSheetDelPerf.UsedRange.Columns.Count],
_xlSheetDelPerf.Cells[_xlSheetDelPerf.UsedRange.Rows.Count, _xlSheetDelPerf.UsedColumns.Count]];
Borders _dataBorders = delPerfDataRange.Borders;
_dataBorders[XlBordersIndex.xlEdgeLeft].LineStyle = XlLineStyle.xlContinuous;
_dataBorders[XlBordersIndex.xlEdgeRight].LineStyle = XlLineStyle.xlContinuous;
_dataBorders[XlBordersIndex.xlEdgeTop].LineStyle = XlLineStyle.xlContinuous;
_dataBorders[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous;
_dataBorders.Color = Color.Black;
实际上,我最终需要限制 sheet 的哪个范围被网格化,无论如何,所以我这样做了:
// Add borders around all the data
var delPerfDataRange =
_xlSheetDelPerf.Range[_xlSheetDelPerf.Cells[DEL_PERF_FIRST_DATA_ROW, PROACT_DISTRIBUTOR_COLUMN],
_xlSheetDelPerf.Cells[curDelPerfRow - 1, TOTAL_PACKAGE_COUNT_COLUMN]];
Borders _dataBorders = delPerfDataRange.Borders;
_dataBorders[XlBordersIndex.xlEdgeLeft].LineStyle = XlLineStyle.xlContinuous;
_dataBorders[XlBordersIndex.xlEdgeRight].LineStyle = XlLineStyle.xlContinuous;
_dataBorders[XlBordersIndex.xlEdgeTop].LineStyle = XlLineStyle.xlContinuous;
_dataBorders[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous;
_dataBorders.Color = Color.Black;
类似于某些答案 here,我用这种方式关闭 Excel 文件中的网格线:
private ApplicationClass _xlApp;
. . .
_xlApp = new ApplicationClass { UserControl = true };
_xlApp.ActiveWindow.DisplayGridlines = false;
但是,在我的工作簿中,我创建了两个 sheet,第二个需要显示网格线。如何在 Worksheet 级别切换网格线的显示?
我试过这个:
private ApplicationClass _xlApp;
private ApplicationClass _xlApp2;
. . .
_xlApp = new ApplicationClass { UserControl = true };
_xlApp.ActiveWindow.DisplayGridlines = false;
. . .
_xlApp2 = new ApplicationClass { UserControl = true };
_xlApp2.ActiveWindow.DisplayGridlines = true;
...但是它发出了一封电子信函,在运行时通知我“Object 引用未设置为 object 的实例”上面显示的最后一行。
所以我可以将一个 sheet 设置为网格线,另一个不设置网格线,还是我必须自己处理问题并为第二个 sheet 添加通用边框?
更新
David Tansey 的 link 很有趣,但它没有提供任何具体的 - 甚至是抽象的 - 如何使用 Worksheet 视图 object 的示例。所以我狂饮(爆炸?)“c# excel interop worksheetview displaygridlines example”并找到 this.
然后我推断出这个 "Virtual Buffoonery" 代码:
Dim wsv As WorksheetView
Set wsv = wnd.SheetViews(1)
' Display formulas and zeros, but hide
' gridlines, headings, and outlines:
wsv.DisplayFormulas = True
wsv.DisplayGridlines = False
wsv.DisplayHeadings = False
wsv.DisplayOutline = False
wsv.DisplayZeros = True
...然后 C# 将其定义为:
// existing:
private ApplicationClass _xlApp;
_xlApp = new ApplicationClass { UserControl = true };
// new / extrapolated:
WorksheetView wsv = _xlApp.ActiveWindow.SheetViews(2);
wsv.DisplayGridlines = true;
wsv.DisplayZeros = true;
...但遇到了 compile-time 指手画脚,“Non-invocable 成员 'Microsoft.Office.Interop.Excel.Window.SheetViews' 不能像方法一样使用。”
所以,我尝试了这个:
WorksheetView wsv = (WorksheetView)_xlApp.Sheets[2];
wsv.DisplayGridlines = true;
wsv.DisplayZeros = true;
它编译了,但在运行时我非常失望(引用 Humperdinck 的话甚至被淘汰)“无法将 COM object 类型 'System.__ComObject' 转换为接口输入 'Microsoft.Office.Interop.Excel.WorksheetView.'...不支持此类接口"
那么有没有办法在 C# 中执行此操作,或者这是 Virus Bits 在 C# 中拥有它的领域之一?
更新 2
这个主题的变化引起了电子精神的相同反应:
_xlSheetDelPerf = (Worksheet)_xlSheets.Item[2];
WorksheetView wsv = (WorksheetView)_xlSheetDelPerf;
wsv.DisplayGridlines = true;
wsv.DisplayZeros = true;
我找不到简单的方法,所以我 "brute forced it" 喜欢这样:
// Add borders to the sheet
var delPerfDataRange =
_xlSheetDelPerf.Range[_xlSheetDelPerf.Cells[1, _xlSheetDelPerf.UsedRange.Columns.Count],
_xlSheetDelPerf.Cells[_xlSheetDelPerf.UsedRange.Rows.Count, _xlSheetDelPerf.UsedColumns.Count]];
Borders _dataBorders = delPerfDataRange.Borders;
_dataBorders[XlBordersIndex.xlEdgeLeft].LineStyle = XlLineStyle.xlContinuous;
_dataBorders[XlBordersIndex.xlEdgeRight].LineStyle = XlLineStyle.xlContinuous;
_dataBorders[XlBordersIndex.xlEdgeTop].LineStyle = XlLineStyle.xlContinuous;
_dataBorders[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous;
_dataBorders.Color = Color.Black;
实际上,我最终需要限制 sheet 的哪个范围被网格化,无论如何,所以我这样做了:
// Add borders around all the data
var delPerfDataRange =
_xlSheetDelPerf.Range[_xlSheetDelPerf.Cells[DEL_PERF_FIRST_DATA_ROW, PROACT_DISTRIBUTOR_COLUMN],
_xlSheetDelPerf.Cells[curDelPerfRow - 1, TOTAL_PACKAGE_COUNT_COLUMN]];
Borders _dataBorders = delPerfDataRange.Borders;
_dataBorders[XlBordersIndex.xlEdgeLeft].LineStyle = XlLineStyle.xlContinuous;
_dataBorders[XlBordersIndex.xlEdgeRight].LineStyle = XlLineStyle.xlContinuous;
_dataBorders[XlBordersIndex.xlEdgeTop].LineStyle = XlLineStyle.xlContinuous;
_dataBorders[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous;
_dataBorders.Color = Color.Black;