我发现了一个开源项目(源代码),它具有基本的Excel功能,包括写入单元格、写入整数到单元格、更改颜色、字体等等。但是缺少一件事情,那就是添加公式,我无法实现它,如果有人知道如何做,那就太好了。
BIFF格式:
internal sealed class BIFF
{
public const ushort DefaultColor = 0x7fff;
public const ushort BOFRecord = 0x0209;
public const ushort EOFRecord = 0x0A;
public const ushort FontRecord = 0x0231;
public const ushort FormatRecord = 0x001E;
public const ushort LabelRecord = 0x0204;
public const ushort WindowProtectRecord = 0x0019;
public const ushort XFRecord = 0x0243;
public const ushort HeaderRecord = 0x0014;
public const ushort FooterRecord = 0x0015;
public const ushort ExtendedRecord = 0x0243;
public const ushort StyleRecord = 0x0293;
public const ushort CodepageRecord = 0x0042;
public const ushort NumberRecord = 0x0203;
public const ushort ColumnInfoRecord = 0x007D;
}
编写整数和字符串的示例方法
private void WriteStringCell(BinaryWriter writer, CellInfo cell)
{
string value;
if (cell.Value is string)
value = (string)cell.Value;
else
value = cell.Value.ToString();
if (value.Length > 255)
value = value.Substring(0, 255);
ushort[] clData = { BIFF.LabelRecord, 0, 0, 0, 0, 0 };
byte[] plainText = Encoding.GetEncoding(CodePage).GetBytes(value);
int iLen = plainText.Length;
clData[1] = (ushort)(8 + iLen);
clData[2] = (ushort)cell.Row;
clData[3] = (ushort)cell.Column;
clData[4] = (ushort)cell.FXIndex;
clData[5] = (ushort)iLen;
WriteUshortArray(writer, clData);
writer.Write(plainText);
}
private void WriteNumberCell(BinaryWriter writer, CellInfo cell)
{
double dValue = Convert.ToDouble(cell.Value);
ushort[] clData = { BIFF.NumberRecord, 14, (ushort)cell.Row, (ushort)cell.Column, (ushort)cell.FXIndex };
WriteUshortArray(writer, clData);
writer.Write(dValue);
}
你可以从提供的链接下载源代码。所以我想要一个方法 WriteFromulaCell,它将在Excel中写入公式。但是完全没有成功。
提前致谢。