repter导出到Excel

 public void DataReadAndWiteToExcel()
        {
            try
            {
                BaseBLL bll = new BaseBLL();
                #region 导出数据格式
               // var list = bll.GetModelList<MSP_GroupUserVideo>("IsDelete=0", "MSP_GroupUserVideo");
                var list = Bindrepter();
                int rownum = 1;
                //创建表头,填充表头
                HSSFWorkbook workbook = new HSSFWorkbook();
                ISheet sheet1 = workbook.CreateSheet("sheet1");

                sheet1.SetColumnWidth(015 * 250);
                sheet1.SetColumnWidth(115 * 250);
                sheet1.SetColumnWidth(215 * 200);
                sheet1.SetColumnWidth(315 * 300);
                sheet1.SetColumnWidth(415 * 300); 

                IRow row = sheet1.CreateRow(0);
                row.CreateCell(0).SetCellValue("单位");
                row.CreateCell(1).SetCellValue("登录ip");
                row.CreateCell(2).SetCellValue("观看时间");
                row.CreateCell(3).SetCellValue("视频");
                row.CreateCell(4).SetCellValue("状态"); 
                foreach (MSP_GroupUserVideo item in list)
                {
                    string name = GetOther(item.GroupUserId, 1);//单位
                    string ip = GetOther(item.GroupUserId, 2);//IP
                    string StartTime = Convert.ToDateTime(item.StartTime).ToString("yyyy-MM-dd:HH:ss:ss");//时间
                    string videoName = GetVideoName(item.VideoId); //视频名称
                    string lineState=GetOther(item.GroupUserId, 3);//在线状态
                    IRow row1 = sheet1.CreateRow(rownum);
                    row1.CreateCell(0).SetCellValue(name);
                    row1.CreateCell(1).SetCellValue(ip);
                    row1.CreateCell(2).SetCellValue(StartTime);
                    row1.CreateCell(3).SetCellValue(videoName);
                    row1.CreateCell(4).SetCellValue(lineState); 
                    rownum++;
                }
                #endregion
                using (MemoryStream ms = new MemoryStream())
                {
                    workbook.Write(ms);
                    Response.ContentType = "application/vnd.ms-excel";
                    //DateTime.Now.ToString("yyyyMMddHHmmssfff")
                    Response.AddHeader("Content-Disposition"string.Format("attachment; filename={0}.xls","学习记录"));
                    Response.Clear();
                    Response.BinaryWrite(ms.ToArray());
                    workbook = null;
                }
            }
            catch
            {
                //JavaScriptManager.show("请确保导出的表格没有被打开!若打开了,请关闭后再导出!");
            }
        }
posted @ 2014-10-22 15:32  You最温暖的港湾  阅读(362)  评论(0)    收藏  举报