zoukankan      html  css  js  c++  java
  • NPOI 自定义单元格背景颜色 XSSFWorkbook

    x

    网上找到了,HSSFWorkbook自定义颜色的例子(讲的还挺细致的),但是XSSFWorkbook确没找到...研究了一下,坑掉了一地...

    NPOI.XSSF.UserModel.XSSFWorkbook xssfworkbook = new NPOI.XSSF.UserModel.XSSFWorkbook();

    方案壹:>>XSSFCellStyle

    XSSFCellStyle rowsStyleColor = (XSSFCellStyle)xssfworkbook.CreateCellStyle();
    XSSFColor xssfColor = new XSSFColor();
    //根据自己需要设置RGB
    byte[] colorRgb = { (byte)252, (byte)139, (byte)139 }; xssfColor.SetRgb(colorRgb); rowsStyleColor.FillForegroundColorColor = xssfColor; rowsStyleColor.FillPattern = FillPattern.SolidForeground;

    方案贰:>>ICellStyle

    ICellStyle rowsStyleColor = (XSSFCellStyle)xssfworkbook.CreateCellStyle();
    rowsStyleColor.Alignment = HorizontalAlignment.Center;
    rowsStyleColor.VerticalAlignment = VerticalAlignment.Center;
    rowsStyleColor.BorderBottom = BorderStyle.Thin;
    rowsStyleColor.BorderLeft = BorderStyle.Thin;
    rowsStyleColor.BorderRight = BorderStyle.Thin;
    rowsStyleColor.BorderTop = BorderStyle.Thin;
    rowsStyleColor.WrapText = true;
    //设置背景颜色...
    rowsStyleColor.FillForegroundColor = 0;
    rowsStyleColor.FillPattern = FillPattern.SolidForeground;
    ((XSSFColor)rowsStyleColor.FillForegroundColorColor).SetRgb(new byte[] { 252, 139, 139 });

    x

    感觉挺别扭的...一点搞不好,就是错误...

    x

    其他的报错:


    //****************************文件流...
    public ActionResult ExportExcelFile()
    {
        byte[] streamData = null;

        try
        {
        MemoryStream file = new MemoryStream();
        workbook.Write(file);
        /*这个错误导致我一直以为NPOI不能导出不报错的.xlsx类型的Excel呢!!!···*/     streamData = file.ToArray();//这种写法就可以的...     //streamData =file.GetBuffer();//用这么的写法,导出的Excel,就是报错,类似格式不正确,需要修复才能打开...


            if (streamData == null || streamData.Length == 0) { return Content("无数据供导出。"); }

            Response.Charset = "UTF-8";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", "filename=Test.xlsx");
            Response.AddHeader("Content-Length", streamData.LongLength.ToString());
            Response.BinaryWrite(streamData);
            Response.Flush();
            Response.End();

            return null;
        }
        catch (Exception ex)
        {
            return Content("未知错误>>>");
        }
    }

  • 相关阅读:
    Eclipse安装flex插件
    Free download SKP900 update tool & activation tool
    How to installation V145 Renault CAN Clip diagnostic software
    How to setup ELM327 Bluetooth WiFi for Android software Torque
    How to make remote key fob for 2002 BMW 3 series
    2015 FVDI V6.3 Software Free Download
    How to solve the SVDI SN Number Display Problem
    Tested work with China Digiprog 3 4.94 mileage programmer
    Autel MaxiDAS DS708 Fatal Application Error illegal operation
    Remap BMW F11 2010 all ECUs with E-Sys and ENET cable
  • 原文地址:https://www.cnblogs.com/love-zf/p/7450409.html
Copyright © 2011-2022 走看看