• 导入 导出 压缩 解压


    --DAL

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Model;
    using Newtonsoft.Json;
    using System.Data.SqlClient;
    using System.Data;
    using System.Data.OleDb;
    namespace DAL
    {
    public class stuDAL
    {
    public SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=lala;Integrated Security=True");
    /// <summary>
    /// 显示
    /// </summary>
    /// <returns></returns>
    public List<stuModel> show()
    {
    string sql = "select * from stu";
    SqlDataAdapter dr = new SqlDataAdapter(sql, conn);
    DataTable ds = new DataTable();
    dr.Fill(ds);
    var p = JsonConvert.SerializeObject(ds);
    var pp = JsonConvert.DeserializeObject<List<stuModel>>(p);
    return pp;
    }
    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="m"></param>
    /// <returns></returns>
    public int add(stuModel m)
    {
    string sql = "insert into stu values('" + m.Name + "','" + m.age + "')";
    conn.Open();
    SqlCommand cmd = new SqlCommand(sql, conn);
    int i = cmd.ExecuteNonQuery();
    conn.Close();
    return i;
    }
    /// <summary>
    /// 表
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public DataTable biao(string path)
    {

    string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=Excel 8.0;";

    OleDbConnection conn = new OleDbConnection(strConn);
    OleDbDataAdapter oda = new OleDbDataAdapter("select * from [Sheet0$]", conn);
    DataSet ds = new DataSet();
    oda.Fill(ds);
    return ds.Tables[0];
    }
    }
    }

    --控制器

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using NPOI.HSSF.UserModel;
    using NPOI.SS.UserModel;
    using System.IO;
    using BLL;
    using System.Data;
    using Model;
    namespace 练习上传.Controllers
    {
    public class showController : Controller
    {
    stuBLL bll = new stuBLL();
    // GET: show
    /// <summary>
    /// 显示 压缩 解压
    /// </summary>
    /// <returns></returns>
    public ActionResult Index()
    {
    var reust = new BLL.stuBLL().show();
    return View(reust);
    }
    // GET: show
    /// <summary>
    /// 压缩
    /// </summary>
    /// <returns></returns>
    public ActionResult ya()
    {
    ZipHandler.ZipDirectory(@"C:Users过客Desktop练习练习上传 压缩 解压 简便", @"C:Users过客Desktop练习Demo1.zip", "123");
    List<stuModel> ls = bll.show(); //返回显示
    return View("Index", ls);

    }
    // GET: show
    /// <summary>
    /// 解压
    /// </summary>
    /// <returns></returns>
    public ActionResult jie()
    {
    ZipHandler.UnZipFile(@"C:Users过客Desktop练习Demo1.zip", @"C:Users过客Desktop练习Demo1", "123");
    //要解压的文件路径 2 解压成功的路径
    List<stuModel> ls = bll.show();//返回显示
    return View("Index",ls);
    }
    /// <summary>
    /// 导出
    /// </summary>
    /// <returns></returns>
    public FileResult DaoChu()
    {
    var p = new BLL.stuBLL().show();
    if (p != null || p.Count > 0)
    {
    HSSFWorkbook book = new HSSFWorkbook();

    ISheet sheet = book.CreateSheet();

    IRow row = sheet.CreateRow(0);

    row.CreateCell(0).SetCellValue("编号");
    row.CreateCell(1).SetCellValue("名称");
    row.CreateCell(2).SetCellValue("年龄");
    for (int i = 0; i < p.Count; i++)
    {
    IRow rows = sheet.CreateRow(i + 1);
    rows.CreateCell(0).SetCellValue(p[i].ID);
    rows.CreateCell(1).SetCellValue(p[i].Name);
    rows.CreateCell(2).SetCellValue(p[i].age);
    }
    MemoryStream ms = new MemoryStream();
    book.Write(ms);
    ms.Seek(0, SeekOrigin.Begin);
    return File(ms, "application/vnd-excel", "学生.xls");
    }
    else
    {
    MemoryStream ms = new MemoryStream();

    ms.Seek(0, SeekOrigin.Begin);
    return File(ms, "application", "");
    }
    }
    /// <summary>
    /// 导入
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public ActionResult DaoRu(HttpPostedFileBase path)
    {

    string strfileName = Server.MapPath("/Word/"); //存储文件的地方

    if (!Directory.Exists(strfileName)) //判断文件路径是否存在
    {
    Directory.CreateDirectory(strfileName);
    }
    string allFilePath = strfileName + "/" + path.FileName;
    string fName = Path.GetFileName(allFilePath); //获取文件名
    path.SaveAs(strfileName + fName);

    #region /// Execl导入部分

    //execl文件读取
    stuBLL bll = new stuBLL();

    DataTable dt = bll.biao(strfileName + fName);//把数据获取到

    //把读取的数据导入到数据库
    foreach (DataRow dr in dt.Rows)
    {
    stuModel m = new stuModel();
    m.ID = Convert.ToInt32(dr[0]);
    m.Name = dr[1].ToString();
    m.age = dr[2].ToString();

    bll.add(m);
    }

    #endregion

    List<stuModel> ls = bll.show();

    return View("Index", ls);
    }
    }
    }

    --前台


    @{
    Layout = null;
    }
    @model List<Model.stuModel>
    <!DOCTYPE html>

    <html>
    <head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    </head>
    <body>
    <div>
    <form action="/show/DaoRu" enctype="multipart/form-data" method="post">
    <input id="File1" type="file" name="path" />
    <input id="Submit1" type="submit" value="导入" />
    </form>
    <input id="Button1" type="button" value="导出" onclick="yang()" />
    <input id="Button1" type="button" value="压缩" onclick="ya()" />
    <input id="Button1" type="button" value="解压" onclick="jie()" />
    <table>
    <tr>
    <td>编号</td>
    <td>名称</td>
    <td>年龄</td>
    </tr>
    <tbody>
    @foreach (var item in Model)
    {
    <tr>
    <td>@item.ID</td>
    <td>@item.Name</td>
    <td>@item.age</td>
    </tr>
    }
    </tbody>
    </table>
    </div>
    <script>
    function yang()
    {
    location.href = "/show/DaoChu";
    }
    function ya() {
    location.href = "/show/ya";
    alert("压缩成功");
    }
    function jie() {

    location.href = "/show/jie";


    }

    </script>
    </body>
    </html>

  • 相关阅读:
    自然二进制与格雷码的转换
    状态机小结
    FSM的几种策略
    跨越鸿沟:同步世界中的异步信号
    边沿检测技术
    门控时钟和时钟使能
    ALTERA器件中复位电路实现之-异步复位同步化
    同步复位
    Altera USB Blaster 仿真器(EPM240仿制版
    五、裸机烧写
  • 原文地址:https://www.cnblogs.com/xing-xing/p/7884328.html
Copyright © 2020-2023  润新知