Asp.Net导出Excel表单(四)

命运对每个人都是一样的,不一样的是各自的努力和付出不同,付出的越多,努力的越多,得到的回报也越多,在你累的时候请看一下身边比你成功却还比你更努力的人,这样,你就会更有动力。

导读:本篇文章讲解 Asp.Net导出Excel表单(四),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

前言

  既然之前用到了导出,干脆凑齐它们,把Excel的导出也实现了吧,下面是我最终的实现效果图,其中主要用到了图片插入、单元格合并,单元格样式等操作
  这里写图片描述

内容

  好像Excel的导出没有太多重要的”知识点”,所以直接把自己的实现代码贴出来。

                List<Model_LIST> Model = _modelList.GetModelListByNUM(Class_ID);

                HSSFWorkbook book = new HSSFWorkbook();
                IRow _row;
                ICell cell;

                #region [-Style-设置单元格样式]
                CellStyleConfiguration style_Default = new CellStyleConfiguration(book)
               .SetFontName("宋体")
               .SetFontHeightInPoints(12)
               .SetFontBoldweight(FontBoldWeight.NORMAL);

                CellStyleConfiguration style_Border = new CellStyleConfiguration(book)
                .SetFontName("宋体")
                .SetFontHeightInPoints(11)
                .SetFontBoldweight(FontBoldWeight.BOLD)
                .SetFontColor(HSSFColor.BLACK.index)
                .SetBorderBottom(NPOI.SS.UserModel.BorderStyle.THIN)
                .SetBottomBorderColor(HSSFColor.BLACK.index)
                .SetBorderLeft(NPOI.SS.UserModel.BorderStyle.THIN)
                .SetLeftBorderColor(HSSFColor.BLACK.index)
                .SetBorderRight(NPOI.SS.UserModel.BorderStyle.THIN)
                .SetRightBorderColor(HSSFColor.BLACK.index)
                .SetBorderTop(NPOI.SS.UserModel.BorderStyle.THIN)
                .SetTopBorderColor(HSSFColor.BLACK.index)
                .SetWrapText(true)
                .SetAlignment(NPOI.SS.UserModel.HorizontalAlignment.RIGHT)
                .SetVerticalAlignment(VerticalAlignment.CENTER);

        #endregion


                #region [-课程列表页面-]
                //页面名称
                ISheet sheet1 = book.CreateSheet("课程列表");
                sheet1.DisplayGridlines = false;

                #region [-列宽设置-]
                //教师
                sheet1.SetColumnWidth(0, 8 * 256);//编号
                sheet1.SetColumnWidth(1, 20 * 256);//教师名
                //课程
                sheet1.SetColumnWidth(2, 20 * 256);//文科类        
                sheet1.SetColumnWidth(3, 20 * 256);//理科类

                sheet1.SetColumnWidth(4, 20 * 256);//课时安排
                sheet1.SetColumnWidth(5, 20 * 256);//听课人数   


                #endregion


                #region 第1行
                _rowCount = 0;
                _row = sheet1.CreateRow(_rowCount);
                _row.Height = 2 * 166;
                cell = _row.CreateCell(0);
                cell.CellStyle = style.cellStyle;
                sheet1.AddMergedRegion(new CellRangeAddress(0, 2, 0, 1));

                #region Logo图片

                HSSFClientAnchor anchor;
                HSSFPatriarch patriarch = (HSSFPatriarch)sheet1.CreateDrawingPatriarch();
                anchor = new HSSFClientAnchor(0, 0, 0, 0, 0, 0, 2, 3);
                anchor.AnchorType = 2;
                HSSFPicture picture = (HSSFPicture)patriarch.CreatePicture(anchor, LoadImage(
                    CommonUtil.GetAppValue("GetFilePhysicalPath") + "/Image/Logo.png",
                    book));
                picture.LineStyle = LineStyle.None;
                #endregion

                #region 大标题
                cell = _row.CreateCell(2);
                cell.SetCellValue("课程列表详情");
                cell.CellStyle = style_Title.cellStyle;
                cell = _row.CreateCell(3);
                cell.CellStyle = style_Title.cellStyle;
                sheet1.AddMergedRegion(new CellRangeAddress(0, 2, 2, 3));
                #endregion

                #region 编号
                cell = _row.CreateCell(4);
                cell.SetCellValue("No.");
                cell.CellStyle = style.cellStyle;
                cell = _row.CreateCell(5);
                cell.SetCellValue(Model.NO);
                cell.CellStyle = style.cellStyle;
                #endregion
                #endregion

                #region 第2行

                _rowCount++;
                _row = sheet1.CreateRow(_rowCount);
                _row.Height = 2 * 160;
                cell = _row.CreateCell(0);
                cell.CellStyle = style.cellStyle;
                cell = _row.CreateCell(1);
                cell.CellStyle = style.cellStyle;
                for (int i = 2; i <= 3; i++)
                {
                    cell = _row.CreateCell(i);
                    cell.CellStyle = style_Title.cellStyle;
                }

                #region 更新日期
                cell = _row.CreateCell(4);
                cell.SetCellValue("DOC NO.");
                cell.CellStyle = style.cellStyle;
                cell = _row.CreateCell(5);
                cell.SetCellValue(Model.Date);
                cell.CellStyle = style.cellStyle;
                #endregion
                #endregion

                #region 第3行

                _rowCount++;
                _row = sheet1.CreateRow(_rowCount);
                _row.Height = 2 * 160;
                cell = _row.CreateCell(0);
                cell.CellStyle = style.cellStyle;
                cell = _row.CreateCell(1);
                cell.CellStyle = style.cellStyle;
                for (int i = 2; i <= 3; i++)
                {
                    cell = _row.CreateCell(i);
                    cell.CellStyle = style_Title.cellStyle;
                }

                #region 制作用户
                cell = _row.CreateCell(4);
                cell.SetCellValue("User.");
                cell.CellStyle = style.cellStyle;
                cell = _row.CreateCell(5);
                cell.SetCellValue(Model.User);
                cell.CellStyle = style.cellStyle;
                #endregion
                #endregion

                #region 第4行
                _rowCount++;
                _row = sheet1.CreateRow(_rowCount);

                cell = _row.CreateCell(0);
                cell.SetCellValue("教师");
                cell.CellStyle = style_TableTitle.cellStyle;

                cell = _row.CreateCell(1);
                cell.CellStyle = style_TableTitle.cellStyle;

                cell = _row.CreateCell(2);
                cell.CellStyle = style_TableTitle.cellStyle;
                cell.SetCellValue("课程名称");

                cell = _row.CreateCell(3);
                cell.CellStyle = style_TableTitle.cellStyle;


                cell = _row.CreateCell(4);
                cell.SetCellValue("课时安排");
                cell.CellStyle = style_TableTitle.cellStyle;

                cell = _row.CreateCell(5);
                cell.SetCellValue("听课人数");
                cell.CellStyle = style_TableTitle.cellStyle;


                //合并单元格
                sheet1.AddMergedRegion(new CellRangeAddress(3, 3, 0, 1));//编号+教师名
                sheet1.AddMergedRegion(new CellRangeAddress(3, 3, 2, 3));//文科类+理科类
                sheet1.AddMergedRegion(new CellRangeAddress(3, 4, 4, 4));//课时安排
                sheet1.AddMergedRegion(new CellRangeAddress(3, 4, 5, 5));//听课人数

                #endregion

                #region 第5行

                _rowCount++;
                _row = sheet1.CreateRow(_rowCount);
                cell = _row.CreateCell(0);
                cell.SetCellValue("编号");
                cell.CellStyle = style_TableTitle.cellStyle;
                cell = _row.CreateCell(1);
                cell.SetCellValue("教师名");
                cell.CellStyle = style_TableTitle.cellStyle;
                cell = _row.CreateCell(2);
                cell.SetCellValue("文科类");
                cell.CellStyle = style_TableTitle.cellStyle;
                cell = _row.CreateCell(3);
                cell.SetCellValue("理科类");
                cell.CellStyle = style_TableTitle.cellStyle;
                cell = _row.CreateCell(4);//课时安排
                cell.CellStyle = style_TableTitle.cellStyle;
                cell = _row.CreateCell(5);//听课人数
                cell.CellStyle = style_TableTitle.cellStyle;
        #endregion

                #region  [-表单列表内容-]
                List<Model_CLASS> CLASS = new BLL_CLASS().GetList(CLASS);
                List<Model_CLASS_PARAM> CLASS_PARAM;

                string StrProductionParameterForCapacitySpec = "课程信息";
                string StrCapacitySpecValue = String.Empty;

                int paramCount;
                int lastRow;
                _rowCount++;
                DAL.DAL_CLASS_PARAM dalDocClassParam = new DAL_CLASS_PARAM();
                for (int i = 0; i < CLASS.Count; i++)
                {
                    CLASS_PARAM = dalDocClassParam.GetParam(CLASS[i].Class_ID);
                    paramCount = CLASS_PARAM.Count;
                    #region 课程详情
                    for (int k = 0; k < paramCount; k++)
                    {
                        if (CLASS_PARAM[k].IN_MAKE_PIC == 1)
                        {
                            lastRow = _rowCount;
                            _row = sheet1.CreateRow(_rowCount);
                            cell = _row.CreateCell(0);
                            cell.SetCellValue(CLASS[i].CODE);
                            cell.CellStyle = style.cellStyle;
                            cell = _row.CreateCell(1);
                            cell.SetCellValue(CLASS[i].T_NAME);
                            cell.CellStyle = style.cellStyle;

                            string name = CLASS_PARAM[k].PARAM_NAME;
                            if (CLASS_PARAM[k].PARAM_TYPE_ID.ToString() == "2")
                            {
                                cell = _row.CreateCell(2);
                                cell.SetCellValue(name);
                                cell.CellStyle = style.cellStyle;
                                cell = _row.CreateCell(3);
                                cell.CellStyle = style.cellStyle;
                            }
                            else
                            {
                                cell = _row.CreateCell(2);
                                cell.CellStyle = style.cellStyle;
                                cell = _row.CreateCell(3);
                                cell.SetCellValue(name);
                                cell.CellStyle = style.cellStyle;
                            }
                            cell = _row.CreateCell(4);
                            cell.SetCellValue(CLASS_PARAM[k].Class_Hour);  
                            cell.CellStyle = style.cellStyle;
                            cell = _row.CreateCell(5);
                            cell.SetCellValue(CLASS_PARAM[k].Tote); 
                            cell.CellStyle = style.cellStyle;
                            #endregion
                            _rowCount += 1;
                        }
                    }
                }
                sheet1.AddMergedRegion(new CellRangeAddress(_rowCount, _rowCount, 0, 5));

                #region 输出
                return ExportAndTransform(book, string.Format("{0}-{1}", Model.Model_LIST_NUM, Model.Model_LIST_VERSION_NUM));
                #endregion
            }
        }
#endregion

小结

  在导出功能的各种实现中对于这些插件的使用以及其中的特性都有了一定的了解。点滴积累,不断学习,让自己在知识的海洋里狗刨~

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/144265.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!