zoukankan      html  css  js  c++  java
  • npoi的用法,动态的判断单元格的大小,设置列的宽度

    public MemoryStream GridToExcelByNPOI(DataTable dt, string strExcelFileName)
    {
    HSSFWorkbook wk = new HSSFWorkbook();
    ISheet tb = wk.CreateSheet("mySheet");
    for (int i = 0; i < dt.Rows.Count; i++)
    {
    if (i == 0)
    {
    IRow row = tb.CreateRow(i);
    for (int j = 0; j < dt.Columns.Count; j++)
    {
    ICell cell = row.CreateCell(j);
    HSSFCellStyle style = (HSSFCellStyle)wk.CreateCellStyle();
    HSSFFont font=(HSSFFont)wk.CreateFont();
    font.FontHeightInPoints=12;
    style.SetFont(font);
    cell.CellStyle = style;
    cell.SetCellValue(dt.Rows[i][j].ToString());
    }
    }
    else
    {
    IRow row = tb.CreateRow(i);
    for (int j = 0; j < dt.Columns.Count; j++)
    {
    ICell cell = row.CreateCell(j);
    cell.SetCellValue(dt.Rows[i][j].ToString());
    }
    }
    }
    for (int k = 0; k < dt.Columns.Count; k++)
    {
    string str = "";
    for (int f = 0; f < dt.Rows.Count-1;f++ )
    {
    string cellstr = dt.Rows[f][k].ToString();
    if (cellstr.Length < dt.Rows[f+1][k].ToString().Length)
    {
    cellstr = dt.Rows[f + 1][k].ToString();
    }
    str = cellstr;
    }
    if (str.Length <= 4)
    {
    tb.SetColumnWidth(k, 15 * 256);
    }
    else
    {
    tb.SetColumnWidth(k, 28 * 256);
    }
    }
    System.IO.MemoryStream ms = new System.IO.MemoryStream();
    wk.Write(ms);
    return ms;
    }

  • 相关阅读:
    购物菜单
    数据库
    增删改查
    页面交互
    计算器
    2020.9.21
    团队-团队编程项目中国象棋-项目总结
    团队-团队编程项目作业名称-最终程序
    课后作业-阅读任务-阅读提问-4
    《20171130-构建之法:现代软件工程-阅读笔记》
  • 原文地址:https://www.cnblogs.com/qinge/p/5500919.html
Copyright © 2011-2022 走看看