0
点赞
收藏
分享

微信扫一扫

C# NPOI自适应宽度不支持中文解决方案

王老师说 2023-08-07 阅读 41


NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
            ////创建一个工作薄 sheet
            ISheet sheet1 = book.CreateSheet("Sheet1");
            //  创建数据行 0代表是在第几行创建 变量 给sheet1添加第一行的头部标题
            IRow row1 = sheet1.CreateRow(0);
            //设置第一行的高度   【Height的单位是1/20个点】,所以Height的值永远是HeightInPoints【HeightInPoints的单位是点】的20倍
            row1.Height = 25 * 20;
            var font = book.CreateFont();//创建字体
            font.Boldweight = (short)FontBoldWeight.Bold;
            //font.FontHeightInPoints = 18;//字体大
            font.Color = (short)HSSFColor.White.Index;
            ICellStyle cellStyle = book.CreateCellStyle();//创建样式
            cellStyle.SetFont(font);
            cellStyle.Alignment = HorizontalAlignment.Center;
            cellStyle.VerticalAlignment = VerticalAlignment.Center;
            cellStyle.WrapText = true;//自动换行
            cellStyle.FillPattern = FillPattern.SolidForeground;//填充的模式,可以是网格、花式等。如果需要填充单色,请使用:SOLID_FOREGROUND
            cellStyle.FillForegroundColor = HSSFColor.CornflowerBlue.Index; //背景色
            cellStyle.BorderLeft = BorderStyle.Thin;
            cellStyle.LeftBorderColor = (short)HSSFColor.White.Index;
            cellStyle.BorderRight = BorderStyle.Thin;
            cellStyle.RightBorderColor = (short)HSSFColor.White.Index;
            cellStyle.BorderTop = BorderStyle.Thin;
            cellStyle.TopBorderColor = (short)HSSFColor.White.Index;
            cellStyle.BorderBottom = BorderStyle.Thin;
            cellStyle.BottomBorderColor = (short)HSSFColor.White.Index;

            ////创建单元格 并且赋值 
            row1.CreateCell(0).SetCellValue("订单号");
            row1.CreateCell(1).SetCellValue("分类");
            row1.CreateCell(2).SetCellValue("用户编号");
            row1.CreateCell(3).SetCellValue("账户");
            row1.CreateCell(4).SetCellValue("收入/支出");
            row1.CreateCell(5).SetCellValue("交易前金额");
            row1.CreateCell(6).SetCellValue("交易余额");
            row1.CreateCell(7).SetCellValue("交易后金额");
            row1.CreateCell(8).SetCellValue("交易类型");
            row1.CreateCell(9).SetCellValue("交易时间");
            row1.CreateCell(10).SetCellValue("交易说明");
            for (int j = 0; j < 11; j++)
            {
                //headerRow.Cells[i].CellStyle;
                row1.GetCell(j).CellStyle = cellStyle;
            }

            //将数据逐步写入sheet1各个行
            int i = 0;
            foreach (var item in collection.FundDetailList)
            {

                i++;
                NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i);
                rowtemp.CreateCell(0).SetCellValue(item.OrderId);
                rowtemp.CreateCell(1).SetCellValue(item.Category);
                rowtemp.CreateCell(2).SetCellValue(item.UserId);
                rowtemp.CreateCell(3).SetCellValue(item.AccountType.ToString().Equals("Bonus", StringComparison.OrdinalIgnoreCase) ? "奖金账户" : item.AccountType.ToString().Equals("FillMoney", StringComparison.OrdinalIgnoreCase) ? "充值账户" : item.AccountType.ToString().Equals("Freeze", StringComparison.OrdinalIgnoreCase) ? "冻结账户" : string.Empty);
                rowtemp.CreateCell(4).SetCellValue(item.PayType.ToString().Equals("Payin", StringComparison.OrdinalIgnoreCase) ? "收入" : item.PayType.ToString().Equals("Payout", StringComparison.OrdinalIgnoreCase) ? "支出" : string.Empty);
                rowtemp.CreateCell(5).SetCellValue(item.BeforeBalance.ToString("N2"));
                rowtemp.CreateCell(6).SetCellValue(item.PayMoney.ToString("N2"));
                rowtemp.CreateCell(7).SetCellValue(item.AfterBalance.ToString("N2"));
                rowtemp.CreateCell(8).SetCellValue(item.AiOrderId);
                rowtemp.CreateCell(9).SetCellValue(item.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"));
                rowtemp.CreateCell(10).SetCellValue(item.Summary);
            }
           
for (int j = 0; j < 11; j++)
{
	//自动调整列的宽度
	sheet1.AutoSizeColumn(j);
	int columnWidth = sheet1.GetColumnWidth(j) / 256;
	for (int rowNum = 1; rowNum <= sheet1.LastRowNum; rowNum++)
	{
	IRow currentRow = sheet1.GetRow(rowNum);
	if (currentRow.GetCell(j) != null)
                {
                    ICell currentCell = currentRow.GetCell(j);
                    int length = Encoding.UTF8.GetBytes(currentCell.ToString()).Length;
                    if (columnWidth < length + 3)
                    {
                        columnWidth = length + 3;
                    }
                }
            }
            sheet1.SetColumnWidth(i, columnWidth * 256);
	}
}
    // 写入到客户端 
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            book.Write(ms);
            ms.Seek(0, SeekOrigin.Begin);
            return File(ms, "application/vnd.ms-excel", string.Format("{0}.xls", DateTime.Now.ToString("yyyy-MM-dd")));

实例:

//列宽自适应,只对英文和数字有效  maxColunm为最大列数

for (int i = 0; i <= maxColumn; i++)

{

   sheet.AutoSizeColumn(i);

}

,然后对比本列的长度,取最大值

for (int columnNum = 0; columnNum <= maxColumn; columnNum++)

{

  //获取当前列的宽度

   int columnWidth = sheet.GetColumnWidth(columnNum) / 256;

   for (int rowNum = 1; rowNum <= sheet.LastRowNum; rowNum++)

   {

       IRow currentRow = sheet.GetRow(rowNum);                

       if(currentRow.GetCell(columnNum) != null)

       {

           ICell currentCell = currentRow.GetCell(columnNum);

           int length = Encoding.UTF8.GetBytes(currentCell.ToString()).Length;

           if (columnWidth < length)

           {

               columnWidth = length;

           }

       }

   }

   sheet.SetColumnWidth(columnNum, columnWidth * 256);

}

举报

相关推荐

0 条评论