【发布时间】:2012-01-06 14:39:07
【问题描述】:
我找到了一个打开的source project,它具有基本的 excel 功能、写入单元格、将 int 写入单元格、更改颜色、字体和其他内容。缺少一件事,它的添加公式,我无法实现它,所以如果有人知道该怎么做,那就太好了。
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;
}
写int和string的示例方法
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。完全没有成功。
提前致谢。
【问题讨论】:
-
为什么不使用 Excel 的自动化模型?
-
由于 BIFF,我现在使用互操作,它的速度很慢,如果超过 3k 条记录就会中断,等等,我找到了这个库,它在不到 3 秒的时间内生成了 10k 条记录。跨度>
-
好的。所以是性能(这种背景对Qs很重要)。通过使用 Excel 自动化从一次单元格变为整个范围,您已经看到了一个数量级的性能变化,您确定如果没有这种根本性的变化,您就无法提高性能吗?
标签: c# excel xls excel-formula