Similar to some of the answers here, I turn gridlines in my Excel file off this way:
与此处的一些答案类似,我通过这种方式在我的Excel文件中打开网格线:
private ApplicationClass _xlApp;
. . .
_xlApp = new ApplicationClass { UserControl = true };
_xlApp.ActiveWindow.DisplayGridlines = false;
However, in my workbook, I create two sheets, and the second one needs to display gridlines. How can I toggle the displaying of gridlines at the Worksheet level?
但是,在我的工作簿中,我创建了两个工作表,第二个工作表需要显示网格线。如何在工作表级别切换网格线的显示?
I tried this:
我试过这个:
private ApplicationClass _xlApp;
private ApplicationClass _xlApp2;
. . .
_xlApp = new ApplicationClass { UserControl = true };
_xlApp.ActiveWindow.DisplayGridlines = false;
. . .
_xlApp2 = new ApplicationClass { UserControl = true };
_xlApp2.ActiveWindow.DisplayGridlines = true;
...but that emitted an electronic epistle informing me at runtime that "Object reference not set to an instance of an object" on the last line displayed above.
...但是发出了一封电子书信,在运行时通知我上面显示的最后一行上的“对象引用没有设置为对象的实例”。
So can I set one sheet to gridlined and the other ungridlined, or will I have to take matters into my own mitts and add universal borders to the second sheet?
那么我可以将一个工作表设置为网格化而另一个工作表未编辑,或者我是否必须将问题纳入我自己的手套并在第二个工作表中添加通用边框?
UPDATE
The link from David Tansey was intiguing, but it didn't provide any concrete - or even abstract - example of how to use the Worksheetview object. So I binged (bang?) "c# excel interop worksheetview displaygridlines example" and found this.
来自David Tansey的链接很有用,但它没有提供任何具体的 - 甚至是抽象的 - 如何使用Worksheetview对象的示例。所以我砰的一声(砰?)“c#excel interop worksheetview displaygridlines example”并找到了这个。
I then extrapolated this "Virtual Buffoonery" code:
然后我推断出这个“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
...and C#ified it thus:
...和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;
...but got the compile-time fingerwag, "Non-invocable member 'Microsoft.Office.Interop.Excel.Window.SheetViews' cannot be used like a method."
...但得到了编译时的指纹,“不可调用的成员'Microsoft.Office.Interop.Excel.Window.SheetViews'不能像方法一样使用。”
So, I tried this:
所以,我试过这个:
WorksheetView wsv = (WorksheetView)_xlApp.Sheets[2];
wsv.DisplayGridlines = true;
wsv.DisplayZeros = true;
It compiled, but at runtime I'm sorely disappointed (and even put out, to quote Humperdinck) by "Unable to cast COM object type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Excel.WorksheetView.'...No such interface supported"
它编译,但在运行时我非常失望(甚至推出,引用Humperdinck)“无法将COM对象类型'System .__ ComObject'转换为接口类型'Microsoft.Office.Interop.Excel.WorksheetView。'。 ..没有这样的界面支持“
So is there a way to do this in C#, or is this one of those areas where Virus Bits has it over C#?
那么有没有办法在C#中实现这一点,或者这是病毒位比C#更多的领域之一?
UPDATE 2
This variation on the theme elicits the same response from the electronic spirits:
主题的这种变化引起了电子精神的相同反应:
_xlSheetDelPerf = (Worksheet)_xlSheets.Item[2];
WorksheetView wsv = (WorksheetView)_xlSheetDelPerf;
wsv.DisplayGridlines = true;
wsv.DisplayZeros = true;
1 个解决方案
#1
0
I was unable to find a simple way to do it, so I "brute forced it" like so:
我无法找到一种简单的方法来做到这一点,所以我“粗暴地强迫它”就像这样:
// 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;
Actually, I ended up needing to restrict just which range of the sheet was gridified, anyway, so I did this:
实际上,我最终需要限制网格化的哪个范围,无论如何,所以我这样做:
// 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;
#1
0
I was unable to find a simple way to do it, so I "brute forced it" like so:
我无法找到一种简单的方法来做到这一点,所以我“粗暴地强迫它”就像这样:
// 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;
Actually, I ended up needing to restrict just which range of the sheet was gridified, anyway, so I did this:
实际上,我最终需要限制网格化的哪个范围,无论如何,所以我这样做:
// 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;