与此处的某些答案类似,我通过以下方式关闭了Excel文件中的网格线:
private ApplicationClass _xlApp;
. . .
_xlApp = new ApplicationClass { UserControl = true };
_xlApp.ActiveWindow.DisplayGridlines = false;
但是,在我的工作簿中,我创建了两张纸,第二张纸需要显示网格线。如何在工作表级别切换网格线的显示?
我尝试了这个:
private ApplicationClass _xlApp;
private ApplicationClass _xlApp2;
. . .
_xlApp = new ApplicationClass { UserControl = true };
_xlApp.ActiveWindow.DisplayGridlines = false;
. . .
_xlApp2 = new ApplicationClass { UserControl = true };
_xlApp2.ActiveWindow.DisplayGridlines = true;
...但是这发出了电子书信,在运行时通知我在上面显示的最后一行上“对象引用未设置为对象的实例”。
因此,我可以将一张纸设置为网格状而另一张未网格状吗?还是我必须将事情纳入自己的手套中,并在第二张纸上添加通用边框?
David Tansey的链接很引人入胜,但没有提供任何有关如何使用Worksheetview对象的具体(甚至是抽象)示例。因此,我对(c#excel interop worksheetview displaygridlines示例)进行了分析(bang?),并找到了这个。
然后,我推断出此“虚拟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;
...但是遇到了编译时的麻烦,“不可调用的成员'Microsoft.Office.Interop.Excel.Window.SheetViews'不能像方法一样使用。 ”
所以,我尝试了这个:
WorksheetView wsv = (WorksheetView)_xlApp.Sheets[2];
wsv.DisplayGridlines = true;
wsv.DisplayZeros = true;
它已编译,但是在运行时,我对“无法将COM对象类型'System .__ ComObject'强制转换为接口类型'Microsoft.Office.Interop.Excel.WorksheetView。”感到非常失望(甚至将其引述为Humperdinck)。 ..不支持此类接口“
那么有没有办法在C#中执行此操作,或者这是Virus Bits在C#上具有病毒位的那些区域之一?
这种主题上的变化引起了电子精神的相同反应:
_xlSheetDelPerf = (Worksheet)_xlSheets.Item[2];
WorksheetView wsv = (WorksheetView)_xlSheetDelPerf;
wsv.DisplayGridlines = true;
wsv.DisplayZeros = true;
我无法找到一种简单的方法来做到这一点,所以我像这样:“强行强迫它”:
// 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;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句