sheetreader.cs

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

CS
930
字号
					if (formattingRecords.isDate(nr.XFIndex))
					{
						DateCell dc = new DateRecord(nr, nr.XFIndex, formattingRecords, nineteenFour, sheet);
						addCell(dc);
					}
					else
					{
						addCell(nr);
					}
				}
				else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.BOOLERR)
				{
					BooleanRecord br = new BooleanRecord(r, formattingRecords, sheet);
					
					if (br.Error)
					{
						ErrorRecord er = new ErrorRecord(br.getRecord(), formattingRecords, sheet);
						addCell(er);
					}
					else
					{
						addCell(br);
					}
				}
				else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.PRINTGRIDLINES)
				{
					printGridLinesRecord = new PrintGridLinesRecord(r);
					settings.PrintGridLines = (printGridLinesRecord.PrintGridLines);
				}
				else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.PRINTHEADERS)
				{
					printHeadersRecord = new PrintHeadersRecord(r);
					settings.PrintHeaders = (printHeadersRecord.PrintHeaders);
				}
				else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.WINDOW2)
				{
					window2Record = new Window2Record(r);
					
					settings.ShowGridLines = (window2Record.ShowGridLines);
					settings.DisplayZeroValues = (window2Record.DisplayZeroValues);
					settings.setSelected();
				}
				else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.PANE)
				{
					PaneRecord pr = new PaneRecord(r);
					
					if (window2Record != null && window2Record.Frozen && window2Record.FrozenNotSplit)
					{
						settings.VerticalFreeze = (pr.RowsVisible);
						settings.HorizontalFreeze = (pr.ColumnsVisible);
					}
				}
				else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.CONTINUE)
				{
					;
				}
				else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.NOTE)
				{
					;
				}
				else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.ARRAY)
				{
					;
				}
				else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.PROTECT)
				{
					ProtectRecord pr = new ProtectRecord(r);
					settings.Protected=(pr.IsProtected());
				}
				else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.SHAREDFORMULA)
				{
					if (sharedFormula == null)
					{
						logger.warn("Shared template formula is null - " + "trying most recent formula template");
						SharedFormulaRecord lastSharedFormula = (SharedFormulaRecord) sharedFormulas[sharedFormulas.Count - 1];
						
						if (lastSharedFormula != null)
						{
							sharedFormula = lastSharedFormula.TemplateFormula;
						}
					}
					
					SharedFormulaRecord sfr = new SharedFormulaRecord(r, sharedFormula, workbook, workbook, sheet);
					sharedFormulas.Add(sfr);
					sharedFormula = null;
				}
				else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.FORMULA || r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.FORMULA2)
				{
					FormulaRecord fr = new FormulaRecord(r, excelFile, formattingRecords, workbook, workbook, sheet, workbookSettings);
					
					if (fr.Shared)
					{
						BaseSharedFormulaRecord prevSharedFormula = sharedFormula;
						sharedFormula = (BaseSharedFormulaRecord) fr.Formula;
						
						// See if it fits in any of the shared formulas
						sharedFormulaAdded = addToSharedFormulas(sharedFormula);
						
						if (sharedFormulaAdded)
						{
							sharedFormula = prevSharedFormula;
						}
						
						// If we still haven't added the previous base shared formula,
						// revert it to an ordinary formula and add it to the cell
						if (!sharedFormulaAdded && prevSharedFormula != null)
						{
							// Do nothing.  It's possible for the biff file to contain the
							// record sequence
							// FORMULA-SHRFMLA-FORMULA-SHRFMLA-FORMULA-FORMULA-FORMULA
							// ie. it first lists all the formula templates, then it
							// lists all the individual formulas
							addCell(revertSharedFormula(prevSharedFormula));
						}
					}
					else
					{
						Cell cell = fr.Formula;
						
						// See if the formula evaluates to date
						if (fr.Formula.Type == CellType.NUMBER_FORMULA)
						{
							NumberFormulaRecord nfr = (NumberFormulaRecord) fr.Formula;
							if (formattingRecords.isDate(nfr.XFIndex))
							{
								cell = new DateFormulaRecord(nfr, formattingRecords, workbook, workbook, nineteenFour, sheet);
							}
						}
						
						addCell(cell);
					}
				}
				else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.LABEL)
				{
					LabelRecord lr = null;
					
					if (workbookBof.isBiff8())
					{
						lr = new LabelRecord(r, formattingRecords, sheet, workbookSettings);
					}
					else
					{
						lr = new LabelRecord(r, formattingRecords, sheet, workbookSettings, LabelRecord.biff7);
					}
					addCell(lr);
				}
				else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.RSTRING)
				{
					RStringRecord lr = null;
					
					// RString records are obsolete in biff 8
					Assert.verify(!workbookBof.isBiff8());
					lr = new RStringRecord(r, formattingRecords, sheet, workbookSettings, RStringRecord.biff7);
					addCell(lr);
				}
				else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.NAME)
				{
					;
				}
				else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.PASSWORD)
				{
					PasswordRecord pr = new PasswordRecord(r);
					settings.PasswordHash=(pr.PasswordHash);
				}
				else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.ROW)
				{
					RowRecord rr = new RowRecord(r);
					
					// See if the row has anything funny about it
					if (!rr.isDefaultHeight() || rr.isCollapsed() || rr.isZeroHeight())
					{
						rowProperties.Add(rr);
					}
				}
				else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.BLANK)
				{
					BlankCell bc = new BlankCell(r, formattingRecords, sheet);
					addCell(bc);
				}
				else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.MULBLANK)
				{
					MulBlankRecord mulblank = new MulBlankRecord(r);
					
					// Get the individual cell records from the multiple record
					int num = mulblank.NumberOfColumns;
					
					for (int i = 0; i < num; i++)
					{
						int ixf = mulblank.getXFIndex(i);
						
						MulBlankCell mbc = new MulBlankCell(mulblank.Row, mulblank.FirstColumn + i, ixf, formattingRecords, sheet);
						
						addCell(mbc);
					}
				}
				else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.SCL)
				{
					SCLRecord scl = new SCLRecord(r);
					settings.ZoomFactor = (scl.ZoomFactor);
				}
				else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.COLINFO)
				{
					ColumnInfoRecord cir = new ColumnInfoRecord(r);
					columnInfosArray.Add(cir);
				}
				else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.HEADER)
				{
					HeaderRecord hr = null;
					if (workbookBof.isBiff8())
					{
						hr = new HeaderRecord(r, workbookSettings);
					}
					else
					{
						hr = new HeaderRecord(r, workbookSettings, HeaderRecord.biff7);
					}
					
					Microsoft.Fawvw.Components.NExcel.HeaderFooter header = new Microsoft.Fawvw.Components.NExcel.HeaderFooter(hr.Header);
					settings.Header = (header);
				}
				else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.FOOTER)
				{
					FooterRecord fr = null;
					if (workbookBof.isBiff8())
					{
						fr = new FooterRecord(r, workbookSettings);
					}
					else
					{
						fr = new FooterRecord(r, workbookSettings, FooterRecord.biff7);
					}
					
					Microsoft.Fawvw.Components.NExcel.HeaderFooter footer = new Microsoft.Fawvw.Components.NExcel.HeaderFooter(fr.Footer);
					settings.Footer=(footer);
				}
				else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.SETUP)
				{
					SetupRecord sr = new SetupRecord(r);
					if (sr.isPortrait())
					{
						settings.Orientation=(PageOrientation.PORTRAIT);
					}
					else
					{
						settings.Orientation=(PageOrientation.LANDSCAPE);
					}
					settings.PaperSize = (PaperSize.getPaperSize(sr.PaperSize));
					settings.HeaderMargin = (sr.HeaderMargin);
					settings.FooterMargin = (sr.FooterMargin);
					settings.ScaleFactor = (sr.ScaleFactor);
					settings.PageStart = (sr.PageStart);
					settings.FitWidth = (sr.FitWidth);
					settings.FitHeight = (sr.FitHeight);
					settings.HorizontalPrintResolution = (sr.HorizontalPrintResolution);
					settings.VerticalPrintResolution = (sr.VerticalPrintResolution);
					settings.Copies = (sr.Copies);
					
					if (workspaceOptions != null)
					{
						settings.FitToPages = (workspaceOptions.FitToPages);
					}
				}
				else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.WSBOOL)
				{
					workspaceOptions = new WorkspaceInformationRecord(r);
				}
				else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.DEFCOLWIDTH)
				{
					DefaultColumnWidthRecord dcwr = new DefaultColumnWidthRecord(r);
					settings.DefaultColumnWidth = (dcwr.Width);
				}
				else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.DEFAULTROWHEIGHT)
				{
					DefaultRowHeightRecord drhr = new DefaultRowHeightRecord(r);
					if (drhr.Height != 0)
					{
						settings.DefaultRowHeight= (drhr.Height);
					}
				}
				else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.LEFTMARGIN)
				{
					MarginRecord m = new LeftMarginRecord(r);
					settings.LeftMargin=(m.Margin);
				}
				else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.RIGHTMARGIN)
				{
					MarginRecord m = new RightMarginRecord(r);
					settings.RightMargin = (m.Margin);
				}
				else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.TOPMARGIN)
				{
					MarginRecord m = new TopMarginRecord(r);
					settings.TopMargin=(m.Margin);
				}
				else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.BOTTOMMARGIN)
				{
					MarginRecord m = new BottomMarginRecord(r);
					settings.BottomMargin=(m.Margin);
				}
				else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.HORIZONTALPAGEBREAKS)
				{
					HorizontalPageBreaksRecord dr = null;
					
					if (workbookBof.isBiff8())
					{
						dr = new HorizontalPageBreaksRecord(r);
					}
					else
					{
						dr = new HorizontalPageBreaksRecord(r, HorizontalPageBreaksRecord.biff7);
					}
					rowBreaks = dr.RowBreaks;
				}
				else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.PLS)
				{
					plsRecord = new PLSRecord(r);
				}
				else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.OBJ)
				{
					objRecord = new ObjRecord(r);
					
					if (objRecord.Type == ObjRecord.PICTURE && !workbookSettings.DrawingsDisabled)
					{
						if (msoRecord == null)
						{
							logger.warn("object record is not associated with a drawing " + " record - ignoring");
						}
						else
						{
							Drawing drawing = new Drawing(msoRecord, objRecord, workbook.DrawingGroup);
							drawings.Add(drawing);
						}
						msoRecord = null;
						objRecord = null;
					}
				}
				else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.MSODRAWING)
				{
					msoRecord = new MsoDrawingRecord(r);
				}
				else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.BOF)
				{
					BOFRecord br = new BOFRecord(r);
					Assert.verify(!br.isWorksheet());
					
					int startpos = excelFile.Pos - r.Length - 4;
					
					// Skip to the end of the nested bof
					// Thanks to Rohit for spotting this
					Record r2 = excelFile.next();
					while (r2.Code != Microsoft.Fawvw.Components.NExcel.Biff.Type.EOF.Value)
					{
						r2 = excelFile.next();
					}
					
					if (br.isChart())
					{
						Chart chart = new Chart(msoRecord, objRecord, startpos, excelFile.Pos, excelFile, workbookSettings);
						charts.Add(chart);
						
						if (workbook.DrawingGroup != null)
						{
							workbook.DrawingGroup.add(chart);
						}
						
						// Reset the drawing records
						msoRecord = null;
						objRecord = null;
					}
					
					// If this worksheet is just a chart, then the EOF reached
					// represents the end of the sheet as well as the end of the chart
					if (sheetBof.isChart())
					{
						cont = false;
					}
				}
				else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.EOF)
				{
					cont = false;
				}
			}
			
			// Restore the file to its accurate position
			excelFile.restorePos();
			
			// Add all the shared formulas to the sheet as individual formulas
			foreach(SharedFormulaRecord sfr in sharedFormulas)
			{
				Cell[] sfnr = sfr.getFormulas(formattingRecords, nineteenFour);
			
				for (int sf = 0; sf < sfnr.Length; sf++)
				{
					addCell(sfnr[sf]);
				}
			}
			
			// If the last base shared formula wasn't added to the sheet, then
			// revert it to an ordinary formula and add it
			if (!sharedFormulaAdded && sharedFormula != null)
			{
				addCell(revertSharedFormula(sharedFormula));
			}
		}
		
		/// <summary> Sees if the shared formula belongs to any of the shared formula
		/// groups
		/// 
		/// </summary>
		/// <param name="fr">the candidate shared formula
		/// </param>
		/// <returns> TRUE if the formula was added, FALSE otherwise
		/// </returns>
		private bool addToSharedFormulas(BaseSharedFormulaRecord fr)
		{
			bool added = false;
			foreach(SharedFormulaRecord sfr in sharedFormulas)
			{
				if (added) break;
				added = sfr.add(fr);
			}
		
			return added;
		}
		
		/// <summary> Reverts the shared formula passed in to an ordinary formula and adds
		/// it to the list
		/// 
		/// </summary>
		/// <param name="f">the formula
		/// </param>
		/// <returns> the new formula
		/// </returns>
		private Cell revertSharedFormula(BaseSharedFormulaRecord f)
		{
			// String formulas look for a STRING record soon after the formula
			// occurred.  Temporarily the position in the excel file back
			// to the point immediately after the formula record
			int pos = excelFile.Pos;
			excelFile.Pos = f.FilePos;
			
			FormulaRecord fr = new FormulaRecord(f.getRecord(), excelFile, formattingRecords, workbook, workbook, FormulaRecord.ignoreSharedFormula, sheet, workbookSettings);
			
			Cell cell = fr.Formula;
			
			// See if the formula evaluates to date
			if (fr.Formula.Type == CellType.NUMBER_FORMULA)
			{
				NumberFormulaRecord nfr = (NumberFormulaRecord) fr.Formula;
				if (formattingRecords.isDate(fr.XFIndex))
				{
					cell = new DateFormulaRecord(nfr, formattingRecords, workbook, workbook, nineteenFour, sheet);
				}
			}
			
			excelFile.Pos = pos;
			return cell;
		}
		static SheetReader()
		{
			logger = Logger.getLogger(typeof(SheetReader));
		}
	}
}

⌨️ 快捷键说明

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