与某些答案here类似,我以这种方式关闭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 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#ified it:
// 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;
它已编译,但在运行时我非常失望(甚至推出,引用Humperdinck)" 无法转换COM对象类型' System .__ ComObject'到界面类型' Microsoft.Office.Interop.Excel.WorksheetView。' ...没有这样的界面支持"
那么有没有办法在C#中执行此操作,或者这是哪个区域的病毒位比C#更多?
主题的这种变化引出了电子精神的相同反应:
_xlSheetDelPerf = (Worksheet)_xlSheets.Item[2];
WorksheetView wsv = (WorksheetView)_xlSheetDelPerf;
wsv.DisplayGridlines = true;
wsv.DisplayZeros = true;
答案 0 :(得分:0)
我无法找到一种简单的方法来做到这一点,所以我强迫它#34;像这样:
// 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;