与这里的一些答案类似,我以这种方式关闭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的链接很有意思,但它没有提供任何关于如何使用Worksheetwiew对象的具体甚至抽象的例子。所以我大快朵颐(砰?)"c#excel interop工作表视图显示网格线示例"并发现了这一点。
然后我推断出这个"虚拟Buffenery"代码:
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;
它进行了编译,但在运行时,我非常失望(甚至引用Humperdink的话),因为"无法将COM对象类型"System.__ComObject"转换为接口类型"Microsoft.Office.Interop.Excel.WorksheetView."…不支持这样的接口"
那么,有没有一种方法可以在C#中做到这一点,或者这是病毒位在C#上占据优势的领域之一?
更新2
这个主题的变化引起了电子精神的同样反应:
_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;