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)

                {

                }

     

    }

  • 相关阅读:
    织梦开发——相关阅读likeart应用
    织梦标签教程
    织梦专题调用代码
    HIT 2543 Stone IV
    POJ 3680 Intervals
    HIT 2739 The Chinese Postman Problem
    POJ 1273 Drainage Ditches
    POJ 2455 Secret Milking Machine
    SPOJ 371 Boxes
    HIT 2715 Matrix3
  • 原文地址:https://www.cnblogs.com/dongwenhua/p/9086676.html
Copyright © 2011-2022 走看看