在NOPI获取到sheet表,然后明明监视sheet能看到rows的属性,可是就是 . 不出来,强行写就告知没有该属性,请问各位大佬是什么原因呢
public class HSSFSheet : ISheet { public const int INITIAL_CAPACITY = 20; public InternalWorkbook book; protected HSSFWorkbook _workbook; public HSSFSheet(HSSFWorkbook workbook); public HSSFSheet(HSSFWorkbook workbook, InternalSheet sheet); public bool IsPrintGridlines { get; set; } public bool RowSumsRight { get; set; } public bool RowSumsBelow { get; set; } public bool FitToPage { get; set; } public bool DisplayGuts { get; set; } public bool Dialog { get; set; } public bool Autobreaks { get; set; } public bool AlternativeFormula { get; set; } public bool AlternativeExpression { get; set; } public InternalSheet Sheet { get; } public bool VerticallyCenter { get; set; } public bool HorizontallyCenter { get; set; } public IPrintSetup PrintSetup { get; } public bool ForceFormulaRecalculation { get; set; } [Obsolete("Please use IsPrintGridlines instead")] public bool IsGridsPrinted { get; set; } public float DefaultRowHeightInPoints { get; set; } public short DefaultRowHeight { get; set; } public int DefaultColumnWidth { get; set; } public int LastRowNum { get; } public int FirstRowNum { get; } public int PhysicalNumberOfRows { get; } public int NumMergedRegions { get; } public IHeader Header { get; } public bool IsSelected { get; set; } public bool IsRightToLeft { get; set; } public string SheetName { get; } public IWorkbook Workbook { get; } public IList DVRecords { get; } public ISheetConditionalFormatting SheetConditionalFormatting { get; } public bool IsAutoTabColor { get; set; } public short TabColorIndex { get; set; } public IDrawing DrawingPatriarch { get; } public EscherAggregate DrawingEscherAggregate { get; } public int[] ColumnBreaks { get; } public int[] RowBreaks { get; } public IFooter Footer { get; } public bool DisplayRowColHeadings { get; set; } public bool DisplayGridlines { get; set; } public PaneInformation PaneInformation { get; } public short LeftCol { get; set; } public short TopRow { get; set; } public bool ScenarioProtect { get; } public bool ObjectProtect { get; } public int Password { get; } public bool Protect { get; } public bool IsActive { get; set; } public bool DisplayZeros { get; set; } public bool DisplayFormulas { get; set; } public CellRangeAddress RepeatingRows { get; set; } public CellRangeAddress RepeatingColumns { get; set; } public static CellRangeAddress GetMergedRegion(HSSFSheet sheet, int rowNum, short cellNum); [Obsolete] public int AddMergedRegion(SS.Util.Region region); public int AddMergedRegion(CellRangeAddress region); public void AddValidationData(IDataValidation dataValidation); public void AutoSizeColumn(int column); public void AutoSizeColumn(int column, bool useMergedCells); public ISheet CloneSheet(HSSFWorkbook workbook); public IRow CopyRow(int sourceIndex, int targetIndex); public ISheet CopySheet(string Name, bool copyStyle); public ISheet CopySheet(string Name); public ISheet CopySheet(bool CopyStyle); public ISheet CopySheet(); public void CopyTo(HSSFWorkbook dest, string name, bool copyStyle, bool keepFormulas); public IDrawing CreateDrawingPatriarch(); public void CreateFreezePane(int colSplit, int rowSplit, int leftmostColumn, int topRow); public void CreateFreezePane(int colSplit, int rowSplit); public IRow CreateRow(int rownum); public void CreateSplitPane(int xSplitPos, int ySplitPos, int leftmostColumn, int topRow, PanePosition activePane); public void DumpDrawingRecords(bool fat); public IComment GetCellComment(int row, int column); public int GetColumnOutlineLevel(int columnIndex); public ICellStyle GetColumnStyle(int column); public int GetColumnWidth(int column); public float GetColumnWidthInPixels(int column); public IDataValidationHelper GetDataValidationHelper(); public List<IDataValidation> GetDataValidations(); public IEnumerator GetEnumerator(); public double GetMargin(MarginType margin); public CellRangeAddress GetMergedRegion(int index); public IRow GetRow(int rowIndex); public IEnumerator GetRowEnumerator(); public void GroupColumn(int fromColumn, int toColumn); public void GroupRow(int fromRow, int toRow); public Font HSSFFont2Font(HSSFFont font1); public void InsertChartRecords(List<RecordBase> records); public bool IsColumnBroken(int column); public bool IsColumnHidden(int column); public bool IsMergedRegion(CellRangeAddress mergedRegion); public bool IsRowBroken(int row); public void ProtectSheet(string password); public ICellRange<ICell> RemoveArrayFormula(ICell cell); public void RemoveColumnBreak(int column); public void RemoveMergedRegion(int index); public void RemoveRow(IRow row); public void RemoveRowBreak(int row); public void SetActive(bool sel); public void SetActiveCell(int row, int column); public void SetActiveCellRange(List<CellRangeAddress8Bit> cellranges, int activeRange, int activeRow, int activeColumn); public void SetActiveCellRange(int firstRow, int lastRow, int firstColumn, int lastColumn); public ICellRange<ICell> SetArrayFormula(string formula, CellRangeAddress range); public IAutoFilter SetAutoFilter(CellRangeAddress range); public void SetBorderBottomOfRegion(CellRangeAddress region, BorderStyle borderType, short color); public void SetBorderLeftOfRegion(CellRangeAddress region, BorderStyle borderType, short color); public void SetBorderRightOfRegion(CellRangeAddress region, BorderStyle borderType, short color); public void SetBorderTopOfRegion(CellRangeAddress region, BorderStyle borderType, short color); public void SetColumnBreak(int column); public void SetColumnGroupCollapsed(int columnNumber, bool collapsed); public void SetColumnHidden(int column, bool hidden); public void SetColumnWidth(int column, int width); public void SetDefaultColumnStyle(int column, ICellStyle style); public void SetEnclosedBorderOfRegion(CellRangeAddress region, BorderStyle borderType, short color); public void SetMargin(MarginType margin, double size); public void SetRowBreak(int row); public void SetRowGroupCollapsed(int row, bool collapse); public void SetZoom(int numerator, int denominator); public void ShiftRows(int startRow, int endRow, int n); public void ShiftRows(int startRow, int endRow, int n, bool copyRowHeight, bool resetOriginalRowHeight); public void ShiftRows(int startRow, int endRow, int n, bool copyRowHeight, bool resetOriginalRowHeight, bool moveComments); public void ShowInPane(short toprow, short leftcol); public void ShowInPane(int toprow, int leftcol); public void UngroupColumn(int fromColumn, int toColumn); public void UngroupRow(int fromRow, int toRow); protected void ShiftMerged(int startRow, int endRow, int n, bool IsRow); protected void ValidateColumn(int column); protected void ValidateRow(int row); protected internal HSSFComment FindCellComment(int row, int column); }
蓝色的是私有属性,private List<ExcelRow> rows;
在你的程序中自然获取不到。
ISheet.GetRow(int index)可以获取你需要的行
你好,可是sheet是接口类型的,应该都是公有的,我进Isheet和HSSFSheet内部也确实找不到rows的字段
@鲁广广: 第一,你看的是ISheet的实例,接口是不能实例化的。第二,HSSFSheet内部一定有这个私有属性,只是你没找到。
@龙葛格: 你看我上面列出来HSSFSheet类了,根本就没有私有字段
@鲁广广: 私有属性在对象浏览器中不显示
namespace NPOI.HSSF.UserModel { using NPOI.DDF; using NPOI.HSSF.Model; using NPOI.HSSF.Record; using NPOI.HSSF.Record.Aggregates; using NPOI.HSSF.Record.AutoFilter; using NPOI.HSSF.Util; using NPOI.SS; using NPOI.SS.Formula; using NPOI.SS.Formula.PTG; using NPOI.SS.UserModel; using NPOI.SS.Util; using System; using System.Collections; using System.Collections.Generic; using System.Drawing; using System.Globalization; [Serializable] public class HSSFSheet : ISheet { [NonSerialized] private HSSFPatriarch _patriarch; private InternalSheet _sheet; protected HSSFWorkbook _workbook; public InternalWorkbook book; private int firstrow; public const int INITIAL_CAPACITY = 20; private int lastrow; private static float PX_DEFAULT = 32f; private static float PX_MODIFIED = 36.56f; private Dictionary<int, IRow> rows; public HSSFSheet(HSSFWorkbook workbook) { this._sheet = InternalSheet.CreateSheet(); this.rows = new Dictionary<int, IRow>(); this._workbook = workbook; this.book = workbook.Workbook; } public HSSFSheet(HSSFWorkbook workbook, InternalSheet sheet) { this._sheet = sheet; this.rows = new Dictionary<int, IRow>(); this._workbook = workbook; this.book = this._workbook.Workbook; this.SetPropertiesFromSheet(this._sheet); } public int AddMergedRegion(CellRangeAddress region) { region.Validate(SpreadsheetVersion.EXCEL97); this.ValidateArrayFormulas(region); return this._sheet.AddMergedRegion(region.FirstRow, region.FirstColumn, region.LastRow, region.LastColumn); } [Obsolete] public int AddMergedRegion(NPOI.SS.Util.Region region) { return this._sheet.AddMergedRegion(region.RowFrom, region.ColumnFrom, region.RowTo, region.ColumnTo); } private void AddRow(HSSFRow row, bool addLow) { this.rows[row.RowNum] = row; if (addLow) { this._sheet.AddRow(row.RowRecord); } bool flag = this.rows.Count == 1; if ((row.RowNum > this.LastRowNum) || flag) { this.lastrow = row.RowNum; } if ((row.RowNum < this.FirstRowNum) || flag) { this.firstrow = row.RowNum; } } public void AddValidationData(IDataValidation dataValidation) { if (dataValidation == null) { throw new ArgumentException("objValidation must not be null"); } HSSFDataValidation validation = (HSSFDataValidation) dataValidation; DataValidityTable orCreateDataValidityTable = this._sheet.GetOrCreateDataValidityTable(); DVRecord dvRecord = validation.CreateDVRecord(this); orCreateDataValidityTable.AddDataValidation(dvRecord); } private void ApplyEscherRemap(EscherRecord parent, Dictionary<int, int> mappings) { foreach (EscherRecord record in parent.ChildRecords) { if (record is EscherOptRecord) { EscherOptRecord record2 = (EscherOptRecord) record; foreach (EscherProperty property in record2.EscherProperties) { if (property.PropertyNumber == 260) { int propertyValue = ((EscherSimpleProperty) property).PropertyValue; if (mappings.ContainsKey(propertyValue)) { ((EscherSimpleProperty) property).PropertyValue = mappings[propertyValue]; } break; } } } if (record.ChildRecords.Count > 0) { foreach (EscherRecord record3 in record.ChildRecords) { this.ApplyEscherRemap(record3, mappings); } } } } private static bool AreAllTrue(params bool[] values) { for (int i = 0; i < values.Length; i++) { if (!values[i]) { return false; } } return true; } public void AutoSizeColumn(int column) { this.AutoSizeColumn(column, false); } public void AutoSizeColumn(int column, bool useMergedCells) { double num = SheetUtil.GetColumnWidth(this, column, useMergedCells); if (num != -1.0) { num *= 256.0; int num2 = 0xff00; if (num > num2) { num = num2; } this.SetColumnWidth(column, (int) num); } } public ISheet CloneSheet(HSSFWorkbook workbook) { IDrawing drawingPatriarch = this.DrawingPatriarch; HSSFSheet sheet = new HSSFSheet(workbook, this._sheet.CloneSheet()); int index = sheet._sheet.FindFirstRecordLocBySid(0xec); DrawingRecord item = (DrawingRecord) sheet._sheet.FindFirstRecordBySid(0xec); if (item != null) { sheet._sheet.Records.Remove(item); } if (this.DrawingPatriarch != null) { HSSFPatriarch patriarch = HSSFPatriarch.CreatePatriarch(this.DrawingPatriarch as HSSFPatriarch, sheet); sheet._sheet.Records.Insert(index, patriarch.GetBoundAggregate()); sheet._patriarch = patriarch; } return sheet; } [Obsolete] private static bool ContainsCell(CellRangeAddress cr, int rowIx, int colIx) { return (((cr.FirstRow <= rowIx) && (cr.LastRow >= rowIx)) && ((cr.FirstColumn <= colIx) && (cr.LastColumn >= colIx))); } public IRow CopyRow(int sourceIndex, int targetIndex) { return SheetUtil.CopyRow(this, sourceIndex, targetIndex); } private static void CopyRow(HSSFSheet srcSheet, HSSFSheet destSheet, HSSFRow srcRow, HSSFRow destRow, IDictionary<int, HSSFCellStyle> styleMap, Dictionary<short, short> paletteMap, bool keepFormulas) { List<CellRangeAddress> mergedRegions = destSheet.Sheet.MergedRecords.MergedRegions; destRow.Height = srcRow.Height; destRow.IsHidden = srcRow.IsHidden; destRow.RowRecord.OptionFlags = srcRow.RowRecord.OptionFlags; for (int i = srcRow.FirstCellNum; i <= srcRow.LastCellNum; i++) { HSSFCell oldCell = (HSSFCell) srcRow.GetCell(i); HSSFCell cell = (HSSFCell) destRow.GetCell(i); if (srcSheet.Workbook == destSheet.Workbook) { cell = (HSSFCell) destRow.GetCell(i); } if (oldCell != null) { if (cell == null) { cell = (HSSFCell) destRow.CreateCell(i); } HSSFCellUtil.CopyCell(oldCell, cell, styleMap, paletteMap, keepFormulas); CellRangeAddress address = GetMergedRegion(srcSheet, srcRow.RowNum, (short) oldCell.ColumnIndex); if (address != null) { CellRangeAddress newMergedRegion = new CellRangeAddress(address.FirstRow, address.LastRow, address.FirstColumn, address.LastColumn); if (IsNewMergedRegion(newMergedRegion, mergedRegions)) { mergedRegions.Add(newMergedRegion); } } } } } public ISheet CopySheet() { return this.CopySheet(this.SheetName + " - Copy", true); } public ISheet CopySheet(bool CopyStyle) { return this.CopySheet(this.SheetName + " - Copy", CopyStyle); } public ISheet CopySheet(string Name) { return this.CopySheet(Name, true); } public ISheet CopySheet(string Name, bool copyStyle) { int lastCellNum = 0; HSSFSheet destSheet = (HSSFSheet) this.Workbook.CreateSheet(Name); destSheet._sheet = this.Sheet.CloneSheet(); IDictionary<int, HSSFCellStyle> styleMap = copyStyle ? new Dictionary<int, HSSFCellStyle>() : null; for (int i = this.FirstRowNum; i <= this.LastRowNum; i++) { HSSFRow srcRow = (HSSFRow) this.GetRow(i); HSSFRow destRow = (HSSFRow) destSheet.CreateRow(i); if (srcRow != null) { CopyRow(this, destSheet, srcRow, destRow, styleMap, new Dictionary<short, short>(), true); if (srcRow.LastCellNum > lastCellNum) { lastCellNum = srcRow.LastCellNum; } } } for (int j = 0; j <= lastCellNum; j++) { destSheet.SetColumnWidth(j, this.GetColumnWidth(j)); } destSheet.ForceFormulaRecalculation = true; destSheet.PrintSetup.Landscape = this.PrintSetup.Landscape; destSheet.PrintSetup.HResolution = this.PrintSetup.HResolution; destSheet.PrintSetup.VResolution = this.PrintSetup.VResolution; destSheet.SetMargin(MarginType.LeftMargin, this.GetMargin(MarginType.LeftMargin)); destSheet.SetMargin(MarginType.RightMargin, this.GetMargin(MarginType.RightMargin)); destSheet.SetMargin(MarginType.TopMargin, this.GetMargin(MarginType.TopMargin)); destSheet.SetMargin(MarginType.BottomMargin, this.GetMargin(MarginType.BottomMargin)); destSheet.PrintSetup.HeaderMargin = this.PrintSetup.HeaderMargin; destSheet.PrintSetup.FooterMargin = this.PrintSetup.FooterMargin; destSheet.Header.Left = this.Header.Left; destSheet.Header.Center = this.Header.Center; destSheet.Header.Right = this.Header.Right; destSheet.Footer.Left = this.Footer.Left; destSheet.Footer.Center = this.Footer.Center; destSheet.Footer.Right = this.Footer.Right; destSheet.PrintSetup.Scale = this.PrintSetup.Scale; destSheet.PrintSetup.FitHeight = this.PrintSetup.FitHeight; destSheet.PrintSetup.FitWidth = this.PrintSetup.FitWidth; return destSheet; } public void CopyTo(HSSFWorkbook dest, string name, bool copyStyle, bool keepFormulas) { int lastCellNum = 0; HSSFSheet destSheet = (HSSFSheet) dest.CreateSheet(name); destSheet._sheet = this.Sheet.CloneSheet(); Dictionary<short, short> paletteMap = new Dictionary<short, short>(); if (dest.NumberOfSheets == 1) { dest.Workbook.CustomPalette.ClearColors(); paletteMap = MergePalettes(this.Workbook as HSSFWorkbook, dest); } else if (dest != this.Workbook) { paletteMap = MergePalettes(this.Workbook as HSSFWorkbook, dest); } IDictionary<int, HSSFCellStyle> styleMap = copyStyle ? new Dictionary<int, HSSFCellStyle>() : null; for (int i = this.FirstRowNum; i <= this.LastRowNum; i++) { HSSFRow srcRow = (HSSFRow) this.GetRow(i); HSSFRow destRow = (HSSFRow) destSheet.CreateRow(i); if (srcRow != null) { CopyRow(this, destSheet, srcRow, destRow, styleMap, paletteMap, keepFormulas); if (srcRow.LastCellNum > lastCellNum) { lastCellNum = srcRow.LastCellNum; } } } for (int j = 0; j < lastCellNum; j++) { destSheet.SetColumnWidth(j, this.GetColumnWidth(j)); } destSheet.ForceFormulaRecalculation = true; destSheet.PrintSetup.Landscape = this.PrintSetup.Landscape; destSheet.PrintSetup.HResolution = this.PrintSetup.HResolution; destSheet.PrintSetup.VResolution = this.PrintSetup.VResolution; destSheet.SetMargin(MarginType.LeftMargin, this.GetMargin(MarginType.LeftMargin)); destSheet.SetMargin(MarginType.RightMargin, this.GetMargin(MarginType.RightMargin)); destSheet.SetMargin(MarginType.TopMargin, this.GetMargin(MarginType.TopMargin)); destSheet.SetMargin(MarginType.BottomMargin, this.GetMargin(MarginType.BottomMargin)); destSheet.PrintSetup.HeaderMargin = this.PrintSetup.HeaderMargin; destSheet.PrintSetup.FooterMargin = this.PrintSetup.FooterMargin; destSheet.Header.Left = this.Header.Left; destSheet.Header.Center = this.Header.Center; destSheet.Header.Right = this.Header.Right; destSheet.Footer.Left = this.Footer.Left; destSheet.Footer.Center = this.Footer.Center; destSheet.Footer.Right = this.Footer.Right; destSheet.PrintSetup.Scale = this.PrintSetup.Scale; destSheet.PrintSetup.FitHeight = this.PrintSetup.FitHeight; destSheet.PrintSetup.FitWidth = this.PrintSetup.FitWidth; EscherAggregate drawingEscherAggregate = this.DrawingEscherAggregate; if (drawingEscherAggregate != null) { if (dest.Workbook.DrawingManager == null) { dest.Workbook.CreateDrawingGroup(); } EscherAggregate aggregate2 = destSheet.DrawingEscherAggregate; IEnumerable<int> enumerable = this.FindUsedPictures(drawingEscherAggregate.EscherRecords); Dictionary<int, int> mappings = new Dictionary<int, int>(); IList allPictures = this.Workbook.GetAllPictures(); foreach (int num4 in enumerable) { if (num4 <= allPictures.Count) { HSSFPictureData data = (HSSFPictureData) allPictures[num4 - 1]; int num5 = dest.AddPicture(data.Data, (PictureType) data.Format); mappings.Add(num4, num5); } } foreach (EscherRecord record in aggregate2.EscherRecords) { this.ApplyEscherRemap(record, mappings); } } } public IDrawing CreateDrawingPatriarch() { this._patriarch = this.GetPatriarch(true); return this._patriarch; } public void CreateFreezePane(int colSplit, int rowSplit) { this.CreateFreezePane(colSplit, rowSplit, colSplit, rowSplit); } public void CreateFreezePane(int colSplit, int rowSplit, int leftmostColumn, int topRow) { this.ValidateColumn(colSplit); this.ValidateRow(rowSplit); if (leftmostColumn < colSplit) { throw new ArgumentException("leftmostColumn parameter must not be less than colSplit parameter"); } if (topRow < rowSplit) { throw new ArgumentException("topRow parameter must not be less than leftmostColumn parameter"); } this.Sheet.CreateFreezePane(colSplit, rowSplit, topRow, leftmostColumn); } public IRow CreateRow(int rownum) { HSSFRow row = new HSSFRow(this._workbook, this, rownum); row.Height = this.DefaultRowHeight; row.RowRecord.BadFontHeight = false; this.AddRow(row, true); return row; } private HSSFRow CreateRowFromRecord(RowRecord row) { HSSFRow row2 = new HSSFRow(this._workbook, this, row); this.AddRow(row2, false); return row2; } } }
帮你找到了NPOI的源码,截取了一部分。有空可以自己找找看
@龙葛格: 原来在dll中在VS中查看是不显示私有字段,第一次知道。我一直以为和自己的定义的类一样可以查看。非常感谢,瞬间豁然开朗了
应该使用sheet.GetRow(0)获取第一行
IRow row = sheet.GetRow(rowIndex);
if (row == null)
row = sheet.CreateRow(rowIndex);
return row;
这样获取Row,兄弟,多试一试,看不到正常,楼上不是说了吗?private点不出来,但是进入isheet应该能看到的
关键是我是ISheet和HSSFSheet都找不到rows字段
@鲁广广: 我不太懂你想要干什么,你iSHEET点不出来,那你就看看这个isheet实例是什么类型的呗,肯定是isheet的衍生类实例呗,有isheet的外壳,但是内在还有别的东西,这个东西有row这个属性