2 sanjin login sanjin_login 于 2015.06.17 14:54 提问

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);
        }

大概是这样。求大神看看

1个回答

sanjin_login
sanjin_login   2015.06.17 15:01

做的是个导出工作,表头和数据都是动态生成,,但是只出现最后一条数据估计是最后一段错了。但是不知道怎么改了。

Csdn user default icon
上传中...
上传图片
插入图片