Ejemplo n.º 1
0
        private ICellStyle GetStyle(IWorkbook wb, ExportFieldAttribute attr)
        {
            ICellStyle cellStyle = wb.CreateCellStyle();

            if (attr.Align == FieldAlign.Left)
            {
                cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
            }
            else if (attr.Align == FieldAlign.Center)
            {
                cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
            }
            else
            {
                cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Right;
            }


            return(cellStyle);
        }
Ejemplo n.º 2
0
        private string GetDataValue(string value, ExportFieldAttribute attr)
        {
            if (!string.IsNullOrWhiteSpace(attr.Format))
            {
                if (attr.Type == FieldType.DateTime)
                {
                    DateTime tt;
                    if (DateTime.TryParse(value, out tt))
                    {
                        return(tt.ToString(attr.Format));
                    }
                }
                else if (attr.Type == FieldType.Numeric)
                {
                    decimal dd;
                    if (decimal.TryParse(value, out dd))
                    {
                        return(dd.ToString(attr.Format));
                    }
                }
            }

            return(value);
        }
Ejemplo n.º 3
0
        public IWorkbook ExportExcel <T>(ExportModel <T> param)
        {
            int fieldCount = GetPropertyCount <T>();
            int rowIndex   = 0;

            IWorkbook wb = new HSSFWorkbook();
            //设置工作簿的名称
            string sheetName = string.IsNullOrEmpty(param.SheetName) ? "sheet1" : param.SheetName;
            //创建一个工作簿
            ISheet sh = wb.CreateSheet(sheetName);

            #region 第一行,文件标题
            ////合并单元格
            //sh.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, fieldCount - 1));
            //IRow row0 = sh.CreateRow(rowIndex);
            //ICell icell1top0 = row0.CreateCell(0);
            //icell1top0.SetCellValue(param.Title);
            //#endregion

            //rowIndex++;

            //#region 第二行,筛选内容
            //sh.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 1, 0, fieldCount - 1));
            //IRow row1 = sh.CreateRow(rowIndex);
            //ICell icell1top1 = row1.CreateCell(0);
            //icell1top1.SetCellValue(param.FilterContent);
            #endregion

            //rowIndex++;

            List <ExportFieldAttribute> colList = new List <ExportFieldAttribute>();
            #region 表头(第3行)
            IRow row2       = sh.CreateRow(rowIndex);
            var  properties = typeof(T).GetProperties();
            int  colIndex   = 0;
            foreach (PropertyInfo item in properties)
            {
                ExportFieldAttribute attr = ((ExportFieldAttribute)Attribute.GetCustomAttribute(item, typeof(ExportFieldAttribute)));
                if (attr == null)
                {
                    continue;
                }
                ICell tableCell = row2.CreateCell(colIndex);
                tableCell.SetCellValue(attr.Title);

                if (attr.Width > 0)
                {
                    sh.SetColumnWidth(colIndex, attr.Width * 256);
                }

                colIndex++;

                colList.Add(attr);
            }
            #endregion

            rowIndex++;
            #region 数据内容

            foreach (T line in param.Items)
            {
                colIndex = 0;
                IRow bodyRow = sh.CreateRow(rowIndex);
                foreach (PropertyInfo pi in properties)
                {
                    ExportFieldAttribute attr = ((ExportFieldAttribute)Attribute.GetCustomAttribute(pi, typeof(ExportFieldAttribute)));
                    if (attr == null)
                    {
                        continue;
                    }
                    ICell  rowCell = bodyRow.CreateCell(colIndex);
                    object obj     = pi.GetValue(line, null);
                    string value   = obj != null?obj.ToString() : string.Empty;

                    value = GetDataValue(value, attr);
                    rowCell.SetCellValue(value);

                    colIndex++;
                }
                rowIndex++;
            }

            #endregion

            return(wb);
        }