• Excel的下载和读取,部分代码(大神请路过)


    A.下载Excel表或其他文件(运用浏览器自带的功能实现下载)

    a)         前台页面:

    <a href="/SolidTransfers/DownloadFile" class="btn" >下载模板</a>

    b)        后台方法:

    public ActionResult DownloadFile()

            {

                var path = Server.MapPath("~/App_Data/OAupload/TempExcel/文件名.xlsx");

                var name = Path.GetFileName(path);

                string fileName = DateTime.Now.ToString("yyyyMMddmmss") + name;

                return File(path, "application/zip-x-compressed", fileName);

    }

    B.读取Excel表

    a)         前台页面:

    //action:路由规则,enctype:上传文件的格式(固定),method:文件的提交方式

    <form action="/SolidTransfers/ExcelWrite" enctype="multipart/form-data"              method="post" target="iframe">

                    <h5>请上传.xlsx文件</h5>

                    <input type="file" name="file" id="file" />

                    <input type="submit" class="btn" value="提交文件" style="float: right;">

                    //iframe作用:提交表单完成后不刷新页面,加载iframe的内容,这里用来阻止          页面的跳转

                    <iframe name="iframe" id="iframe" style="display: none"                           onload="Show()"></iframe>

    </form>

    b)        后台方法:

            [HttpPost]//规定提交文件的方式

            public void ExcelWrite()

            {

                try

                {

                    HttpFileCollectionBase fileCollection = Request.Files;

                    if (fileCollection.Count > 0)

                    {

                        HttpPostedFileBase postedFile = fileCollection[0];

                        string fileFolderPath =                                                           Request.MapPath("/App_Data/SolidExcel/OAupload/");

     

                        string proID = "PRJ" + DateTime.Now.ToString("yyyyMMddhhmmss");

                        var fileName = proID + postedFile.FileName;

                        if (!Directory.Exists(fileFolderPath))

                        {

                            Directory.CreateDirectory(fileFolderPath);

                        }

                        if (!string.IsNullOrEmpty(postedFile.FileName))

                        {

                            postedFile.SaveAs(fileFolderPath + "/" + fileName);

                        }

                        FileStream fs = System.IO.File.OpenRead(fileFolderPath + "/" +                                                  fileName);  //打开myxls.xls文件

                        XSSFWorkbook wk = new XSSFWorkbook(fs);   //把xls文件中的数据写入                                                                      wk中

                        ISheet sheet = wk.GetSheetAt(0);   //读取当前表数据

                        List<string> list = new List<string>();

                        list.Add(sheet.GetRow(0).GetCell(0).ToString()); //突发环境事件应急                                                         预案备案申请表0

                        list.Add(sheet.GetRow(2).GetCell(1).ToString());//单位名称1

                        list.Add(sheet.GetRow(3).GetCell(1).ToString());//法定代表人2

                        list.Add(sheet.GetRow(4).GetCell(1).ToString());//联系人3

                        list.Add(sheet.GetRow(5).GetCell(1).ToString());//传真4

     

     

                       

                    }

                }

                catch (Exception ex)

                {

                }

     

    }

  • 相关阅读:
    随手记录---transform 属性
    界面实例--图片布局在前端
    随手记录---jq如何判断当前元素是第几个元素
    PDF.Js的使用—javascript中前端显示pdf文件
    Jszip的使用和打包下载图片
    有关Canvas的一点小事—canvas和resize
    form input限制
    idea打war包正确姿势
    轻松建站神器!15个超精致的Bootstrap网站模板下载
    bootstrap教程
  • 原文地址:https://www.cnblogs.com/dongwenhua/p/9086676.html
Copyright © 2020-2023  润新知