zoukankan      html  css  js  c++  java
  • Mvc导入

    [HttpPost]
    public void Import()
    {
    //获取文件
    
    HttpPostedFileBase fileBase = Request.Files["file"];
    
    if(fileBase==null)
    {
      Response.Write("");
    }
    //转数据流
    Stream stream = fileBase.InputStream;
    IWorkbook workbook = null;
    
    if (Path.GetExtension(fileBase.FileName).ToLower().Equals(".xls"))
    {
      workbook = new HSSFWorkbook(stream);
    }
    //获取sheet
    ISheet sheet = workbook.GetSheetAt(0);
    //获取头
    IRow row = sheet.GetRow(0);
    //定义datatable
    DataTable dt = new DataTable();
    foreach (ICell item in row.Cells)
    {
    //循环添加表头
      dt.Columns.Add(item.StringCellValue);
    }
    for (int i = 0; i <= sheet.LastRowNum; i++)
    {
      DataRow dr = dt.NewRow();
      for (int j = 0; j < sheet.GetRow(i).Cells.Count; j++)
    {
    
    CellType cellType = sheet.GetRow(i).Cells[j].CellType;
    int index = sheet.GetRow(i).Cells[j].ColumnIndex;
    switch (cellType)
    {
      case CellType.Numeric:dr[index]=sheet.GetRow(i).Cells[j].NumericCellValue;
      break;
      case CellType.String:
      dr[index] = sheet.GetRow(i).Cells[j].StringCellValue;
      break;
      case CellType.Boolean:
      dr[index] = sheet.GetRow(i).Cells[j].BooleanCellValue;
      break;
      }
    }
      dt.Rows.Add(dr);
    }
    
    
    //sql拼接
    try
    {
    dt.Columns.RemoveAt(0);
    foreach (DataRow item in dt.Rows)
    {
      string sql = $"inserrt into mess values(";
      foreach (object it in item.ItemArray)
      {
      sql += $"'{it}',";
      }
      sql = sql.TrimEnd(',');
      sql += ")";
      DB.ExecuteNonQuery(sql);
      }
    }
    catch (Exception)
    {
    
    throw;
    }
    }

    //前台

    <form action="/Default/Import" method="post" enctype="multipart/form-data">
    <input type="file" name="file" id="file">
    <input type="submit" name="name" class="btn btn-success btn-sm" value="导入">
    </form>

     

  • 相关阅读:
    jquery 第二节 Dom和jQuery的互相转换
    jquery 第一节 什么是jQuery
    SQL四大语句、四大完整性、五大约束
    empty和is_null以及isset函数在0、”0”、‘空串’、NULL、false、array()的计算值
    WAMP常用环境配置
    解读Java内部类
    每日编程系列——暗黑的字符串
    每日编程系列——跳石板
    每日编程系列——优雅的点
    每日编程系列——回文序列
  • 原文地址:https://www.cnblogs.com/l1999/p/11976396.html
Copyright © 2011-2022 走看看