Centos使用NPOI导出Excel报错Font ‘?‘ cannot be found.

一开始以为centos没有中文字体报错,在尝试了添加字体以后,仍然报这个错。

问题出在下面这段代码自适应列宽上:

for (int j = 0; j <= 14; j++)
{
    sheet.AutoSizeColumn(j);//自适应列宽
}

网上搜索到说这个自适应列宽只适用于英文和数字,不适用中文 。

更改使用网上大佬如下代码自适应列宽后解决报错问题:

for (int columnNum = 0; columnNum <= 14; columnNum++)
{
    int columnWidth = sheet.GetColumnWidth(columnNum) / 256;
    for (int rowNum = 1; rowNum <= sheet.LastRowNum; rowNum++)
    {
        IRow currentRow;
        //当前行未被使用过
        if (sheet.GetRow(rowNum) == null)
        {
            currentRow = sheet.CreateRow(rowNum);
        }
        else
        {
            currentRow = sheet.GetRow(rowNum);
        }

        if (currentRow.GetCell(columnNum) != null)
        {
            ICell currentCell = currentRow.GetCell(columnNum);
            int length = Encoding.Default.GetBytes(currentCell.ToString()).Length;
            if (columnWidth < length)
            {
                columnWidth = length + 3; //+3是为了美观
            }
        }
    }
    sheet.SetColumnWidth(columnNum, columnWidth * 256);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值