如何添加网格线到一个工作表而不是其他(C#Excel Interop)?

类似于这里的一些答案,我将Excel文件中的网格线closures:

private ApplicationClass _xlApp; . . . _xlApp = new ApplicationClass { UserControl = true }; _xlApp.ActiveWindow.DisplayGridlines = false; 

但是,在我的工作簿中,我创build了两张纸,第二张纸需要显示网格线。 如何在工作表级切换显示网格线?

我试过这个:

 private ApplicationClass _xlApp; private ApplicationClass _xlApp2; . . . _xlApp = new ApplicationClass { UserControl = true }; _xlApp.ActiveWindow.DisplayGridlines = false; . . . _xlApp2 = new ApplicationClass { UserControl = true }; _xlApp2.ActiveWindow.DisplayGridlines = true; 

…但是发出了一封电子信件,通知我在运行时在上面显示的最后一行显示“ 对象引用未设置为对象实例 ”。

那么我可以将一张表格设为网格,另一张表格是非网格的,还是必须将自己的手套放在第二张表格中并添加通用边框?

UPDATE

来自David Tansey的链接已经开始酝酿,但是并没有提供如何使用Worksheetview对象的具体甚至是抽象的例子。 所以我bangged(砰?)“ c#excel互操作工作表视图displaygridlines示例 ”,并发现这一点 。

然后,我推断这个“虚拟包袱”代码:

 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#ified它如此:

 // existing: private ApplicationClass _xlApp; _xlApp = new ApplicationClass { UserControl = true }; // new / extrapolated: WorksheetView wsv = _xlApp.ActiveWindow.SheetViews(2); wsv.DisplayGridlines = true; wsv.DisplayZeros = true; 

…但得到了编译时fingerwag,“ 不可调用的成员”Microsoft.Office.Interop.Excel.Window.SheetViews“不能像方法一样使用。

所以,我试过这个:

 WorksheetView wsv = (WorksheetView)_xlApp.Sheets[2]; wsv.DisplayGridlines = true; wsv.DisplayZeros = true; 

它编译了,但在运行时,我非常失望(甚至用引用Humperdinck)“ 无法将COM对象typesSystem .__ ComObject'转换为接口types'Microsoft.Office.Interop.Excel.WorksheetView'。没有这样的接口支持

那么有没有办法在C#中做到这一点,或者这是病毒位有C#的地方之一吗?

更新2

这个主题的变化引发了电子精神的相同回应:

 _xlSheetDelPerf = (Worksheet)_xlSheets.Item[2]; WorksheetView wsv = (WorksheetView)_xlSheetDelPerf; wsv.DisplayGridlines = true; wsv.DisplayZeros = true; 

我无法find一个简单的方法来做到这一点,所以我“强迫它”如此:

 // 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; 

实际上,我最终需要限制表单的哪个范围被网格化,无论如何,所以我这样做了:

 // 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;