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;
    }

  • 相关阅读:
    遥感影像数据产品级别概述
    数据传输中的成帧
    帧同步
    I、Q信号是如何产生的,I、Q信号复用的作用
    调制解调技术
    基带信号、载波信号和宽带信号
    浅谈MVP架构及开发模式
    剑指offer代码 vs2013执行
    进程的状态
    死锁的必要条件及避免
  • 原文地址:https://www.cnblogs.com/qinge/p/5500919.html
Copyright © 2011-2022 走看看