码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-05-25 10:57:10      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:exist   float   跳转   director   upload   str   文件的   cti   cep   

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)

            {

            }

 

}

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

标签:exist   float   跳转   director   upload   str   文件的   cti   cep   

原文地址:https://www.cnblogs.com/dongwenhua/p/9086676.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!