using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using NPOI.HSSF.UserModel; using System.IO; using StackExchange.Redis; //using System.Threading; //using System.Data; using CSharpUtil; //using Newtonsoft.Json; //using Newtonsoft.Json.Linq; //using System.Diagnostics; namespace DataTransfer.tongji { class loseUserDataTransfer { public static void excelText111() { //导出:将数据库中的数据,存储到一个excel中 //1、查询数据库数据 User user1 = new User() { Id = 1, Name = "小明", pwd = "123123", type = "学生" }; User user2 = new User() { Id = 2, Name = "小红", pwd = "123123", type = "学生" }; User user3 = new User() { Id = 3, Name = "小绿", pwd = "123123", type = "学生" }; User user4 = new User() { Id = 4, Name = "小白", pwd = "123123", type = "老师" }; User user5 = new User() { Id = 5, Name = "小黑", pwd = "123123", type = "老师" }; User user6 = new User() { Id = 6, Name = "小蓝", pwd = "123123", type = "老师" }; List list = new List(); list.Add(user1); list.Add(user2); list.Add(user3); list.Add(user4); list.Add(user5); list.Add(user6); //2、 生成excel //2_1、生成workbook //2_2、生成sheet //2_3、遍历集合,生成行 //2_4、根据对象生成单元格 HSSFWorkbook workbook = new HSSFWorkbook(); //创建工作表 var sheet = workbook.CreateSheet("信息表"); //创建标题行(重点) 从0行开始写入 var row = sheet.CreateRow(0); //创建单元格 var cellid = row.CreateCell(0); cellid.SetCellValue("编号"); var cellname = row.CreateCell(1); cellname.SetCellValue("用户名"); var cellpwd = row.CreateCell(2); cellpwd.SetCellValue("密码"); var celltype = row.CreateCell(3); celltype.SetCellValue("类型"); //遍历集合,生成行 int index = 1; //从1行开始写入 for (int i = 0; i < list.Count; i++) { int x = index + i; var rowi = sheet.CreateRow(x); var id = rowi.CreateCell(0); id.SetCellValue(list[i].Id); var name = rowi.CreateCell(1); name.SetCellValue(list[i].Name); var pwd = rowi.CreateCell(2); pwd.SetCellValue(list[i].pwd); var type = rowi.CreateCell(3); type.SetCellValue(list[i].type); } FileStream file = new FileStream(@"F:\excel\信息表.xls", FileMode.CreateNew, FileAccess.Write); workbook.Write(file); file.Dispose(); } public static void excelText(string memKey,int retain,int zoneid,float day) { //获取redis种的数据 var mem = Redis.Ins.GetDatabase(0); HashEntry[] ctxList = mem.HashGetAll(memKey); int length = ctxList.Length; if (length <= 0) { return; } HSSFWorkbook workbook = new HSSFWorkbook(); //创建工作表 var sheet = workbook.CreateSheet("信息表"); var row = sheet.CreateRow(0); //创建单元格 var cellid = row.CreateCell(0); cellid.SetCellValue("指标类型"); var cellname = row.CreateCell(1); cellname.SetCellValue("指标id"); var cellpwd = row.CreateCell(2); cellpwd.SetCellValue("指标累计值"); int x = 1; foreach (var item in ctxList) { string[] sList = item.ToString().Split(':'); string type = sList[0]; int num = int.Parse(sList[1]); string[] ctx = type.Split('-'); string name = ctx[0]; int id = int.Parse(ctx[1]); var rowi = sheet.CreateRow(x); var cname = rowi.CreateCell(0); cname.SetCellValue(name); var cid = rowi.CreateCell(1); cid.SetCellValue(id); var cnum = rowi.CreateCell(2); cnum.SetCellValue(num); x += 1; } string txtName = "流失玩家指标累计信息表_" +"zone"+zoneid+"_"+retain+"retaine_" + day; FileStream file = new FileStream(@"F:\excel\"+txtName+".xls", FileMode.CreateNew, FileAccess.Write); workbook.Write(file); file.Dispose(); } } public class User { /// /// 索引 /// public int Id { get; set; } public string Name { get; set; } public string pwd { get; set; } public string type { get; set; } } public class Model { /// /// 索引 /// public int Id { get; set; } public string Name { get; set; } public int pwd { get; set; } public string type { get; set; } } }