zoukankan      html  css  js  c++  java
  • 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)

                {

                }

     

    }

  • 相关阅读:
    2016.5.15——leetcode:Number of 1 Bits ,
    2016.5.14——leetcode-HappyNumber,House Robber
    记录学习过程
    npm 模块安装机制简介
    搭建Vue.js开发环境(window10)
    pwd 显示当前所在的工作路径
    Lucene 6.5.0 入门Demo
    java.lang.UnsupportedClassVersionError
    window.onload 和 $(document).ready(function(){}) 的区别
    plsql + 客户端 连接oracle数据库
  • 原文地址:https://www.cnblogs.com/dongwenhua/p/9086676.html
Copyright © 2011-2022 走看看