码迷,mamicode.com
首页 > 其他好文 > 详细

NPOI _导出exl(简单应用)

时间:2017-11-17 16:20:13      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:对齐   cells   getc   members   ==   rto   cal   taf   nta   

1. 导出exl表格,创建表格导出到客户端

  public static MemoryStream Export_Table<T>(List<T> datalist)
        {
            MemoryStream ms = new MemoryStream();
            var members = typeof(T).GetProperties();
            var workbook = new NPOI.HSSF.UserModel.HSSFWorkbook();
            NPOI.SS.UserModel.ISheet sheet = workbook.CreateSheet();
            NPOI.SS.UserModel.IRow headerRow = sheet.CreateRow(0);
            int order = 1;
            foreach (var meber in members)//初始化标题
            {
                string titlevalue = "";
                var name = meber.GetCustomAttributes(typeof(TableAttribulate), false);
                if (name.Length == 0)
                    continue;
                var pro = name[0] as TableAttribulate;
                if (pro == null)
                    continue;
                titlevalue = pro.CName;
                var cell = headerRow.CreateCell(order);
                cell.SetCellValue(titlevalue);
                if(pro.Weight==0)
                    sheet.AutoSizeColumn(order,true);
                else
                {
                    sheet.SetColumnWidth(order,pro.Weight);
                }
                cell.CellStyle = GetStyle(workbook);
                cell.CellStyle.Alignment = pro.HorizontalAlignment;
                var cellfont = workbook.CreateFont();
                cellfont.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Bold;
                cell.CellStyle.SetFont(cellfont);
                order++;
            }

            int rowIndex = 1;
            foreach (var row in datalist)
            {
                NPOI.SS.UserModel.IRow dataRow = sheet.CreateRow(rowIndex);
                int colIndex = 0;
                var cellindex = dataRow.CreateCell(colIndex);
                cellindex.SetCellValue(rowIndex);
                cellindex.CellStyle = GetStyle(workbook);
                var cellfont = workbook.CreateFont();
                cellfont.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Normal;
                cellfont.Color = NPOI.HSSF.Util.HSSFColor.LightBlue.Index;
                cellindex.CellStyle.SetFont(cellfont);
                colIndex++;
                foreach (var meber in members)
                {
                    var name = meber.GetCustomAttributes(typeof(TableAttribulate), false);
                    if (name.Length == 0)
                        continue;
                    var pro = name[0] as TableAttribulate;
                    if (pro == null)
                        continue;
                    if (pro.Weight == 0)
                        sheet.AutoSizeColumn(colIndex, true);
                    var cell = dataRow.CreateCell(colIndex);
                    var mebervalue = meber.GetValue(row);
                    cell.SetCellValue(mebervalue == null ? "" : mebervalue.ToString());
                    cell.CellStyle = GetStyle(workbook);
                    cell.CellStyle.Alignment = pro.HorizontalAlignment;
                    colIndex++;
                }
                rowIndex++;
            }
            workbook.Write(ms);
            ms.Seek(0, SeekOrigin.Begin);
            return ms;//
        }

2.代码总的特性

public class TableAttribulate:Attribute
        {
            public TableAttribulate(string name, int weight = 0, NPOI.SS.UserModel.HorizontalAlignment hoalign = NPOI.SS.UserModel.HorizontalAlignment.Center)
            {
                CName = name;
                Weight = weight;
                HorizontalAlignment = hoalign;
            }
            public string CName { get; set; }

            public int Weight { get; set; }

            public NPOI.SS.UserModel.HorizontalAlignment HorizontalAlignment { get; set; }//对齐方式

        }

 3.函数

        public static NPOI.SS.UserModel.ICellStyle GetStyle(NPOI.HSSF.UserModel.HSSFWorkbook workbook)
        {
            var cs = workbook.CreateCellStyle();
            cs.DataFormat = NPOI.HSSF.UserModel.HSSFDataFormat.GetBuiltinFormat("@");
            cs.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
            cs.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
            cs.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
            cs.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
            cs.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
            cs.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
            
            var cellfont = workbook.CreateFont();
            cellfont.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Normal;

            cs.SetFont(cellfont);
            return cs;
        }

 

NPOI _导出exl(简单应用)

标签:对齐   cells   getc   members   ==   rto   cal   taf   nta   

原文地址:http://www.cnblogs.com/leolzi/p/7851735.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!