sheetreader.cs
来自「Excel的操作,其中可以读取及写入Excel 文件」· CS 代码 · 共 930 行 · 第 1/2 页
CS
930 行
using System;
using System.Collections;
using Microsoft.Fawvw.Components.NExcel.ExcelCommon;
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> Reads the sheet. This functionality was originally part of the
/// SheetImpl class, but was separated out in order to simplify the former
/// class
/// </summary>
sealed class SheetReader
{
/// <summary> Accessor
///
/// </summary>
/// <returns> the number of rows
/// </returns>
internal int NumRows
{
get
{
return numRows;
}
}
/// <summary> Accessor
///
/// </summary>
/// <returns> the number of columns
/// </returns>
internal int NumCols
{
get
{
return numCols;
}
}
/// <summary> Accessor
///
/// </summary>
/// <returns> the cells
/// </returns>
internal Cell[][] Cells
{
get
{
return cells;
}
}
/// <summary> Accessor
///
/// </summary>
/// <returns> the row properties
/// </returns>
internal ArrayList RowProperties
{
get
{
return rowProperties;
}
}
/// <summary> Accessor
///
/// </summary>
/// <returns> the column information
/// </returns>
internal ArrayList ColumnInfosArray
{
get
{
return columnInfosArray;
}
}
/// <summary> Accessor
///
/// </summary>
/// <returns> the hyperlinks
/// </returns>
internal ArrayList Hyperlinks
{
get
{
return hyperlinks;
}
}
/// <summary> Accessor
///
/// </summary>
/// <returns> the charts
/// </returns>
internal ArrayList Charts
{
get
{
return charts;
}
}
/// <summary> Accessor
///
/// </summary>
/// <returns> the drawings
/// </returns>
internal ArrayList Drawings
{
get
{
return drawings;
}
}
/// <summary> Accessor
///
/// </summary>
/// <returns> the ranges
/// </returns>
internal Range[] MergedCells
{
get
{
return mergedCells;
}
}
/// <summary> Accessor
///
/// </summary>
/// <returns> the sheet settings
/// </returns>
internal SheetSettings Settings
{
get
{
return settings;
}
}
/// <summary> Accessor
///
/// </summary>
/// <returns> the row breaks
/// </returns>
internal int[] RowBreaks
{
get
{
return rowBreaks;
}
}
/// <summary> Accessor
///
/// </summary>
/// <returns> the workspace options
/// </returns>
internal WorkspaceInformationRecord WorkspaceOptions
{
get
{
return workspaceOptions;
}
}
/// <summary> Accessor
///
/// </summary>
/// <returns> the environment specific print record
/// </returns>
internal PLSRecord PLS
{
get
{
return plsRecord;
}
}
/// <summary> The logger</summary>
private static Logger logger;
/// <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 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 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 merged cells on this page</summary>
private Range[] mergedCells;
/// <summary> The list of charts on this page</summary>
private ArrayList charts;
/// <summary> The list of drawings on this page</summary>
private ArrayList drawings;
/// <summary> Indicates whether or not the dates are based around the 1904 date system</summary>
private bool nineteenFour;
/// <summary> The PLS print record</summary>
private PLSRecord plsRecord;
/// <summary> The workspace options</summary>
private WorkspaceInformationRecord workspaceOptions;
/// <summary> The horizontal page breaks contained on this sheet</summary>
private int[] rowBreaks;
/// <summary> The sheet settings</summary>
private SheetSettings settings;
/// <summary> The workbook settings</summary>
private WorkbookSettings workbookSettings;
/// <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 sheet</summary>
private SheetImpl sheet;
/// <summary> Constructor
///
/// </summary>
/// <param name="fr">the formatting records
/// </param>
/// <param name="sst">the shared string table
/// </param>
/// <param name="f">the excel file
/// </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="wp">the workbook which this sheet belongs to
/// </param>
/// <param name="sp">the start position of the sheet bof in the excel file
/// </param>
/// <param name="sh">the sheet
/// </param>
/// <param name="nf">1904 date record flag
/// </param>
/// <exception cref=""> BiffException
/// </exception>
internal SheetReader(File f, SSTRecord sst, FormattingRecords fr, BOFRecord sb, BOFRecord wb, bool nf, WorkbookParser wp, int sp, SheetImpl sh)
{
excelFile = f;
sharedStrings = sst;
formattingRecords = fr;
sheetBof = sb;
workbookBof = wb;
columnInfosArray = new ArrayList();
sharedFormulas = new ArrayList();
hyperlinks = new ArrayList();
rowProperties = new ArrayList(10);
charts = new ArrayList();
drawings = new ArrayList();
nineteenFour = nf;
workbook = wp;
startPosition = sp;
sheet = sh;
settings = new SheetSettings();
workbookSettings = workbook.Settings;
}
/// <summary> Adds the cell to the array
///
/// </summary>
/// <param name="cell">the cell to add
/// </param>
private void addCell(Cell cell)
{
// Sometimes multiple cells (eg. MULBLANK) can exceed the
// column/row boundaries. Ignore these
if (cell.Row < numRows && cell.Column < numCols)
{
if (cells[cell.Row][cell.Column] != null)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
Microsoft.Fawvw.Components.NExcel.CellReferenceHelper.getCellReference(cell.Column, cell.Row, sb);
logger.warn("Cell " + sb.ToString() + " already contains data");
}
cells[cell.Row][cell.Column] = cell;
}
}
/// <summary> Reads in the contents of this sheet</summary>
internal void read()
{
Record r = null;
BaseSharedFormulaRecord sharedFormula = null;
bool sharedFormulaAdded = false;
bool cont = true;
// Set the position within the file
excelFile.Pos = startPosition;
// Handles to the last drawing and obj records
MsoDrawingRecord msoRecord = null;
ObjRecord objRecord = null;
// A handle to window2 record
Window2Record window2Record = null;
// A handle to printgridlines record
PrintGridLinesRecord printGridLinesRecord = null;
// A handle to printheaders record
PrintHeadersRecord printHeadersRecord = null;
while (cont)
{
r = excelFile.next();
if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.UNKNOWN && r.Code == 0)
{
//System.Console.Error.Write("Warning: biff code zero found");
// Try a dimension record
if (r.Length == 0xa)
{
logger.warn("Biff code zero found - trying a dimension record.");
r.Type = (Microsoft.Fawvw.Components.NExcel.Biff.Type.DIMENSION);
}
else
{
logger.warn("Biff code zero found - Ignoring.");
}
}
if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.DIMENSION)
{
DimensionRecord dr = null;
if (workbookBof.isBiff8())
{
dr = new DimensionRecord(r);
}
else
{
dr = new DimensionRecord(r, DimensionRecord.biff7);
}
numRows = dr.NumberOfRows;
numCols = dr.NumberOfColumns;
cells = new Cell[numRows][];
for (int i = 0; i < numRows; i++)
{
cells[i] = new Cell[numCols];
}
}
else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.LABELSST)
{
LabelSSTRecord label = new LabelSSTRecord(r, sharedStrings, formattingRecords, sheet);
addCell(label);
}
else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.RK || r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.RK2)
{
RKRecord rkr = new RKRecord(r, formattingRecords, sheet);
if (formattingRecords.isDate(rkr.XFIndex))
{
DateCell dc = new DateRecord(rkr, rkr.XFIndex, formattingRecords, nineteenFour, sheet);
addCell(dc);
}
else
{
addCell(rkr);
}
}
else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.HLINK)
{
HyperlinkRecord hr = new HyperlinkRecord(r, sheet, workbookSettings);
hyperlinks.Add(hr);
}
else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.MERGEDCELLS)
{
MergedCellsRecord mc = new MergedCellsRecord(r, sheet);
if (mergedCells == null)
{
mergedCells = mc.Ranges;
}
else
{
Range[] newMergedCells = new Range[mergedCells.Length + mc.Ranges.Length];
Array.Copy(mergedCells, 0, newMergedCells, 0, mergedCells.Length);
Array.Copy(mc.Ranges, 0, newMergedCells, mergedCells.Length, mc.Ranges.Length);
mergedCells = newMergedCells;
}
}
else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.MULRK)
{
MulRKRecord mulrk = new MulRKRecord(r);
// Get the individual cell records from the multiple record
int num = mulrk.NumberOfColumns;
int ixf = 0;
for (int i = 0; i < num; i++)
{
ixf = mulrk.getXFIndex(i);
NumberValue nv = new NumberValue(mulrk.Row, mulrk.FirstColumn + i, RKHelper.getDouble(mulrk.getRKNumber(i)), ixf, formattingRecords, sheet);
if (formattingRecords.isDate(ixf))
{
DateCell dc = new DateRecord(nv, ixf, formattingRecords, nineteenFour, sheet);
addCell(dc);
}
else
{
nv.setNumberFormat(formattingRecords.getNumberFormat(ixf));
addCell(nv);
}
}
}
else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.NUMBER)
{
NumberRecord nr = new NumberRecord(r, formattingRecords, sheet);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?