using System; using System.Collections.Generic; using System.Text; namespace Biff8Excel.Excel { /// /// 用于定义Excel页眉的内容及格式 /// public class ExcelHeader: IDisposable { Biff8Excel.Records.Header m_header; StringBuilder m_headerString; internal byte[] WriteRecord() { if (m_headerString.Length == 0) //if (m_headerString.Length == 0) return null ; // make sure the string starts in one of the three sections // in case we forget put it in the left section switch (m_headerString.ToString().Substring(0, 2)) { case "&L": case "&C": case "&R": break; default : m_headerString.Append("&L"); break; } m_header.Head = Globals.GetUnicodeBytes(m_headerString.ToString()); m_header.TextLen = (ushort)m_headerString.ToString().Length; return m_header.GetByte(); } public void AddText(string sText) { if (sText.Length == 0) return; int pos; //Check fot the & character, needs to be duplicated in the string pos = 0; while(true) { pos = sText.IndexOf('&',pos); if (pos < 0) break; //add an extra & character sText = sText.Substring(0,pos+1) + sText.Substring(pos); pos += 2; } m_headerString.Append(sText); } public void SetFont(string fontName) { SetFont(fontName, EnumFontStyle.Regular); } public void SetFont(string fontName, EnumFontStyle fontStyle) { m_headerString.Append("&" + (char)(0x22)); // Quotes 双引号 m_headerString.Append(fontName + ","); string s =""; switch (fontStyle) { case EnumFontStyle.Regular: s = "Regular"; break; case EnumFontStyle.Bold: s = "Bold"; break; case EnumFontStyle.Italic: s = "Italic"; break; case EnumFontStyle.BoldItalic: s = "Bold Italic"; break; } m_headerString.Append(s + (char)(0x22)); } public void SetFontSize(ushort fontSize) { m_headerString.Append("&" + fontSize.ToString()); } public void StartLeftSection() { m_headerString.Append("&L"); } public void StartCenterSection() { m_headerString.Append("&C"); } public void StartRightSection() { m_headerString.Append("&R"); } public void AddPageNumber() { m_headerString.Append("&P"); } public void AddPageCount() { m_headerString.Append("&N"); } public void AddCurrentDate() { m_headerString.Append("&D"); } public void AddCurrentTime() { m_headerString.Append("&T"); } public void AddSheetName() { m_headerString.Append("&A"); } public void AddFileName() { m_headerString.Append("&F"); } // This is the same for both on and off, but makes code clearer to define then separately public void SetUnderlineOn() { m_headerString.Append("&U"); } public void SetUnderlineOff() { m_headerString.Append("&U"); } public void SetDoubleUnderlineOn() { m_headerString.Append("&E"); } public void SetDoubleUnderlineOff() { m_headerString.Append("&E"); } public void SetStrikeOutOnOff() { m_headerString.Append("&S"); } public void SetSuperScriptOutOnOff() { m_headerString.Append("&X"); } public void SetSubScriptOutOnOff() { m_headerString.Append("&Y"); } public ExcelHeader() { m_header = new Biff8Excel.Records.Header(); m_headerString = new StringBuilder(); } #region IDisposable 成员 public void Dispose() { m_header = null; m_headerString = null; } #endregion } }