用Visual Studio(C#) 实现Excel数据导入(映射)

本文介绍了一种利用Visual Studio和C#进行Excel数据导入的方法,着重讲解了get和post方法、视图页面的设置以及ImportStudentsFile1()和Import()两个关键方法的实现过程。需要注意的是,该方法存在数据量限制,当列数据过多时,可能只能导入约2000条数据,并且要求预先对表格列进行类声明。
摘要由CSDN通过智能技术生成

本方法缺点:数据量有一定的限制,若列数据过多,则只能导入2000条左右。此方法必须对表格中的列进行类声明。

1.get:

 public virtual async Task<ActionResult> ImportStudentMessage(int page = 1)
        {
            return View(await db.StudentInforms.GetPagedDataAsyns(new PageDataParameter<StudentInform, Guid>(page: page)));
        }
``

2.视图页面

@using (Html.BeginForm("ImportStudentMessage", "StudentInforms", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div form-group>
        <div class="row">
            <div class="clearfix">
                <div class="col-xs-4">
                    <div class="pull-left">
                    <input name="file" type="file" class="form-control" accept=".xls,.xlsx" />
                        </div>
                    <div class="pull-right">
                    <input type="submit" value="导入" class="btn btn-success btn-sm" />
                    @Html.ValidationMessage("file", "", new { @class = "text-danger" })
                        </div>
                </div>
            </div>
        </div>
    </div>
}

3.post方法

[HttpPost]
        //[Authorize]
        public virtual ActionResult ImportStudentMessage(string file, bool stopWhenError = false)
        {
            if (Request.Files.Count > 0)
            {
                if (Request.Files[0].ContentLength > 0)
                {
                    string contentType = Request.Files[0].ContentType;
                    Guid fileId = Guid.NewGuid();
                    string path = Server.MapPath("\\Uploads");
                    string fileNameWithoutPath = System.IO.Path.GetFileName(Request.Files[0].FileName);
                    string fileName = fileId.ToString().Replace("-", "") + fileNameWithoutPath;
                    fileName = System.IO.Path.Combine(path, fileName);
                    try
                    {
                        Request.Files[
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值