NPOI를 통한 Excel 생성

2125 단어 Excel
        HSSFWorkbook hssfworkbook;

        ISheet sheet1;



        public void BuildExcel()

        {

            hssfworkbook = new HSSFWorkbook();

            //  Excel 

            sheet1 = hssfworkbook.CreateSheet("Sheet1");



            //  

            for (var i = 0; i < 10;i++ )

            {

                IRow row1 = sheet1.CreateRow(i);

                for (var j = 0; j < 10; j++)

                {

                    // 

                    ICell cell = row1.CreateCell(j);







                    //  

                    cell.SetCellValue(" "+j.ToString());

                }

            }

            

            //  

            sheet1.SetColumnWidth(2, 10 * 256);





            //    

            ICellStyle styleCell = hssfworkbook.CreateCellStyle();

            // 

            styleCell.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;

            //  

            styleCell.VerticalAlignment = VerticalAlignment.Top;

            ICellStyle cellStyle = hssfworkbook.CreateCellStyle();



            // 

            IFont fontColorRed = hssfworkbook.CreateFont();

            fontColorRed.Color = HSSFColor.OliveGreen.Red.Index;



            styleCell.SetFont(fontColorRed);



            

            sheet1.GetRow(2).GetCell(2).CellStyle = styleCell;



            //  

            sheet1.AddMergedRegion(new CellRangeAddress(2, 4, 2, 5));





            //  Excel

            string filename = "cnblogs.rhythmk.com. .xls";

            var context = HttpContext.Current;

            context.Response.ContentType = "application/vnd.ms-excel";

            context.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", context.Server.UrlEncode(filename)));

            context.Response.Clear();



           

            MemoryStream file = new MemoryStream();

            hssfworkbook.Write(file);

            context.Response.BinaryWrite(file.GetBuffer());

            context.Response.End();



          



        }


좋은 웹페이지 즐겨찾기