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 DIFY API Endpoint 问题。
    • ¥20 sub地址DHCP问题
    • ¥15 delta降尺度计算的一些细节,有偿
    • ¥15 Arduino红外遥控代码有问题
    • ¥15 数值计算离散正交多项式
    • ¥30 数值计算均差系数编程
    • ¥15 redis-full-check比较 两个集群的数据出错
    • ¥15 Matlab编程问题
    • ¥15 训练的多模态特征融合模型准确度很低怎么办
    • ¥15 kylin启动报错log4j类冲突