1 public ActionResult save_dc()
2 {
3 using (EFDbContext db = new EFDbContext())
4 {
5 //var list = (from p in db.Books
6 // join s in db.BTypes on p.Tid equals s.Tid
7 // select new
8 // {
9 // p.Bbh,
10 // p.Bname,
11 // p.Bzz,
12 // p.Bdetm,
13 // p.Bprice,
14 // p.Bcs,
15 // p.Tid,
16 // p.Timg,
17 // p.Tcbs,
18 // s.Tname
19 // }).ToList();
20
21 //获取要导出的表的数据
22 var list = db.Books.ToList();
23 //创建一个工作簿)
24 IWorkbook workbook = new XSSFWorkbook();
25 //创建一个页面根据名字来
26 ISheet sheet = workbook.CreateSheet("Sheet1");
27 //创建一行 设置表中第一行的数据
28 IRow row = sheet.CreateRow(0);
29 //标题(将表中的字段手动写入) 给第一行中的单元格赋值
30 row.CreateCell(0).SetCellValue("编号");
31 row.CreateCell(1).SetCellValue("封面");
32 row.CreateCell(2).SetCellValue("书名");
33 row.CreateCell(3).SetCellValue("作者");
34 row.CreateCell(4).SetCellValue("入馆时间");
35 row.CreateCell(5).SetCellValue("价格");
36 row.CreateCell(6).SetCellValue("借阅次数");
37 row.CreateCell(7).SetCellValue("类别");
38 row.CreateCell(8).SetCellValue("出版社");
39 //循环根据下标将表中的数据添加单元格中,给单元格赋值
40 for (int i = 0; i < list.Count(); i++)
41 {
42 //在该页中,再添加一行 ,表示第几行
43 row = sheet.CreateRow(i + 1);
44 //根据下标创建单元格,和赋值
45 row.CreateCell(0).SetCellValue(list[i].Bbh);
46 row.CreateCell(1).SetCellValue(list[i].Timg);
47 row.CreateCell(2).SetCellValue(list[i].Bname);
48 row.CreateCell(3).SetCellValue(list[i].Bzz);
49 row.CreateCell(4).SetCellValue(list[i].Bdetm);
50 row.CreateCell(5).SetCellValue(list[i].Bprice);
51 row.CreateCell(6).SetCellValue(list[i].Bcs);
52 row.CreateCell(7).SetCellValue(list[i].Tid);
53 row.CreateCell(8).SetCellValue(list[i].Tcbs);
54 }
55 //文件流 实例化一个支持存储区为内存的流
56 using (MemoryStream ms = new MemoryStream())
57 {
58 //写入到工作簿(写入流)
59 workbook.Write(ms);
60 //获取字节数组
61 byte[] huanchong = ms.GetBuffer();
62 // 将文件创建出来,第一个参数表示数据,第二个是格式,第三个是后缀名
63 return File(huanchong, "application/x-xls", "p.xlsx");
64 }
65 }
66
67
68 }
1 public actionresult importuserinfo()
2 {
3 var excelfile = request.files["excelfile"];
4 loghelper.write("开始导入excel");
5 iworkbook workbook = new xssfworkbook(excelfile.inputstream);
6 isheet sheet = workbook.getsheetat(0);
7 //irow row = sheet.getrow(sheet.firstrownum); //如果需要获取标题列 使用此代码
8
9 list<book_model> userlist = new list<book_model>();
10 list<btype_model> jobs = getalljob();
11 //假如excel有标题行,跳过标题行,也就是+1; 如果没有标题行,则不需要+1
12 for (int i = sheet.firstrownum + 1; i <= sheet.lastrownum; i++)
13 {
14 book_model user = new book_model();
15 irow row = sheet.getrow(i);
16 user.name = row.getcell(0).stringcellvalue;
17 user.age = convert.toint32(row.getcell(1).numericcellvalue);
18 user.spell = row.getcell(2).stringcellvalue;
19 string jobname = row.getcell(3).stringcellvalue;
20 int jobid = jobs.where(j => j.jobname == jobname.trim()).select(j => j.id).firstordefault();
21 user.jobid = jobid;
22 userlist.add(user);
23 }
24 loghelper.write("成功读取excel数据:" + jsonconvert.serializeobject(userlist));
25 loghelper.write($"开始向数据库添加数据,{userlist.count}条:");
26
27 using (efdbcontext db = new efdbcontext())
28 {
29 db.user.addrange(userlist);
30 int ret = db.savechanges();
31 //return redirect("/user/show");
32 loghelper.write($"成功向数据库中导入数据,{ret}条:");
33 return content($"<script>alert('成功导入{ret}条数据');location.href='/user/show';</script>");
34 }
35 }