Excel导入

public ActionResult Excel(HttpPostedFileBase file)
        {
            HttpPostedFileBase files = file;//接收客户端传递过来的数据.
            try
            {
                if (files == null)
                {
                    return Content("请选择上传的Excel文件");
                }
                else
                {
                    file.SaveAs(Server.MapPath("~/UploadFile/Excel/" + file.FileName));
                    //对文件的格式判断,此处省略
                    AltasEntities db = new AltasEntities();//EF上下文对象
                    Stream inputStream = files.InputStream;
                    IWorkbook workbook;
                    string fileExt = Path.GetExtension(files.FileName);

                    if (fileExt == ".xls")
                    {
                        workbook = new HSSFWorkbook(inputStream);
                    }
                    else if (fileExt == ".xlsx")
                    {
                        workbook = new XSSFWorkbook(inputStream);
                    }
                    else
                    {
                        return null;
                    }
                    NPOI.SS.UserModel.ISheet sheet = workbook.GetSheetAt(0);
                    int rowCount = sheet.LastRowNum + 1;

                    for (int i = (sheet.FirstRowNum + 1); i <= rowCount - 1; i++)
                    {
                        IRow row = sheet.GetRow(i);
                        Equiemints model = new Equiemints();

                        if (row != null)
                        {
                            if (row.GetCell(0) != null)
                            {
                                model.EquiemintId = GetCellValue(row.GetCell(0)).ToUpper();
                            }
                            if (row.GetCell(1) != null)
                            {
                                model.Company = GetCellValue(row.GetCell(1));
                            }
                            if (row.GetCell(2) != null)
                            {
                                model.ModelsType = GetCellValue(row.GetCell(2));
                            }
                            if (row.GetCell(3) != null)
                            {
                                model.OpenCreatetime = DateTime.Parse(GetCellValue(row.GetCell(3)));
                            }
                            if (row.GetCell(4) != null)
                            {
                                model.ZBType = GetCellValue(row.GetCell(4));
                            }
                            if (row.GetCell(5) != null)
                            {
                                model.ZBEndCreatetime = DateTime.Parse(GetCellValue(row.GetCell(5)));
                                //model.SalesName = GetCellValue(row.GetCell(5));
                            }
                            if (row.GetCell(6) != null)
                            {
                                model.a1 = GetCellValue(row.GetCell(6));
                            }
                            if (row.GetCell(7) != null)
                            {
                                model.a2 = GetCellValue(row.GetCell(7));
                            }
                            if (row.GetCell(8) != null)
                            {
                               model.a3 = GetCellValue(row.GetCell(8));
                            }
                            if (row.GetCell(9) != null)
                            {
                               model.a4 = GetCellValue(row.GetCell(9));
                            }
                            if (row.GetCell(10) != null)
                            {
                               model.a5 = GetCellValue(row.GetCell(10));
                            }
                        }

                        db.Equiemints.Add(model);
                    }
                    db.SaveChanges();
                    return Content("<script>alert('导入成功!',location.href='/Equiemints/Index');</script>");

                }

            }
            catch (Exception)
            {
                return Content("<script>alert('导入失败!');</script>");
            }

        }

        /// <summary>  
        /// 根据Excel列类型获取列的值  
        /// </summary>  
        /// <param name="cell">Excel列</param>  
        /// <returns></returns>  
        private static string GetCellValue(ICell cell)
        {
            if (cell == null)
                return string.Empty;
            switch (cell.CellType)
            {
                case CellType.Blank:
                    return string.Empty;
                case CellType.Boolean:
                    return cell.BooleanCellValue.ToString();
                case CellType.Error:
                    return cell.ErrorCellValue.ToString();
                case CellType.Numeric:
                case CellType.Unknown:
                default:
                    return cell.ToString();
                case CellType.String:
                    return cell.StringCellValue;
                case CellType.Formula:
                    try
                    {
                        HSSFFormulaEvaluator e = new HSSFFormulaEvaluator(cell.Sheet.Workbook);
                        e.EvaluateInCell(cell);
                        return cell.ToString();
                    }
                    catch
                    {
                        return cell.NumericCellValue.ToString();
                    }
            }
        }

Excel导出

public ActionResult DcExcel(List<int> id)
        {
             var db = new AltasEntities();
            if (id == null)
            {
                id = db.Equiemints.Select(s => s.Id).ToList();
            }
            string filename = DateTime.Now.ToString("yyyyMMddHHmmss") + ".csv";

            string path = Server.MapPath("/UploadFile/" + filename);
            FileInfo fi = new FileInfo(path);
            using (StreamWriter sw = fi.CreateText())
            {
                sw.WriteLine("设备序号,所属公司,机型,开始时间,质保类型,结束时间,服务销售,是否绑定,字段1,字段2,字段3");
                foreach (int i in id)
                {

        //找到要导出的数据
                    var saa = db.Equiemints.Find(i);
                    if (saa.a1==null)
                    {
                        saa.a1 = "无";
                    }
                    if (saa.a2 == null)
                    {
                        saa.a2 = "无";
                    }
                    if (saa.a3 == null)
                    {
                        saa.a3 = "无";
                    }
                    sw.WriteLine(
                        saa.EquiemintId.ToString()
                        + "," + saa.Company.ToString()
                        + "," + saa.ModelsType.ToString()
                         + "," + saa.OpenCreatetime.ToString()
                         + "," + saa.ZBType.ToString()
                         + "," + saa.ZBEndCreatetime.ToString()
                          + "," + saa.uname.ToString()
                          + "," + saa.bangname.ToString()
                         + "," + saa.a1.ToString()
                         + "," + saa.a2.ToString()
                         + "," + saa.a3.ToString()
                        );
                }
                sw.Close();
            }
           
            var name = Path.GetFileName(path);
            return File(path, "application/x-zip-compressed", name);
        }

excel 导入 与 导出的更多相关文章

  1. C# Excel导入、导出【源码下载】

    本篇主要介绍C#的Excel导入.导出. 目录 1. 介绍:描述第三方类库NPOI以及Excel结构 2. Excel导入:介绍C#如何调用NPOI进行Excel导入,包含:流程图.NOPI以及C#代 ...

  2. ASP.NET MVC5+EF6+EasyUI 后台管理系统(63)-Excel导入和导出-自定义表模导入

    系列目录 前言 上一节使用了LinqToExcel和CloseXML对Excel表进行导入和导出的简单操作,大家可以跳转到上一节查看: ASP.NET MVC5+EF6+EasyUI 后台管理系统(6 ...

  3. C# Excel导入、导出

    本篇主要介绍C#的Excel导入.导出. 目录 1. 介绍:描述第三方类库NPOI以及Excel结构 2. Excel导入:介绍C#如何调用NPOI进行Excel导入,包含:流程图.NOPI以及C#代 ...

  4. JXLS (Excel导入、导出工具使用)

    JXLS (Excel导入.导出工具使用) 1:简介: jxls是一个简单的.轻量级的excel导出库,使用特定的标记在excel模板文件中来定义输出格式和布局.java中成熟的excel导出工具有p ...

  5. ASP.NET MVC5+EF6+EasyUI 后台管理系统(63)-Excel导入和导出

    系列目录 昨天文章太过仓促没有补充导出的示例源码,在者当时弄到到很晚没时间做出导出功能,对阅读理解造成影响,现补充一份示例源码,顺便补充导出的功能说明,望理解 示例代码下载   https://yun ...

  6. winform之excel导入和导出

    引用命名空间   using Microsoft.Office.Interop.Excel;DataGridView 导出到Excel public static void SaveAs(DataGr ...

  7. ASP.NET MVC5+EF6+EasyUI 后台管理系统(87)-MVC Excel导入和导出

    本文示例代码下载: 链接:http://pan.baidu.com/s/1jHBdgCA 密码:hzh7 ps:Vs数据库脚本在解压目录下,修改web.config数据库链接,示例代码包含:导入,导出 ...

  8. ASP.NET MVC5+EF6+EasyUI 后台管理系统(88)-Excel导入和导出-自定义表模导出

    前言 之前说了导入和导出,也提供了自定义的表模的导入,可见LinqToExcel可以做的事情不仅仅如此 这次我们来演示比较复杂的导出Excel,导出复杂的Excel与导入复杂的Excel原理基本是一样 ...

  9. c# excel 导入 与 导出(可直接用)

    c#操作excel方式很多 采用OleDB读取EXCEL文件: 引用的com组件:Microsoft.Office.Interop.Excel.dll   读取EXCEL文件 将EXCEL文件转化成C ...

随机推荐

  1. Redis集群方案

    Redis集群方案 前段时间搞了搞Redis集群,想用做推荐系统的线上存储,说来挺有趣,这边基础架构不太完善,因此需要我们做推荐系统的自己来搭这个存储环境,就自己折腾了折腾.公司所给机器的单机性能其实 ...

  2. 基于认证的代理平台搭建配置squid-20130730

    基于认证的代理平台搭建配置squid-20130730 功能:通过squid代理实现 (1)基于用户名密码认证的出口ip路由选择 (2)基于client源ip的出口ip路由选择 (3)基于连接本机ip ...

  3. [转]How WebKit’s Event Model Works

    原文:https://homes.cs.washington.edu/~burg/projects/timelapse/articles/webkit-event-implementation/ Fi ...

  4. 用django搭建一个简易blog系统(翻译)(二)

    03. Starting the blog app 在这部分,将要为你的project创建一个blog 应用,通过编辑setting.py文件,并把它添加到INSTALLED_APPS. 在你的命令行 ...

  5. 消除Switch...Case的过程

    http://www.cnblogs.com/happyframework/p/3300170.html 目录 备注需求第一遍代码(重复的代码)第二遍代码(消除重复)备注 备注返回目录 不要重复自己, ...

  6. JDBC之事务隔离级别以及ACID特性

    JDBC之事务隔离级别以及ACID特性 事务隔离级别: 1.更新遗失(Lost update) 两个事务都同时更新一行数据,但是第二个事务却中途失败退出,导致对数据的两个修改都失效了.这是因为系统没有 ...

  7. ngx-push-stream模块源码学习(四)——订阅

    一.概述 push stream模块允许三种模式的订阅者: longpolling:每收到服务端响应数据即断开连接然后迅速重连,连接耗时可以忽略 stream:与服务端保持长连接,持续不断的请求-&g ...

  8. 一种利用异常机制基于MVC过滤器的防止重复提交的机制分享

    防止重复提交验证机制 某些时候因为系统反应稍慢,急性子用户可能不耐烦会进行重复的提交,这个操作不仅可能造成系统负担,也可能产生垃圾数据. 出现这两种状况都是我们不希望的. 为此,在公司项目系统设计了以 ...

  9. new和instanceof的内部机制

    new和instanceof的内部机制 首先我们来看看obj = new o()这条语句发生了什么: var obj = (function(){ var obj = {}; obj.__proto_ ...

  10. IOS设计模式学习(6)生成器

    1 前言 有时候,构建某些对象有多种不同方式.如果这些逻辑包含在构建这些对象的类中的单一方法中,构建的逻辑会非常荒唐(例如,针对各种构建需求的一大片嵌套if-else或者switch-case语句). ...