sheetimpl.cs
来自「Excel的操作,其中可以读取及写入Excel 文件」· CS 代码 · 共 884 行 · 第 1/2 页
CS
884 行
cells[row][column] = c;
}
return c;
}
/// <summary> Gets the cell whose contents match the string passed in.
/// If no match is found, then null is returned. The search is performed
/// on a row by row basis, so the lower the row number, the more
/// efficiently the algorithm will perform
///
/// </summary>
/// <param name="contents">the string to match
/// </param>
/// <returns> the Cell whose contents match the paramter, null if not found
/// </returns>
public virtual Cell findCell(string contents)
{
Cell cell = null;
bool found = false;
for (int i = 0; i < Rows && !found; i++)
{
Cell[] row = getRow(i);
for (int j = 0; j < row.Length && !found; j++)
{
if (row[j].Contents.Equals(contents))
{
cell = row[j];
found = true;
}
}
}
return cell;
}
/// <summary> Gets the cell whose contents match the string passed in.
/// If no match is found, then null is returned. The search is performed
/// on a row by row basis, so the lower the row number, the more
/// efficiently the algorithm will perform. This method differs
/// from the findCell methods in that only cells with labels are
/// queried - all numerical cells are ignored. This should therefore
/// improve performance.
///
/// </summary>
/// <param name="contents">the string to match
/// </param>
/// <returns> the Cell whose contents match the paramter, null if not found
/// </returns>
public virtual LabelCell findLabelCell(string contents)
{
LabelCell cell = null;
bool found = false;
for (int i = 0; i < Rows && !found; i++)
{
Cell[] row = getRow(i);
for (int j = 0; j < row.Length && !found; j++)
{
if ((row[j].Type == CellType.LABEL || row[j].Type == CellType.STRING_FORMULA) && row[j].Contents.Equals(contents))
{
cell = (LabelCell) row[j];
found = true;
}
}
}
return cell;
}
/// <summary> Gets all the cells on the specified row. The returned array will
/// be stripped of all trailing empty cells
///
/// </summary>
/// <param name="row">the rows whose cells are to be returned
/// </param>
/// <returns> the cells on the given row
/// </returns>
public virtual Cell[] getRow(int row)
{
// just in case this has been cleared, but something else holds
// a reference to it
if (cells == null)
{
readSheet();
}
// Find the last non-null cell
bool found = false;
int col = numCols - 1;
while (col >= 0 && !found)
{
if (cells[row][col] != null)
{
found = true;
}
else
{
col--;
}
}
// Only create entries for non-null cells
Cell[] c = new Cell[col + 1];
for (int i = 0; i <= col; i++)
{
c[i] = getCell(i, row);
}
return c;
}
/// <summary> Gets all the cells on the specified column. The returned array
/// will be stripped of all trailing empty cells
///
/// </summary>
/// <param name="col">the column whose cells are to be returned
/// </param>
/// <returns> the cells on the specified column
/// </returns>
public virtual Cell[] getColumn(int col)
{
// just in case this has been cleared, but something else holds
// a reference to it
if (cells == null)
{
readSheet();
}
// Find the last non-null cell
bool found = false;
int row = numRows - 1;
while (row >= 0 && !found)
{
if (cells[row][col] != null)
{
found = true;
}
else
{
row--;
}
}
// Only create entries for non-null cells
Cell[] c = new Cell[row + 1];
for (int i = 0; i <= row; i++)
{
c[i] = getCell(col, i);
}
return c;
}
/// <summary> Gets the name of this sheet
///
/// </summary>
/// <returns> the name of the sheet
/// </returns>
public virtual string Name
{
get
{
return name;
}
}
/// <summary> Sets the name of this sheet
///
/// </summary>
/// <param name="s">the sheet name
/// </param>
internal void setName(string s)
{
name = s;
}
/// <summary> Determines whether the sheet is hidden
///
/// </summary>
/// <returns> whether or not the sheet is hidden
/// </returns>
/// <deprecated> in favour of the getSettings function
/// </deprecated>
public virtual bool Hidden
{
get
{
return hidden;
}
set
{
hidden = value;
}
}
/// <summary> Gets the column info record for the specified column. If no
/// column is specified, null is returned
///
/// </summary>
/// <param name="col">the column
/// </param>
/// <returns> the ColumnInfoRecord if specified, NULL otherwise
/// </returns>
public virtual ColumnInfoRecord getColumnInfo(int col)
{
if (!columnInfosInitialized)
{
// Initialize the array
foreach(ColumnInfoRecord cir in columnInfosArray)
{
int startcol = Math.Max(0, cir.StartColumn);
int endcol = Math.Min(columnInfos.Length - 1, cir.EndColumn);
for (int c = startcol; c <= endcol; c++)
{
columnInfos[c] = cir;
}
if (endcol < startcol)
{
columnInfos[startcol] = cir;
}
}
columnInfosInitialized = true;
}
return col < columnInfos.Length?columnInfos[col]:null;
}
// /// <summary> Sets the visibility of this sheet
// ///
// /// </summary>
// /// <param name="h">hidden flag
// /// </param>
// internal void setHidden(bool h)
// {
// hidden = h;
// }
/// <summary> Clears out the array of cells. This is done for memory allocation
/// reasons when reading very large sheets
/// </summary>
internal void clear()
{
cells = null;
mergedCells = null;
columnInfosArray.Clear();
sharedFormulas.Clear();
hyperlinks.Clear();
columnInfosInitialized = false;
if (!workbookSettings.GCDisabled)
{
System.GC.Collect();
}
}
/// <summary> Reads in the contents of this sheet</summary>
internal void readSheet()
{
// If this sheet contains only a chart, then set everything to
// empty and do not bother parsing the sheet
// Thanks to steve.brophy for spotting this
if (!sheetBof.isWorksheet())
{
numRows = 0;
numCols = 0;
cells = new Cell[0][];
for (int i = 0; i < 0; i++)
{
cells[i] = new Cell[0];
}
// return;
}
SheetReader reader = new SheetReader(excelFile, sharedStrings, formattingRecords, sheetBof, workbookBof, nineteenFour, workbook, startPosition, this);
reader.read();
// Take stuff that was read in
numRows = reader.NumRows;
numCols = reader.NumCols;
cells = reader.Cells;
rowProperties = reader.RowProperties;
columnInfosArray = reader.ColumnInfosArray;
hyperlinks = reader.Hyperlinks;
charts = reader.Charts;
drawings = reader.Drawings;
mergedCells = reader.MergedCells;
settings = reader.Settings;
settings.Hidden = hidden;
rowBreaks = reader.RowBreaks;
workspaceOptions = reader.WorkspaceOptions;
plsRecord = reader.PLS;
reader = null;
if (!workbookSettings.GCDisabled)
{
System.GC.Collect();
}
if (columnInfosArray.Count > 0)
{
ColumnInfoRecord cir = (ColumnInfoRecord) columnInfosArray[columnInfosArray.Count - 1];
columnInfos = new ColumnInfoRecord[cir.EndColumn + 1];
}
else
{
columnInfos = new ColumnInfoRecord[0];
}
}
/// <summary> Gets the row record. Usually called by the cell in the specified
/// row in order to determine its size
///
/// </summary>
/// <param name="r">the row
/// </param>
/// <returns> the RowRecord for the specified row
/// </returns>
internal virtual RowRecord getRowInfo(int r)
{
if (!rowRecordsInitialized)
{
rowRecords = new RowRecord[Rows];
foreach(RowRecord rr in rowProperties)
{
rowRecords[rr.RowNumber] = rr;
}
}
return rowRecords[r];
}
/// <summary> Gets the column format for the specified column
///
/// </summary>
/// <param name="col">the column number
/// </param>
/// <returns> the column format, or NULL if the column has no specific format
/// </returns>
/// <deprecated> use getColumnView instead
/// </deprecated>
public virtual Microsoft.Fawvw.Components.NExcel.Format.CellFormat getColumnFormat(int col)
{
CellView cv = getColumnView(col);
return cv.Format;
}
/// <summary> Gets the column width for the specified column
///
/// </summary>
/// <param name="col">the column number
/// </param>
/// <returns> the column width, or the default width if the column has no
/// specified format
/// </returns>
public virtual int getColumnWidth(int col)
{
return this.getColumnView(col).Size / 256;
}
/// <summary> Gets the column width for the specified column
///
/// </summary>
/// <param name="col">the column number
/// </param>
/// <returns> the column format, or the default format if no override is
/// specified
/// </returns>
public virtual CellView getColumnView(int col)
{
ColumnInfoRecord cir = getColumnInfo(col);
CellView cv = new CellView();
if (cir != null)
{
cv.Dimension = (cir.Width / 256); //deprecated
cv.Size = cir.Width;
cv.Hidden = cir.Hidden;
cv.Format = formattingRecords.getXFRecord(cir.XFIndex);
}
else
{
cv.Dimension = (settings.DefaultColumnWidth / 256); //deprecated
cv.Size = (settings.DefaultColumnWidth);
}
return cv;
}
/// <summary> Gets the row height for the specified column
///
/// </summary>
/// <param name="row">the row number
/// </param>
/// <returns> the row height, or the default height if the row has no
/// specified format
/// </returns>
/// <deprecated> use getRowView instead
/// </deprecated>
public virtual int getRowHeight(int row)
{
return getRowView(row).Dimension;
}
/// <summary> Gets the row view for the specified row
///
/// </summary>
/// <param name="row">the row number
/// </param>
/// <returns> the row format, or the default format if no override is
/// specified
/// </returns>
public virtual CellView getRowView(int row)
{
RowRecord rr = getRowInfo(row);
CellView cv = new CellView();
if (rr != null)
{
cv.Dimension = (rr.RowHeight); //deprecated
cv.Size = (rr.RowHeight);
cv.Hidden = rr.isCollapsed();
}
else
{
cv.Dimension = (settings.DefaultRowHeight);
cv.Size = (settings.DefaultRowHeight); //deprecated
}
return cv;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?