sheetimpl.cs

来自「Excel的操作,其中可以读取及写入Excel 文件」· CS 代码 · 共 884 行 · 第 1/2 页

CS
884
字号
using System;
using System.Collections;

using Microsoft.Fawvw.Components.NExcel.Biff;
using Microsoft.Fawvw.Components.NExcel.Biff.Drawing;
using Microsoft.Fawvw.Components.NExcel.Format;

namespace Microsoft.Fawvw.Components.NExcel.Read.Biff
{
	
	/// <summary> Represents a sheet within a workbook.  Provides a handle to the individual
	/// cells, or lines of cells (grouped by Row or Column)
	/// In order to simplify this class due to code bloat, the actual reading
	/// logic has been delegated to the SheetReaderClass.  This class' main
	/// responsibility is now to implement the API methods declared in the
	/// Sheet interface
	/// </summary>
	public class SheetImpl : Sheet
	{
		/// <summary> Returns the number of rows in this sheet
		/// 
		/// </summary>
		/// <returns> the number of rows in this sheet
		/// </returns>
		virtual public int Rows
		{
			get
			{
				// just in case this has been cleared, but something else holds
				// a reference to it
				if (cells == null)
				{
					readSheet();
				}
				
				return numRows;
			}
			
		}
		/// <summary> Returns the number of columns in this sheet
		/// 
		/// </summary>
		/// <returns> the number of columns in this sheet
		/// </returns>
		virtual public int Columns
		{
			get
			{
				// just in case this has been cleared, but something else holds
				// a reference to it
				if (cells == null)
				{
					readSheet();
				}
				
				return numCols;
			}
			
		}
		/// <summary> Gets all the column info records
		/// 
		/// </summary>
		/// <returns> the ColumnInfoRecordArray
		/// </returns>
		virtual public ColumnInfoRecord[] ColumnInfos
		{
			get
			{
				// Just chuck all the column infos we have into an array
				ColumnInfoRecord[] infos = new ColumnInfoRecord[columnInfosArray.Count];
				for (int i = 0; i < columnInfosArray.Count; i++)
				{
					infos[i] = (ColumnInfoRecord) columnInfosArray[i];
				}
				
				return infos;
			}
			
		}
		/// <summary> Gets the hyperlinks on this sheet
		/// 
		/// </summary>
		/// <returns> an array of hyperlinks
		/// </returns>
		virtual public Hyperlink[] Hyperlinks
		{
			get
			{
				Hyperlink[] hl = new Hyperlink[hyperlinks.Count];
				
				for (int i = 0; i < hyperlinks.Count; i++)
				{
					hl[i] = (Hyperlink) hyperlinks[i];
				}
				
				return hl;
			}
			
		}
		/// <summary> Gets the cells which have been merged on this sheet
		/// 
		/// </summary>
		/// <returns> an array of range objects
		/// </returns>
		virtual public Range[] MergedCells
		{
			get
			{
				if (mergedCells == null)
				{
					return new Range[0];
				}
				
				return mergedCells;
			}
			
		}
		/// <summary> Gets the non-default rows.  Used when copying spreadsheets
		/// 
		/// </summary>
		/// <returns> an array of row properties
		/// </returns>
		virtual public RowRecord[] RowProperties
		{
			get
			{
				RowRecord[] rp = new RowRecord[rowProperties.Count];
				for (int i = 0; i < rp.Length; i++)
				{
					rp[i] = (RowRecord) rowProperties[i];
				}
				
				return rp;
			}
			
		}
		/// <summary> Gets the row breaks.  Called when copying sheets
		/// 
		/// </summary>
		/// <returns> the explicit row breaks
		/// </returns>
		virtual public int[] RowPageBreaks
		{
			get
			{
				return rowBreaks;
			}
			
		}
		/// <summary> Gets the charts.  Called when copying sheets
		/// 
		/// </summary>
		/// <returns> the charts on this page
		/// </returns>
		virtual public Chart[] Charts
		{
			get
			{
				Chart[] ch = new Chart[charts.Count];
				
				for (int i = 0; i < ch.Length; i++)
				{
					ch[i] = (Chart) charts[i];
				}
				return ch;
			}
			
		}
		/// <summary> Gets the drawings.  Called when copying sheets
		/// 
		/// </summary>
		/// <returns> the drawings on this page
		/// </returns>
		virtual public Drawing[] Drawings
		{
			get
			{
				System.Object[] dr = drawings.ToArray();
				Drawing[] dr2 = new Drawing[dr.Length];
				Array.Copy(dr, 0, dr2, 0, dr.Length);
				return dr2;
			}
			
		}
		/// <summary> Determines whether the sheet is protected
		/// 
		/// </summary>
		/// <returns> whether or not the sheet is protected
		/// </returns>
		/// <deprecated> in favour of the getSettings() api
		/// </deprecated>
		virtual public bool Protected
		{
			get
			{
				return settings.Protected;
			}
			
		}
		/// <summary> Gets the workspace options for this sheet.  Called during the copy
		/// process
		/// 
		/// </summary>
		/// <returns> the workspace options
		/// </returns>
		virtual public WorkspaceInformationRecord WorkspaceOptions
		{
			get
			{
				return workspaceOptions;
			}
			
		}
		/// <summary> Accessor for the sheet settings
		/// 
		/// </summary>
		/// <returns> the settings for this sheet
		/// </returns>
		virtual public SheetSettings Settings
		{
			get
			{
				return settings;
			}
			
		}
		/// <summary> Accessor for the workbook</summary>
		/// <returns>  the workbook
		/// </returns>
		virtual internal WorkbookParser Workbook
		{
			get
			{
				return workbook;
			}
			
		}
		/// <summary> Used when copying sheets in order to determine the type of this sheet
		/// 
		/// </summary>
		/// <returns> the BOF Record
		/// </returns>
		virtual public BOFRecord SheetBof
		{
			get
			{
				return sheetBof;
			}
			
		}
		/// <summary> Accessor for the environment specific print record, invoked when
		/// copying sheets
		/// 
		/// </summary>
		/// <returns> the environment specific print record
		/// </returns>
		virtual public PLSRecord PLS
		{
			get
			{
				return plsRecord;
			}
			
		}
		/// <summary> The excel file</summary>
		private File excelFile;
		/// <summary> A handle to the shared string table</summary>
		private SSTRecord sharedStrings;
		
		/// <summary> A handle to the sheet BOF record, which indicates the stream type</summary>
		private BOFRecord sheetBof;
		
		/// <summary> A handle to the workbook BOF record, which indicates the stream type</summary>
		private BOFRecord workbookBof;
		
		/// <summary> A handle to the formatting records</summary>
		private FormattingRecords formattingRecords;
		
		/// <summary> The name of this sheet</summary>
		private string name;
		
		/// <summary> The  number of rows</summary>
		private int numRows;
		
		/// <summary> The number of columns</summary>
		private int numCols;
		
		/// <summary> The cells</summary>
		private Cell[][] cells;
		
		/// <summary> The start position in the stream of this sheet</summary>
		private int startPosition;
		
		/// <summary> The list of specified (ie. non default) column widths</summary>
		private ColumnInfoRecord[] columnInfos;
		
		/// <summary> The array of row records</summary>
		private RowRecord[] rowRecords;
		
		/// <summary> The list of non-default row properties</summary>
		private ArrayList rowProperties;
		
		/// <summary> An array of column info records.  They are held this way before
		/// they are transferred to the more convenient array
		/// </summary>
		private ArrayList columnInfosArray;
		
		/// <summary> A list of shared formula groups</summary>
		private ArrayList sharedFormulas;
		
		/// <summary> A list of hyperlinks on this page</summary>
		private ArrayList hyperlinks;
		
		/// <summary> A list of charts on this page</summary>
		private ArrayList charts;
		
		/// <summary> A list of drawings on this page</summary>
		private ArrayList drawings;
		
		/// <summary> A list of merged cells on this page</summary>
		private Range[] mergedCells;
		
		/// <summary> Indicates whether the columnInfos array has been initialized</summary>
		private bool columnInfosInitialized;
		
		/// <summary> Indicates whether the rowRecords array has been initialized</summary>
		private bool rowRecordsInitialized;
		
		/// <summary> Indicates whether or not the dates are based around the 1904 date system</summary>
		private bool nineteenFour;
		
		/// <summary> The workspace options</summary>
		private WorkspaceInformationRecord workspaceOptions;
		
		/// <summary> The hidden flag</summary>
		private bool hidden;
		
		/// <summary> The environment specific print record</summary>
		private PLSRecord plsRecord;
		
		/// <summary> The sheet settings</summary>
		private SheetSettings settings;
		
		/// <summary> The horizontal page breaks contained on this sheet</summary>
		private int[] rowBreaks;
		
		/// <summary> A handle to the workbook which contains this sheet.  Some of the records
		/// need this in order to reference external sheets
		/// </summary>
		private WorkbookParser workbook;
		
		/// <summary> A handle to the workbook settings</summary>
		private WorkbookSettings workbookSettings;
		
		/// <summary> Constructor
		/// 
		/// </summary>
		/// <param name="f">the excel file
		/// </param>
		/// <param name="sst">the shared string table
		/// </param>
		/// <param name="fr">formatting records
		/// </param>
		/// <param name="sb">the bof record which indicates the start of the sheet
		/// </param>
		/// <param name="wb">the bof record which indicates the start of the sheet
		/// </param>
		/// <param name="nf">the 1904 flag
		/// </param>
		/// <param name="wp">the workbook which this sheet belongs to
		/// </param>
		/// <exception cref=""> BiffException
		/// </exception>
		internal SheetImpl(File f, SSTRecord sst, FormattingRecords fr, BOFRecord sb, BOFRecord wb, bool nf, WorkbookParser wp)
		{
			excelFile = f;
			sharedStrings = sst;
			formattingRecords = fr;
			sheetBof = sb;
			workbookBof = wb;
			columnInfosArray = new ArrayList();
			sharedFormulas = new ArrayList();
			hyperlinks = new ArrayList();
			rowProperties = new ArrayList(10);
			columnInfosInitialized = false;
			rowRecordsInitialized = false;
			nineteenFour = nf;
			workbook = wp;
			workbookSettings = workbook.Settings;
			
			// Mark the position in the stream, and then skip on until the end
			startPosition = f.Pos;
			
			if (sheetBof.isChart())
			{
				// Set the start pos to include the bof so the sheet reader can handle it
				startPosition -= (sheetBof.Length + 4);
			}
			
			Record r = null;
			int bofs = 1;
			
			while (bofs >= 1)
			{
				r = f.next();
				
				// use this form for quick performance
				if (r.Code == Microsoft.Fawvw.Components.NExcel.Biff.Type.EOF.Value)
				{
					bofs--;
				}
				
				if (r.Code == Microsoft.Fawvw.Components.NExcel.Biff.Type.BOF.Value)
				{
					bofs++;
				}
			}
		}
		
		/// <summary> Returns the cell specified at this row and at this column
		/// 
		/// </summary>
		/// <param name="row">the row number
		/// </param>
		/// <param name="column">the column number
		/// </param>
		/// <returns> the cell at the specified co-ordinates
		/// </returns>
		public virtual Cell getCell(int column, int row)
		{
			// just in case this has been cleared, but something else holds
			// a reference to it
			if (cells == null)
			{
				readSheet();
			}
			
			Cell c = cells[row][column];
			
			if (c == null)
			{
				c = new EmptyCell(column, row);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?