asp.net带格式导出excel

asp.net带格式导出excel

开发工具:Visual Studio 2015、SQL Server 2014 Management Studio
关键技术:C# MVC
作者:刘东标
撰写时间:2019年3月27日
效果图片:
asp.net带格式导出excel
//创建Excel文件的对象
NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
//添加一个sheet
NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet(“Sheet1”);
//获取list数据
//List<TB_STUDENTINFOModel> listRainInfo = m_BLL.GetSchoolListAATQ(schoolname);

        HSSFCellStyle style = (HSSFCellStyle)book.CreateCellStyle();

        //垂直居中
        style.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
        //水平居中
        style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
        //设置\n自动换行
        style.WrapText = true;
        //给单元格设置样式
        HSSFFont hf = (HSSFFont)book.CreateFont();
        //hf.Boldweight = 900;//设置字体加粗
        //hf.FontHeight = 260;//字体的高度
        hf.FontName = "仿宋_GB2312";
        //hf.Color = HSSFColor.Blue.Index;//设置字体颜色
        style.SetFont(hf);

        NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(0);
        for (int j = 0; j < 19; j++)
        {
            sheet1.SetDefaultColumnStyle(j, style);
        }
                   sheet1.SetColumnWidth(0, 20 * 200);
        sheet1.SetColumnWidth(1, 20 * 256);
        sheet1.SetColumnWidth(2, 20 * 230);
        sheet1.SetColumnWidth(3, 20 * 80);
        sheet1.SetColumnWidth(4, 20 * 120);
        sheet1.SetColumnWidth(5, 20 * 256);
        sheet1.SetColumnWidth(6, 20 * 256);
        sheet1.SetColumnWidth(7, 20 * 100);
        sheet1.SetColumnWidth(8, 20 * 100);
        sheet1.SetColumnWidth(9, 20 * 100);
        sheet1.SetColumnWidth(10, 20 * 100);
        sheet1.SetColumnWidth(11, 20 * 100);
        sheet1.SetColumnWidth(12, 20 * 100);
        sheet1.SetColumnWidth(13, 20 * 100);
        sheet1.SetColumnWidth(14, 20 * 100);
        sheet1.SetColumnWidth(15, 20 * 100);
        sheet1.SetColumnWidth(16, 20 * 100);
        sheet1.SetColumnWidth(17, 20 * 150);
        sheet1.SetColumnWidth(18, 20 * 200);