ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

.netCore.NPOI 下载数据到excel 自定义颜色 单元格宽度自适应 工具类

2021-02-18 17:35:00  阅读:214  来源: 互联网

标签:自定义 maxWidth netCore 单元格 System transWorkbook using filePath transStyle


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.Drawing;

namespace SmartReleaseApi.Utils
{
    //下载到Excel的公共方法
    public class DownloadToExcelUtil
    {
        public static string DownloadToExcel(string[] headerNames,Dictionary<string,List<string>> values,string fileName)
        {
            HSSFWorkbook transWorkbook = new HSSFWorkbook();
            //自定义颜色
            HSSFPalette palette = transWorkbook.GetCustomPalette();
            //纬创蓝 先加入到色板中 RGB颜色值,第一个值:8~64之间,后面三个值为RGB色值
            palette.SetColorAtIndex((short)8, (byte)0, (byte)80, (byte)110);      
            Color color = Color.FromArgb(0, 80, 110);
            var myColor = palette.FindColor(color.R, color.G, color.B);

            //表头样式
            ICellStyle transStyle = transWorkbook.CreateCellStyle();
            transStyle.Alignment = HorizontalAlignment.CenterSelection;
            IFont headerfont = transWorkbook.CreateFont();
            headerfont.FontName = "微软雅黑";//字体
            headerfont.FontHeightInPoints = 12;//字体大小
            headerfont.IsBold = true;//是否加粗
            headerfont.Color = 9; //表头白色
            transStyle.SetFont(headerfont);
          
            transStyle.FillForegroundColor = myColor.Indexed;
             transStyle.FillPattern = FillPattern.SolidForeground;
            ISheet transSheet = transWorkbook.CreateSheet("sheet");
            IRow transRowHead = transSheet.CreateRow(0);
            //存储最大列宽
            Dictionary<int, int> maxWidth = new Dictionary<int, int>();
            for (int i = 0; i < headerNames.Length; i++) {
                ICell ccell = transRowHead.CreateCell(i);
                ccell.SetCellValue(headerNames[i]);
                ccell.CellStyle = transStyle;
                //表格宽度自适应
                getMaxWidth(maxWidth, ccell, i);

            }

            //表体样式
            ICellStyle bodyStyle = transWorkbook.CreateCellStyle();
            bodyStyle.Alignment = HorizontalAlignment.CenterSelection;
            IFont bodyFont = transWorkbook.CreateFont();
            bodyFont.FontName = "微软雅黑";//字体
            bodyFont.FontHeightInPoints = 10;
            bodyFont.IsBold = false;//是否加粗
            bodyStyle.SetFont(bodyFont);

            var trnsCount = 1;
            List<string> vkeys = values.Keys.ToList();
            foreach (var item in vkeys)
            {
                IRow trnsRow = transSheet.CreateRow(trnsCount);
                List<string> valueList = values[item];
                for (int k = 0; k < valueList.Count; k++) {
                    ICell createCell = trnsRow.CreateCell(k);
                    createCell.SetCellValue(valueList[k]);
                    //表格宽度自适应
                    getMaxWidth(maxWidth,createCell,k);
                    createCell.CellStyle = bodyStyle;
                }         
                trnsCount++;
            }
            if (null != values && values.Count > 0) {
                for (int i = 0; i < headerNames.Length; i++)
                {
                    transSheet.SetColumnWidth(i, maxWidth[i]);
                }
            }
            string filePath = Environment.CurrentDirectory + @"\wwwroot\UploadTemp\" + UUID.Getuuid() + ".xls";
            if (!string.IsNullOrEmpty(fileName)) {
                filePath = Environment.CurrentDirectory + @"\wwwroot\UploadTemp\" + fileName + ".xls";
            }
           
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
            using (FileStream maurl = File.OpenWrite(filePath))
            {
                transWorkbook.Write(maurl);
                transWorkbook.Close();
            }
            return filePath;
        }

        private static void getMaxWidth(Dictionary<int, int> maxWidth,ICell cell,int key) {
            //表格宽度自适应
            int length = System.Text.Encoding.Default.GetBytes(cell.StringCellValue).Count() * 256 + 200;
            //这里把宽度最大限制到15000
            if (length > 15000)
            {
                length = 15000;
            }
            if (!maxWidth.ContainsKey(key))
            {
                maxWidth.Add(key, Math.Max(length, 0));
            }
            else
            {
                maxWidth[key] = Math.Max(length, maxWidth[key]);
            }
        }
    }
}

 

标签:自定义,maxWidth,netCore,单元格,System,transWorkbook,using,filePath,transStyle
来源: https://www.cnblogs.com/tarrying/p/14412730.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有