sanjin_login 2015-06-17 07:14 采纳率: 0%
浏览 1047
已结题

关于npoi导出的问题,只能导出最后一个数据

  public void Export(HttpContext context)
        {
            context.Response.ContentType = "application/vnd.ms-excel;charset=UTF-8";
            //文件名进行url编码,防止乱码  
            string strFileName = HttpUtility.UrlDecode("班级平均分排名.xls");
            context.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", strFileName));
            context.Response.Clear();
            //创建一个excel对象
            HSSFWorkbook hwb = new HSSFWorkbook();
            //文档摘要信息
            DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
            //公司  
            dsi.Company = "四川大宇信息系统有限公司";
            //文档摘要信息  
            SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
            //文档主题  
            si.Subject = "班级平均分排名";
            //标题  
            si.Title = "班级平均分排名";
            //添加到execl对象里
            hwb.DocumentSummaryInformation = dsi;
            hwb.SummaryInformation = si;
            //拿到需要绑定的subject表头
            List<string> n = context.Session["SubjectName"] as List<string>;

            #region 需要绑定的数据
            int ProId = Convert.ToInt32(context.Request["ProId"]);
            int GradesId = Convert.ToInt32(context.Request["GradesId"]);
            Proc_list_ClassAverageRank plc = new Proc_list_ClassAverageRank();
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("Id", ProId);
            dic.Add("GradesId", GradesId);
            IList<Hashtable> list = action.SelectAllRank(dic);
            IList<Hashtable> abc = new List<Hashtable>();

            foreach (Hashtable item_1 in list)
            {
                //Hashtable c用来储存要绑定的数据
                Hashtable c = new Hashtable();
                foreach (string item in n)
                {
                    if (item_1.ContainsKey(item))
                    {
                        //用于储存键值
                        string avgScroce = "";
                        string rank = "";
                        //如果数据不为空,进行字符串分割
                        if (item_1[item] != null)
                        {
                            string b = item_1[item].ToString();
                            string[] value = b.Split('_');
                            avgScroce = value[0].ToString();
                            avgScroce = Convert.ToDecimal(avgScroce).ToString("0.00");
                            rank = value[1];
                        }
                        //数据为空,赋值为0
                        else
                        {
                            avgScroce = "0";
                            rank = "0";
                        }
                        //remove 掉重复的班级名称,否则会报错
                        c.Remove("ClassName");
                        //添加需要的键值
                        c.Add("ClassName", item_1["ClassName"]);
                        c.Add(item + "pz", avgScroce);
                        c.Add(item + "pm", rank);

                    }
                }

                abc.Add(c);
            }
            #endregion
            ISheet sheet = hwb.CreateSheet("sheet1");
            //创建标题
            IRow row_title = sheet.CreateRow(0);
            IRow row_title_2 = sheet.CreateRow(1);
            IRow row_title_3 = sheet.CreateRow(2);
            ICell cell_title = row_title.CreateCell(0);
            //标题-1
            cell_title.SetCellValue(context.Request["Title"].ToString());
            ICellStyle style = hwb.CreateCellStyle();
            IFont font = hwb.CreateFont();
            //标题样式(标题-1)
            style.Alignment = HorizontalAlignment.Center;
            cell_title.CellStyle = style;


            //班级样式(标题-2)
            ICellStyle cellClassSubject = hwb.CreateCellStyle();
            cellClassSubject.Alignment = HorizontalAlignment.Center;


            ICell cell_Class = row_title_2.CreateCell(0);
            cell_Class.SetCellValue("班级");
            cell_Class.CellStyle = cellClassSubject;

            sheet.AddMergedRegion(new CellRangeAddress(1, 2, 0, 0));
            sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, n.Count * 2));
            //获选获取科目,生成标题
            for (int i = 0; i < n.Count; i++)
            {
                ICell cell_Subject = row_title_2.CreateCell((i + 1) * 2 - 1);
                cell_Subject.CellStyle = cellClassSubject;
                cell_Subject.SetCellValue(n[i].ToString());
                sheet.AddMergedRegion(new CellRangeAddress(1, 1, (i + 1) * 2 - 1, (i + 1) * 2));

                ICell cell_pz = row_title_3.CreateCell((i + 1) * 2 - 1);
                cell_pz.SetCellValue("平均分");
                cell_pz.CellStyle = cellClassSubject;
                ICell cell_pm = row_title_3.CreateCell((i + 1) * 2);
                cell_pm.SetCellValue("排名");
                cell_pm.CellStyle = cellClassSubject;
            }
            string[] n_l = new string[n.Count * 2];
            for (int i = 0; i < n.Count; i++)
            {
                n_l[i] = n[i] + "pz";
                n_l[i + 1] = n[i] + "pm";
            }
            //循环填充数据
            for (int i = 0; i < n.Count; i++)
            {
                string pz = n[i] + "pz";
                string pm = n[i] + "pm";
                for (int j = 0; j < abc.Count; j++)
                {
                    IRow row_content = sheet.CreateRow(j + 3);
                    foreach (DictionaryEntry item in abc[j])
                    {
                        ICell cell_Class_content;
                        ICell cell_content_pz;
                        ICell cell_content_pm;
                        if (item.Key.Equals("ClassName"))
                        {
                            cell_Class_content = row_content.CreateCell(0);
                            cell_Class_content.SetCellValue(item.Value.ToString());
                        }
                        else if (pz.Equals(item.Key.ToString()))
                        {
                            cell_content_pz = row_content.CreateCell((i + 1) * 2 - 1);
                            cell_content_pz.SetCellValue(item.Value.ToString());
                        }
                        else if (pm.Equals(item.Key.ToString()))
                        {
                            cell_content_pm = row_content.CreateCell((i + 1) * 2);
                            cell_content_pm.SetCellValue(item.Value.ToString());
                        }
                    }
                }
            }
            hwb.Write(context.Response.OutputStream);
        }

代码很长,动态添加行列还有合并,查询数据什么的估计是最后一点儿出问题了,在给列赋值的时候,只显示最后一条数据,前面的都没有图片说明

本来都有数据的、

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 cgictest.cgi文件无法访问
    • ¥20 删除和修改功能无法调用
    • ¥15 kafka topic 所有分副本数修改
    • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
    • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
    • ¥40 串口调试助手打开串口后,keil5的代码就停止了
    • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
    • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档
    • ¥50 C++五子棋AI程序编写
    • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。