← 返回所有博客文章

Spread for WinForms中提供了获取单元格、行和列实际大小的方法,分别如下:

单元格实际大小:

fpSpread1.ActiveSheet.GetPreferredCellSize(0, 0);

 

行实际高度:

fpSpread1.ActiveSheet.GetPreferredRowHeight(0);

 

列实际宽度:

fpSpread1.ActiveSheet.GetPreferredColumnWidth(0);

 

在通过 GetPreferredRowHeight 获取有合并单元格的行的行高时,可能不是十分准确,这是我们可以通过以下方法来计算这样行的准确高度,代码如下:

    private float GetRowBestHeight(int viRow)
    {
        Graphics vgphGraphics = fpSpread1.CreateGraphics();
        int iLoop = 0;
        float fBestHeight = 0;

        SizeF sfBestSize = default(SizeF);
        float fWidth = 0;

        while ((iLoop < fpSpread1.ActiveSheet.ColumnCount - 1))
        {
            fWidth = 0;
            for (int iTemp = 0; iTemp <= (fpSpread1.ActiveSheet.Cells[viRow, iLoop].ColumnSpan - 1); iTemp++)
            {
                fWidth += fpSpread1.ActiveSheet.Columns[iLoop + iTemp].Width;
            }
            sfBestSize = vgphGraphics.MeasureString(fpSpread1.ActiveSheet.Cells[viRow, iLoop].Text, fpSpread1.Font, Convert.ToInt32(fWidth));
            if (sfBestSize.Height > fBestHeight)
            {
                fBestHeight = sfBestSize.Height;
            }
            iLoop += fpSpread1.ActiveSheet.Cells[viRow, iLoop].ColumnSpan;

        }
        return fBestHeight;
    }

 

运行截图:

 

源码下载:

 7404_High.zip (10.89 kb)