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

  • 相关阅读:
    OpenCV 图像二值化 threshold
    C++ pow()函数
    Marr-Hildreth 边缘检测 OpenCV C++实现
    OpenCV 边缘检测 Canny
    OpenCV Canny()函数
    OpenCV Sobel()函数
    OpenCV 边缘检测 Sobel
    OpenCV 边缘检测 Laplacian
    OpenCV 边缘检测 Scharr
    OpenCV 形态学变换 morphologyEx函数
  • 原文地址:https://www.cnblogs.com/qinge/p/5500919.html
Copyright © 2020-2023  润新知