NPOI Excel 셋업 셀 높이 자동 적응 내보내기

5756 단어 .Net
 public class ExcelAHelper
    {
        /// 
        /// List Excel
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static byte[] Export(IList list, IList dicts, string SheetName = "Sheet1")
        {
            IWorkbook book = new XSSFWorkbook();

            // 
            ICellStyle styleCell = book.CreateCellStyle();
            styleCell.Alignment = HorizontalAlignment.Center;
            styleCell.WrapText = true;
            styleCell.BorderTop = BorderStyle.Thin;
            styleCell.BorderRight = BorderStyle.Thin;
            styleCell.BorderLeft = BorderStyle.Thin;
            styleCell.BorderBottom = BorderStyle.Thin;
            styleCell.Alignment = HorizontalAlignment.Center;
            IFont font = book.CreateFont();
            font.FontHeightInPoints = 10;
            font.FontName = " ";
            styleCell.SetFont(font);
            ISheet sheet = book.CreateSheet(SheetName);
            IRow row = sheet.CreateRow(0);
            row.RowStyle = styleCell;
            for (int i = 0; i < dicts.Count; i++)
            {
                row.CreateCell(i).SetCellValue(dicts[i].ColumnName);
                sheet.SetColumnWidth(i, dicts[i].Width * 256);
                sheet.SetDefaultColumnStyle(i, styleCell);
            }
            for (int i = 0; i < list.Count; i++)
            {
                var entity = list[i];
                IRow mRow = sheet.CreateRow(i + 1);
                for (int j = 0; j < dicts.Count; j++)
                {
                    var property = typeof(T).GetProperty(dicts[j].PropertyName);
                    if (property != null)
                    {
                        mRow.CreateCell(j).SetCellValue(property.GetValue(entity) == null ? "" : property.GetValue(entity).ToString());
                        mRow.RowStyle = styleCell;
                    }

                }
            }

            //      
            MemoryStream ms = new MemoryStream();
            book.Write(ms);
            ms.Flush();



            return ms.ToArray();
        }

        public static byte[] ExportByGroup(IEnumerable> datas, IList columns)
        {
            IWorkbook book = new XSSFWorkbook();

            // 
            ICellStyle styleCell = book.CreateCellStyle(); 
            styleCell.Alignment = HorizontalAlignment.Center;
            styleCell.WrapText = true;
            styleCell.BorderTop = BorderStyle.Thin;
            styleCell.BorderRight = BorderStyle.Thin;
            styleCell.BorderLeft = BorderStyle.Thin;
            styleCell.BorderBottom = BorderStyle.Thin;
            styleCell.VerticalAlignment = VerticalAlignment.Distributed; 
            styleCell.Alignment = HorizontalAlignment.Center;
            IFont font = book.CreateFont();
            font.FontHeightInPoints = 10;
            font.FontName = " "; 
            styleCell.SetFont(font);


            foreach (var item in datas)
            {
                var sheet = book.CreateSheet(item.Key); 
                IRow row = sheet.CreateRow(0);
                row.RowStyle = styleCell;
                for (int i = 0; i < columns.Count; i++)
                {
                    row.CreateCell(i).SetCellValue(columns[i].ColumnName);
                    sheet.SetColumnWidth(i, columns[i].Width * 256); 
                    sheet.VerticallyCenter = true; 
                    sheet.SetDefaultColumnStyle(i, styleCell);
                }

                for (int i = 0; i < item.Count(); i++)
                {
                    var entity = item.ToList()[i];
                    IRow mRow = sheet.CreateRow(i + 1);
                    mRow.RowStyle=styleCell;
                    for (int j = 0; j < columns.Count; j++)
                    {
                        var property = typeof(T).GetProperty(columns[j].PropertyName);
                        if (property != null)
                        {
                            ICell cell = mRow.CreateCell(j);
                            
                            cell.SetCellValue(property.GetValue(entity) == null ? "" : HttpUtility.UrlDecode(property.GetValue(entity).ToString()).Replace("&", "&").Replace("<", "").Replace(""", "\"").Replace("'", "'"));
                            cell.CellStyle = styleCell;
                        } 
                         
                    }

                }

            }
            MemoryStream ms = new MemoryStream();
            book.Write(ms);
            ms.Flush();  
            return ms.ToArray();
        }

        public static void Bytes2File(byte[] buff, string savepath)
        {
            if (File.Exists(savepath))
            {
                File.Delete(savepath);
            }

            FileStream fs = new FileStream(savepath, FileMode.CreateNew);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(buff, 0, buff.Length);
            bw.Close();
            fs.Close();
        }

    }


    public class ExcelEntity
    {
        /// 
        ///  
        /// 
        public string ColumnName { get; set; }
        /// 
        ///  
        /// 
        public string PropertyName { get; set; }
        /// 
        /// 
        /// 
        public int Width { get; set; }
    }

좋은 웹페이지 즐겨찾기