1. 程式人生 > >NPOI _導出exl(簡單應用)

NPOI _導出exl(簡單應用)

mode pro format top eof short col ntb poi

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(簡單應用)