NPOI导出Excel - 自动适应中文宽度

21 篇文章 0 订阅

NPOI导出Excel - 自动适应中文宽度

    /// <summary>
    /// 创建工作簿
    /// </summary>
    /// <param name="fileName">下载文件名</param>
    /// <param name="dt">数据源</param>
    public static void CreateSheet(string fileName, DataTable dt)
    {
      HSSFWorkbook workbook = new HSSFWorkbook();
      MemoryStream ms = new MemoryStream();

      //创建一个名称为Payment的工作表
      ISheet paymentSheet = workbook.CreateSheet("Payment");

      //数据源
      DataTable tbPayment = dt;

      //头部标题
      IRow paymentHeaderRow = paymentSheet.CreateRow(0);

      //循环添加标题
      foreach (DataColumn column in tbPayment.Columns)
        paymentHeaderRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);

      // 内容
      int paymentRowIndex = 1;

      foreach (DataRow row in tbPayment.Rows)
      {
        IRow newRow = paymentSheet.CreateRow(paymentRowIndex);

        //循环添加列的对应内容
        foreach (DataColumn column in tbPayment.Columns)
        {
          newRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
        }

        paymentRowIndex++;
      }

      //列宽自适应,只对英文和数字有效
      for (int i = 0; i <= dt.Rows.Count; i++)
      {
        paymentSheet.AutoSizeColumn(i);
      }
      //获取当前列的宽度,然后对比本列的长度,取最大值
      for (int columnNum = 0; columnNum <= dt.Rows.Count; columnNum++)
      {
        int columnWidth = paymentSheet.GetColumnWidth(columnNum) / 256;
        for (int rowNum = 1; rowNum <= paymentSheet.LastRowNum; rowNum++)
        {
          IRow currentRow;
          //当前行未被使用过
          if (paymentSheet.GetRow(rowNum) == null)
          {
            currentRow = paymentSheet.CreateRow(rowNum);
          }
          else
          {
            currentRow = paymentSheet.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;
            }
          }
        }
        paymentSheet.SetColumnWidth(columnNum, columnWidth * 256);
      }

      //将表内容写入流 通知浏览器下载
      workbook.Write(ms);
      System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xls", fileName));
      System.Web.HttpContext.Current.Response.BinaryWrite(ms.ToArray()); //进行二进制流下在

      workbook = null;
      ms.Close();
      ms.Dispose();
    }

    /// <summary>
    /// 虚拟 DataTable内容
    /// </summary>
    /// <returns></returns>
    public static DataTable CreatTable()
    {
      //创建DataTable 将数据库中没有的数据放到这个DT中
      DataTable datatable = new DataTable();
      datatable.Columns.Add("列1", typeof(string));
      datatable.Columns.Add("列2", typeof(string));
      datatable.Columns.Add("列3", typeof(string));
      //创建DatatTable 结束---------------------------

      //开始给临时datatable赋值
      for (int i = 0; i < 10; i++)
      {
        DataRow row = datatable.NewRow();
        row["列1"] = "列111111111111111111111111111111";
        row["列2"] = "列222222222222222222222222222222222222222";
        row["列3"] = "列3333333322222222222211111111111111111111111113";
        datatable.Rows.Add(row);
      }
      return datatable;
    }



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值