NPOI导入Excel表格取数据

先准备一个Excel表格
在这里插入图片描述

前端页面,上传按钮

  <style>
        table tr td{
            border:1px solid ;
        }
    </style>
  <button id="chuan">点击上传1</button>
    <input type="file"  hidden id="moban1" value="上传模板" onchange="File1()"><br><table>
        <thead>
            <tr>
                <td>系统编号</td>
                <td>发起人</td>
                <td>类型</td>
                <td>类型编号</td>
                <td>主题</td>
                <td>发起日期</td>
                <td>反馈截止日期</td>
                <td>关闭日期</td>
                <td>当前状态</td>
            </tr>
        </thead>
        <tbody id="daoru">
           
        </tbody>
    </table>
    <div id="QWER"></div>

在这里插入图片描述
js部分

    //点击上传
        $("#chuan").click(function () {
            $("#moban1").click()
        })
            //文件上传
    function File1(type) {
          var formData = new FormData();
        var aaa = $("#moban1")[0];
        var filePath = URL.createObjectURL(aaa.files[0])
            //上传之后会有一个预览效果
           $("#QWER").append("<a href=\"" + filePath + "\">" + aaa.files[0].name+"</a><br/>")
           formData.append("file", aaa.files[0]);
            $.ajax({
                url: '@Url.Action("Moban2", "Home")',
                type: "POST",
                cache: false,
                processData: false,
                contentType: false,
                data: formData,
                success: function (datee) {
                    for (var i in datee.biao) {
                        $("#daoru").append(
                            " <tr>"
                            + "<td>" + datee.biao[i].id + "</td>"
                            + "<td>" + datee.biao[i].name + "</td>"
                            + "<td>" + datee.biao[i].lei + "</td>"
                            + "<td>" + datee.biao[i].leiid + "</td>"
                            + "<td>" + datee.biao[i].theme + "</td>"
                            + "<td>" + datee.biao[i].playdata + "</td>"
                            + "<td>" + datee.biao[i].lowdata + "</td>"
                            + "<td>" + datee.biao[i].closedata + "</td>"
                            + "<td>" + datee.biao[i].state + "</td>"
                            + "</tr >"
                        )
                    }
                

            })
    }

上传预览
在这里插入图片描述
后端代码
引用NPOI
在这里插入图片描述
在这里插入图片描述

using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;

定义的实体

public class ManagerInfo
    {
        public string id { get; set; }
        public string name { get; set; }
        public string lei { get; set; }
        public string leiid { get; set; }
        public string theme { get; set; }
        public string playdata { get; set; }
        public string lowdata { get; set; }
        public string closedata { get; set; }
        public string state { get; set; }
    }
  //导入Excel表格
        public ActionResult Moban2()
        {

                //创建一个实体接受表字段
                List<ManagerInfo> biao = new List<ManagerInfo>();
                var biao1 = new ManagerInfo();
               
                    HttpPostedFileBase file = Request.Files[0];
                    string extension = Path.GetExtension(file.FileName);
                    string fileName = Path.GetFileName(file.FileName);
                    var excelUploadPath = "~/Files/Upload/file/" + fileName;
                //没有路径的话创建一个文件夹
                    if (!Directory.Exists(Path.GetDirectoryName(Server.MapPath(excelUploadPath))))
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(Server.MapPath(excelUploadPath)));
                    }
                    //1.1读取文件                            路径
                    FileStream fs = new FileStream(Server.MapPath(excelUploadPath), FileMode.Create, FileAccess.Write);
                    file.InputStream.CopyTo(fs);
                    fs.Close();
                    //读取文件
                    FileStream file1 = new FileStream(Server.MapPath(excelUploadPath), FileMode.Open, FileAccess.Read);
                    HSSFWorkbook wb = new HSSFWorkbook(file1);
                    HSSFSheet sheet = (HSSFSheet)wb.GetSheet("sheet");
                    //判断数据多少行
                    var rownum = sheet.LastRowNum;
                //循环行
                    for (int j = 2; j <= rownum; j++)
                    {
                        IRow row = sheet.GetRow(j);
                        biao1.id = sheet.GetRow(j).GetCell(0).ToString();
                        biao1.name = sheet.GetRow(j).GetCell(1).ToString();
                        biao1.lei = sheet.GetRow(j).GetCell(2).ToString();
                        biao1.leiid = sheet.GetRow(j).GetCell(3).ToString();
                        biao1.theme = sheet.GetRow(j).GetCell(4).ToString();
                        biao1.playdata = sheet.GetRow(j).GetCell(5).ToString();
                        biao1.lowdata = sheet.GetRow(j).GetCell(6).ToString();
                        biao1.closedata = sheet.GetRow(j).GetCell(7).ToString();
                        biao1.state = sheet.GetRow(j).GetCell(8).ToString();
                        biao.Add(biao1);
                    }
                    file1.Close();
             

                return Json(new { code = 200, biao });
            
          
        }

最终效果
在这里插入图片描述

代码命名不规范,后续会改进

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值