| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 | using NPOI.SS.UserModel;using NPOI.XSSF.UserModel;using System;using System.Collections.Generic;using System.IO;using System.Text;using wms.dto.request.hj.dto;namespace wms.util.Ext{    public class ExeclHelperOv    {        public static byte[] OutputExcel<T>(List<T> entitys, List<ExportModel> row)        {            IWorkbook workbook = new XSSFWorkbook();            var headFont = workbook.CreateFont();            headFont.IsBold = true;            //标题列样式            var headStyle = workbook.CreateCellStyle();            headStyle.Alignment = HorizontalAlignment.Center;            headStyle.BorderBottom = BorderStyle.Thin;            headStyle.BorderLeft = BorderStyle.Thin;            headStyle.BorderRight = BorderStyle.Thin;            headStyle.BorderTop = BorderStyle.Thin;            headStyle.SetFont(headFont);            ISheet sheet = workbook.CreateSheet("sheet");            IRow Title = null;            IRow rows = null;            for (int i = 0; i <= entitys.Count; i++)            {                if (i == 0)                {                    Title = sheet.CreateRow(0);                    for (int k = 1; k < row.Count + 1; k++)                    {                        Title.CreateCell(0).SetCellValue("序号");                        Title.CreateCell(k).SetCellValue(row[k - 1].Title);                    }                    continue;                }                else                {                    rows = sheet.CreateRow(i);                    var entity = entitys[i - 1];                    for (int j = 0; j < row.Count; j++)                    {                        var filenames = row[j].Code.Substring(0, 1).ToUpper() + row[j].Code.Substring(1);                        var propInfo = entity.GetType().GetProperty(filenames);                        if (propInfo == null)                            continue;                        var kkk = propInfo.GetValue(entity);                        if (kkk != null)                        {                            rows.CreateCell(0).SetCellValue(i);                            switch (propInfo.PropertyType.Name)                            {                                case "String":                                    rows.CreateCell(j + 1).SetCellValue(kkk.ToString());                                    break;                                case "Decimal":                                case "Double":                                    rows.CreateCell(j + 1).SetCellValue(Convert.ToDouble(kkk.ToString()));                                    break;                                case "Int32":                                    rows.CreateCell(j + 1).SetCellValue(Convert.ToInt32(kkk.ToString()));                                    break;                                case "DateTime":                                    rows.CreateCell(j + 1).SetCellValue(kkk.ToString());                                    break;                            }                        }                    }                }            }            byte[] buffer = new byte[1024 * 2];            using (MemoryStream ms = new MemoryStream())            {                workbook.Write(ms, true);                buffer = ms.ToArray();                ms.Close();            }            return buffer;        }    } }
 |