冯 海

一个程序新人菜鸟的日记,希望大家多多关照。QQ:32316131

mvc5+ef+ adminlte教程13:LinqToExcel 表的导入和导出(以上章的echars图表中的表为例 )

1.还是新建一个方法和视图

目的是读出这个表的所有内容

//public async Task<ActionResult> Index(string SeachColumnString)
		public async Task<ActionResult> IndexList()
		{
			return View(await db.Echarones.ToListAsync());
		}

 

视图

@model IEnumerable<jsdhh2.Models.Echarone>

@{
	ViewBag.Title = "Index"; Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
<!-- Content Header (Page header) -->
<section class="content-header">
	<h1>
		图表数据表1数据
		<small>这是图表1的数据</small>
	</h1>
	<ol class="breadcrumb">
		<li><a href="#"><i class="fa fa-dashboard"></i> Level</a></li>
		<li class="active">Here</li>
	</ol>
</section>
<!--end Content Header (Page header) -->
<section class="content">
	<div class="panel panel-default">
		<div class="panel-heading">@Html.ActionLink("新建", "Create", "", new { @class = "btn btn-success" })</div>
		<div class="panel-body">
			<table class="table">
				<tr>
					<th>
					栏目ID
					</th>
					<th>
					主体名
					</th>
					<th>
						任务 
					</th>
					<th>
					完成 
					</th>
					<th>
					排序
					</th>
					 
					<th></th>
				</tr>

				@foreach (var item in Model)
				{
					<tr>
						<td>
							@Html.DisplayFor(modelItem => item.EcharColmnId)
						</td>
						<td>
							@Html.DisplayFor(modelItem => item.ObjNmae)
						</td>
						<td>
							@Html.DisplayFor(modelItem => item.TaskDondeOne)
						</td>
						<td>
							@Html.DisplayFor(modelItem => item.TaskDondeTwo)
						</td>
						<td>
							@Html.DisplayFor(modelItem => item.Sort)
						</td>
						<td>
							@Html.ActionLink("修改", "Edit", new { id = item.Id }) |
					 
							@Html.ActionLink("删除", "Delete", new { id = item.Id })
						</td>
					</tr>
				}

			</table>






		</div>
	</div>




</section>

  

3.效果

 

4.我发现我以前设计得过于复杂了,所以我得重新first code这个表。

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace jsdhh2.Models
{
	public class Echarone
	{
		[Display(Name = "图标ID")]
		public int Id { get; set; }

		[Display(Name = "栏目Id")]
		public string EcharColmnId { get; set; }

		public int Sort { get; set; }

		[Display(Name = "对象名称")]
		public string ObjNmae { get; set; }
 

		[Display(Name = "任务")]

		public double TaskDondeOne { get; set; }

		[Display(Name = "完成")]
		public double TaskDondeTwo {get; set; }
 
   


	}
}

  在上面的顶部增加一个按钮入导。

 @Html.ActionLink("导入", "Import", "", new { @class = "btn btn-info" })

  

 

7.先建一个

Import方法并增加视图和,无实现xlsx文件的上传。
	//导入的操作
		public ActionResult Import()
		{
			return View( );
		}
		//上传方法
		public ActionResult Browse()
		{
			//int saveCount = 0; //定义变量

			//if (ModelState.IsValid && !string.IsNullOrEmpty(id))
			//{

		    	var files = Request.Files;
				if (files.Count > 0)
				{
					var file = files[0];
					string strFileSavePath = Request.MapPath("~/Content/Excle");  //定义上传目标地址
					string strFileExtention = Path.GetExtension(file.FileName);
					if (!Directory.Exists(strFileSavePath))
					{
						Directory.CreateDirectory(strFileSavePath);
					}
					 
						var dt = DateTime.Now;
						string str = dt.ToString("yyyyMMddHHmmss");
						file.SaveAs(strFileSavePath + "/" + str + strFileExtention);   //保存文件



				return RedirectToAction("Import", "Echarones");

			}





			return RedirectToAction("Import", "Echarones");
		}

  

视图

@model IEnumerable<jsdhh2.Models.Echarone>

@{
	ViewBag.Title = "Index"; Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
<!-- Content Header (Page header) -->
<section class="content-header">
	<h1>
	导入数据库
		<small>导入电子表格,请注意模板</small>
	</h1>
	<ol class="breadcrumb">
		<li><a href="#"><i class="fa fa-dashboard"></i> Level</a></li>
		<li class="active">Here</li>
	</ol>
</section>
<!--end Content Header (Page header) -->
<section class="content">
	<div class="panel panel-default">
		<div class="panel-heading">
			@Html.ActionLink("新建", "Create", "", new { @class = "btn btn-success" })









	</div>
	<div class="panel-body">
		<!-- button -->
	 
        @using (Html.BeginForm("Browse", "Echarones", FormMethod.Post, new { enctype = "multipart/form-data", id = "form_Upload" }))
		{
          @Html.AntiForgeryToken() //防止跨站请求伪造(CSRF:Cross-site request forgery)攻击
								<input id="input-41" name="input41[]" type="file" multiple class="file-loading">
								<hr>
								<button type="submit" class="btn btn-primary">上传</button>
								<button type="reset" class="btn btn-default">清除</button>



		}

</div>




</section>
@section scripts{
<link href="~/Content/bootstrap-fileinput/css/fileinput.css" rel="stylesheet" />
<script src="~/Content/bootstrap-fileinput/js/fileinput.js"></script>
	<script>
		$(document).on('ready', function () {
			$("#input-41").fileinput({
				maxFileCount: 10,
				maxFileSize: 1000,
				allowedPreviewTypes: ['other'],
				allowedFileExtensions: ['xlsx','xls']
			});
		});
</script>

}

  

点击上传,测试能正常传到我的那个Content/Excle 目录下。

 

9.在项目中安装LinqToExcle包,现在搜来是1.1版本。

 

posted @ 2017-06-16 19:44  秋天来了哟  阅读(319)  评论(0)    收藏  举报
认识就是缘份,愿天下人都快乐!
QQ: 32316131
Email: 32316131@qq.com