using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Text.RegularExpressions;
using System.Collections.Specialized;
namespace Biff8Excel.Excel
{
///
/// 全部cell中内容为label的字符串的处理
///
public class ExcelSst : IDisposable
{
Records.SST m_sst;
const int SSTMaxLen = 8224 - 8; // length 8224 - totalstrings(uint)(4) stringnumber(uint)(4) 之后的用CONTINUE
const int ContinueMaxLen = 8224; // 每8224后重新开始
int m_ContinueTimes; // Continue次数, m_ContinueIndex & m_ContinueStringLength 数组长度
uint m_LabelCells;
uint m_totalStrings;
List m_arrString;
Hashtable m_htStringIndex; //空间换时间用于得到字符串数组的索引
bool m_isContinue;
uint[] m_ContinueIndex;
uint[] m_ContinueStringLength;
uint[] m_ContinueTotalStrings;
//uint m_absolute;
uint m_SSTStringsLength;
internal byte[] WriteRecord()
{
m_htStringIndex.Clear();
m_sst.TotalStringCells = m_LabelCells;
if (m_totalStrings > 0)
{
m_sst.SharedStrings = m_arrString;
//m_sst.TotalStringCells = m_totalStringsLength;
m_sst.TotalStringsLength = m_SSTStringsLength;
if (m_isContinue)
{
m_sst.ContinueIndex = m_ContinueIndex;
m_sst.ContinueStringLength = m_ContinueStringLength;
m_sst.ContinueTotalStrings = m_ContinueTotalStrings;
}
}
//m_sst.absolutoffset = m_absolute;
return m_sst.GetByte();
}
///
/// 查找将加入的string是否存在数组中,存在则返回所在数组的index
/// 否则将string加入数组的最后,返回数组的最后的index
///
/// 要加入的string
/// 加入string所在数组的index
internal uint AddString(string st)
{
object tmp = m_htStringIndex[st];
if (tmp != null)
return Convert.ToUInt32(tmp);
m_htStringIndex.Add(st, m_totalStrings);
m_arrString.Add(st);
m_totalStrings++;
CalcStringLen(st);
return m_totalStrings - 1;
}
///
/// 计算字符串长度如当前所有字符串长度之和大于SST允许,则加入至CONTINUE
///
///
private void CalcStringLen(string cs)
{
uint bytelen = (uint)Globals.GetDefaultBytesLength(cs);
uint charcount = (uint)cs.Length;
uint realLen = 0;
if (bytelen != charcount)
realLen = charcount * 2;
else
realLen = charcount;
//uint realLen = (uint)Globals.GetUnicodeByteLength(cs);
if (m_isContinue)
{
if (((m_ContinueTotalStrings[m_ContinueTimes - 1]+1) * 3 + m_ContinueStringLength[m_ContinueTimes - 1] + realLen) >= ContinueMaxLen)
{
AddContinue(realLen);
return;
}
m_ContinueTotalStrings[m_ContinueTimes - 1]++;
m_ContinueStringLength[m_ContinueTimes - 1] += realLen;
return;
}
else
{
if ((m_totalStrings * 3 + m_SSTStringsLength + realLen) >= SSTMaxLen)
{
m_isContinue = true;
AddContinue(realLen);
return;
}
m_SSTStringsLength += realLen;
return;
}
}
private void AddContinue(uint realLen)
{
m_ContinueTimes++;
Array.Resize(ref m_ContinueIndex, m_ContinueTimes);
Array.Resize(ref m_ContinueStringLength, m_ContinueTimes);
Array.Resize(ref m_ContinueTotalStrings, m_ContinueTimes);
m_ContinueIndex[m_ContinueTimes - 1] = m_totalStrings - 1;
m_ContinueStringLength[m_ContinueTimes - 1] += realLen;
m_ContinueTotalStrings[m_ContinueTimes - 1] = 1;
}
//public uint AbsolutOffset
//{
// set { m_absolute = value; }
//}
public ExcelSst()
{
m_sst = new Biff8Excel.Records.SST();
m_arrString = new List();
m_htStringIndex = new Hashtable();
//m_totalStrings = 0;
//m_SSTStringsLength = 0;
//m_isContinue = false;
//m_ContinueTimes = 0;
}
#region IDisposable 成员
public void Dispose()
{
m_sst = null;
}
#endregion
}
}