npoi上传xlsx文件,并读取数据

视图

public PartialViewResult UploadIndex()
{
    return PartialView();
}
<div>
    <fieldset style="height:100px;">
        <legend>说明</legend>
        <h6 style="color: red; font-size: 15px;">请从IV查询中进行下载数据,按其中的表格格式进行数据上传,否则会导致上传失败或数据出错!</h6>
    </fieldset>
    <fieldset style="height: 50px;">
    <legend>工单号</legend>
      <div style="margin-top: 10px;">
      <form id="formAkIvUpload" name="formToUpload" method="post" action="AkIv/UploadProcess" enctype="multipart/form-data" target="msgAkIvUpload">
          <input id="fileAkIvUpload" type="file" size="45" name="fileAkIvUpload" />
          <input type="submit" style="cursor: pointer;" value="上传" />
      </form>
      <iframe id="msgAkIvUpload" name="msgAkIvUpload" style="display: none;"></iframe>
     </div>
    </fieldset>
</div>

处理

public void UploadProcess()
{
            //用于反馈执行信息
            string strReturn =
@"
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type='text/javascript'>
        alert('@msg');
    </script>
</head>
<body>
</body>
</html>
";
            //获取文件
            var file = Request.Files["fileAkIvUpload"];
            string msg = "";

            //保存
            if (file != null && file.ContentLength > 0)
            {
                //文件目录
                string path = Server.MapPath("~") + "Content\\File\\";
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                //文件类型验证
                string[] allowExtension = { "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" };
                if (!allowExtension.Contains(file.ContentType))
                {
                    msg = "上传xlsx类型文件";
                }
                else
                {
                    string filePath = path + "\\" + file.FileName;
                    if (System.IO.File.Exists(filePath))
                        System.IO.File.Delete(filePath);
                    file.SaveAs(filePath);//文件

                    try
                    {
                        NPOI.XSSF.UserModel.XSSFWorkbook hssfworkbook;
                        using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                        {
                            hssfworkbook = new NPOI.XSSF.UserModel.XSSFWorkbook(fs);
                        }

                        NPOI.SS.UserModel.ISheet sheet = hssfworkbook.GetSheetAt(0);
                        System.Collections.IEnumerator rows = sheet.GetRowEnumerator();

                        while (rows.MoveNext())
                        {
                            NPOI.SS.UserModel.IRow row = (NPOI.SS.UserModel.IRow)rows.Current;

                            if (row.GetCell(0).ToString() == "条码" || row.GetCell(0) == null || string.IsNullOrEmpty(row.GetCell(0).ToString()))
                                continue;

                            try
                            {

                                float power = 0;
                                try
                                {
                                    power = row.GetCell(19) == null ? 0 : float.Parse(row.GetCell(19).ToString());
                                }
                                catch { }

                                AkIv akIv = new AkIv()
                                {
                                    BarCode = row.GetCell(0) == null ? "" : row.GetCell(0).ToString(),
                                    DateTime = row.GetCell(1) == null ? DateTime.Now : Convert.ToDateTime(row.GetCell(1).ToString()),
                                    Eff = row.GetCell(2) == null ? 0 : float.Parse(row.GetCell(2).ToString()),
                                    Isc = row.GetCell(3) == null ? 0 : float.Parse(row.GetCell(3).ToString()),
                                    Voc = row.GetCell(4) == null ? 0 : float.Parse(row.GetCell(4).ToString()),
                                    Rs = row.GetCell(5) == null ? 0 : float.Parse(row.GetCell(5).ToString()),
                                    Rsh = row.GetCell(6) == null ? 0 : float.Parse(row.GetCell(6).ToString()),
                                    Pmax = row.GetCell(7) == null ? 0 : float.Parse(row.GetCell(7).ToString()),
                                    Vpm = row.GetCell(8) == null ? 0 : float.Parse(row.GetCell(8).ToString()),
                                    Ipm = row.GetCell(9) == null ? 0 : float.Parse(row.GetCell(9).ToString()),
                                    FF = row.GetCell(10) == null ? 0 : float.Parse(row.GetCell(10).ToString()),
                                    Sun = row.GetCell(11) == null ? 0 : float.Parse(row.GetCell(11).ToString()),
                                    Temp = row.GetCell(12) == null ? 0 : float.Parse(row.GetCell(12).ToString()),
                                    Class = row.GetCell(13) == null ? "" : row.GetCell(13).ToString(),
                                    Employee = row.GetCell(14) == null ? "" : row.GetCell(14).ToString(),
                                    LineTitle = row.GetCell(15) == null ? "" : row.GetCell(15).ToString(),
                                    StationTitle = row.GetCell(16) == null ? "" : row.GetCell(16).ToString(),
                                    OrderNumber = row.GetCell(17) == null ? "" : row.GetCell(17).ToString(),
                                    BatterySupplier = row.GetCell(18) == null ? "" : row.GetCell(18).ToString(),
                                    Power = power,
                                    InterconnectId = row.GetCell(20) == null ? "" : row.GetCell(20).ToString(),
                                    InterconnectSpec = row.GetCell(21) == null ? "" : row.GetCell(21).ToString()
                                };

                                _akIvRepository.Insert(akIv);
                            }
                            catch (Exception e)
                            {

                            }
                        }

                        SaveUserLog("AkIv上传", filePath, true);
                        msg = "上传功成成功!";
                    }
                    catch (Exception e)
                    {
                        msg = e.ToString();
                    }
                }
            }
            else
            {
                msg = "请选择文件!";
            }

            System.Web.HttpContext.Current.Response.ContentType = "text/html";
            System.Web.HttpContext.Current.Response.Write(strReturn.Replace("@msg", msg));
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值