为什么我的Excel Interop代码中的字体大小设置超过[ridden,written]?

时间:2016-02-09 16:49:01

标签: c# excel fonts excel-interop named-ranges

我在代码中设置范围的字体大小:

var rowRngRptTitle = _xlSheet.Range[_xlSheet.Cells[2, 1], _xlSheet.Cells[2, 13]];
rowRngRptTitle.Merge(Type.Missing);
rowRngRptTitle.Font.Size = 24;
rowRngRptTitle.Font.Bold = true;
rowRngRptTitle.Value2 = String.Format("Delivery Performance - {0} - {1}", _delPerfBeginDate.ToShortDateString(), _delPerfEndDate.ToShortDateString());

...但是价值没有生效,或者"坚持" - 字体很小(看起来像10,与非配置范围内的相同)。

但是,如果我稍后设置它,在编写整个工作表之后,通过创建一个新范围:

var rowRngRptTitle2 = _xlSheet.Range[_xlSheet.Cells[2, 1], _xlSheet.Cells[2, 13]];
rowRngRptTitle2.Merge(Type.Missing);
rowRngRptTitle2.Font.Size = 24;
rowRngRptTitle2.Font.Bold = true;

......它有效 - 字体大小确实是24而不是10。

什么可以取消早期设置的大小?没有其他" Font.Size ="在引用该范围的整个代码中。在更多的上下文中,这里是代码(不必要的effluvia省略):

private void PopulateSheet(string shortName)
{
    if (_xlSheet != null)
    {
        var rowRngUnitName = _xlSheet.Range[_xlSheet.Cells[1, 1], _xlSheet.Cells[1, 13]];
        rowRngUnitName.Merge(Type.Missing);
        rowRngUnitName.Font.Bold = true;
        rowRngUnitName.Font.Size = 16;
        rowRngUnitName.Value2 = shortName;

        var rowRngRptTitle = _xlSheet.Range[_xlSheet.Cells[2, 1], _xlSheet.Cells[2, 13]];
        rowRngRptTitle.Merge(Type.Missing);
        rowRngRptTitle.Font.Size = 24;
        rowRngRptTitle.Font.Bold = true;
        rowRngRptTitle.Value2 = String.Format("Delivery Performance - {0} - {1}", _delPerfBeginDate.ToShortDateString(), _delPerfEndDate.ToShortDateString());

        . . .

        _xlSheet.Columns.AutoFit();
    }
    FreezePane(FIRST_DATA_ROW, SUN_ORDERS_COLUMN);

    var rowRngRptTitle2 = _xlSheet.Range[_xlSheet.Cells[2, 1], _xlSheet.Cells[2, 13]];
    rowRngRptTitle2.Merge(Type.Missing);
    rowRngRptTitle2.Font.Size = 24;
    rowRngRptTitle2.Font.Bold = true;

    ConfigureForPrinting(_xlSheet.UsedRange.Rows.Count);

    if (null != _xlSheet)
    {
        Marshal.ReleaseComObject(_xlSheet);
    }
}

如果我不得不在任何地方重新指定范围,而不是标准/通用/默认字体大小,那将是一件痛苦的事。有没有办法保留范围的初始字体大小分配?

我以这种方式初始化Excel应用程序:

private void InitializeExcelObjects()
{
    _xlApp = new Excel.Application
    {
        SheetsInNewWorkbook = 1,
        StandardFont = "Calibri",
        StandardFontSize = 10
    };

    _xlBook = _xlApp.Workbooks.Add(Type.Missing);
    _xlApp.Windows.Application.ActiveWindow.DisplayGridlines = false;

    _xlSheets = _xlBook.Worksheets;
    _xlSheet = (Microsoft.Office.Interop.Excel.Worksheet)_xlSheets.Item[1];
}

0 个答案:

没有答案