Aspose.Cells导入导出
2016-07-04 10:26
971 查看
插件:Aspose.Cells
没有安装office插件也能使用;
导出:不能使用ajax异步·
导入Controller
● 由于接收多个文件,所以控制器方法的参数变成了IEnumerable<HttpPostedFileBase> files
● 变量files与前台视图的name属性值对应
● 如果没有指定的文件夹,就创建一个文件夹
为什么使用HttpPostedFileBase而不是FormCollection来接收文件
可见,FormCollection是键值集合,键是string类型,值是string类型,而上传的文件类型不是string,所以不能用FormCollection作为参数来接收文件。
参考资料:
MVC中的文件上传http://blog.sina.com.cn/s/blog_75a555e40101q8i7.html
Confusing required maxRequestLength and maxAllowedContentLength settings
http://forums.iis.net/t/1169846.aspx
关于MVC4.0 WebAPI上传图片重命名以及图文结合
http://www.cnblogs.com/ang/archive/2012/10/24/2634176.html
ASP.NET Web Api Self Host大文件上传功能
http://www.cnblogs.com/fenglin1985/archive/2013/01/29/2879926.html
首发地址:http://www.yuanxj.net/2014/02/upladfile/
没有安装office插件也能使用;
导出:不能使用ajax异步·
/// <summary> /// 导入试题 /// </summary> /// <param name="userId">用户Id</param> /// <param name="courseId">课程Id</param> /// <returns>int userId, int courseId,</returns> [HttpPost] public ActionResult ImportQuestions(HttpPostedFileBase upFileBase) { HttpPostedFileBase fileBase = Request.Files["fileImport"]; if (fileBase != null) { #region 保存文件 string fileName = Path.GetFileName(fileBase.FileName); string fileNameNoExt = Path.GetFileNameWithoutExtension(fileBase.FileName); string extension = Path.GetExtension(fileName); //if (extension.ToLower() != ".zip") //extension.ToLower() != ".xls" || extension.ToLower() != ".xlsx" || //{ // //window.location.href='@Url.Action('a','b')' // return Content("<script type='text/javascript'>alert('请上传zip格式的压缩文件');window.location.href='/admin/ImportQuestions';</script>"); //} string filePath = "/UploadFile/试题/"; // +DateTime.Now.ToString("yyyyMMdd") + "/"; if (!Directory.Exists(Server.MapPath(filePath))) //文件夹 { Directory.CreateDirectory(Server.MapPath(filePath)); } string nowTime = DateTime.Now.ToString("yyyyMMddHHmmssfff"); string fullFileName = Server.MapPath(filePath + nowTime + "_" + userId + "_" + courseId + "_" + fileName);//文件名 fileBase.SaveAs(fullFileName); //保存在服务器 #endregion string fileFullName = Path.Combine(strFileUrl, Path.GetFileName(file.Name)); #region 导入EXECL Workbook BuildReport_WorkBook = new Workbook(); BuildReport_WorkBook.Open(fileFullName); Worksheets sheets = BuildReport_WorkBook.Worksheets; //试题表 Worksheet workSheetQuestion = BuildReport_WorkBook.Worksheets["试题表"]; // sheet1 Cells cellsQuestion = workSheetQuestion.Cells; //单元格 Worksheet workSheetDesc = BuildReport_WorkBook.Worksheets["试题描述"]; //sheet2 Cells cellsDesc = workSheetDesc.Cells; //试题表 for (int i = 1; i < cellsQuestion.MaxDataRow + 1; i++) { #region 试题表 Question modQuestion = new Question();//实体 int questionId = cellsQuestion[i, 0].IntValue; dicQuestion[i] = cellsQuestion[i, 0].IntValue; modQuestion.CourseId = cellsQuestion[i, 1].IntValue; modQuestion.QuestionType = cellsQuestion[i, 2].IntValue; //数据库操作 } for (int j = 1; j < cellsDesc.MaxDataRow + 1; j++) { int questionDescFK = cellsDesc[j, 1].IntValue; if (questionId == questionDescFK) { Question_Desc modQuestionDesc = new Question_Desc(); //实体 modQuestionDesc.QuestionId = questionIdNew; //新的 modQuestionDesc.Description = cellsDesc[j, 2].StringValue.Trim(); modQuestionDesc.Analyses = cellsDesc[j, 3].StringValue.Trim(); modQuestionDesc.DescriptionText = cellsDesc[j, 4].StringValue.Trim(); //数据库操作 } } #endregion } return View(); }
导入Controller
● 由于接收多个文件,所以控制器方法的参数变成了IEnumerable<HttpPostedFileBase> files
● 变量files与前台视图的name属性值对应
● 如果没有指定的文件夹,就创建一个文件夹
为什么使用HttpPostedFileBase而不是FormCollection来接收文件
public sealed class FormCollection : NameValueCollection, IValueProvider
可见,FormCollection是键值集合,键是string类型,值是string类型,而上传的文件类型不是string,所以不能用FormCollection作为参数来接收文件。
参考资料:
MVC中的文件上传http://blog.sina.com.cn/s/blog_75a555e40101q8i7.html
Confusing required maxRequestLength and maxAllowedContentLength settings
http://forums.iis.net/t/1169846.aspx
关于MVC4.0 WebAPI上传图片重命名以及图文结合
http://www.cnblogs.com/ang/archive/2012/10/24/2634176.html
ASP.NET Web Api Self Host大文件上传功能
http://www.cnblogs.com/fenglin1985/archive/2013/01/29/2879926.html
首发地址:http://www.yuanxj.net/2014/02/upladfile/
相关文章推荐
- vs2010 asp.net webForm Html表格数据模板转换pdf文件,web打印Pdf
- ASP.NET网站发布-允许更新此预编译站点 打勾与不打勾的区别
- .net core 1.0 中的asp.net identity 的基本使用 序言
- [ASP.NET]Repeater的使用方法
- ASP.NET总结
- ASP.NET MVC4入门到精通系列目录汇总
- 学习ASP.NET Core,你必须了解无处不在的“依赖注入”
- .NET Core 1.0学习(2)
- Spring 之AOP AspectJ切入点语法详解
- Raspberry Raspbian turning off the UART functioning as a serial console
- 【ASP.NET】——有缘千里来相会
- asp.net mvc5 设置Area下的为启动页
- ASP.NET视频总结
- ASP.NET的缓存机制
- 【iReport+JasperReport】2.在Java工程中利用jasper导出PDF报表
- 【iReport+JasperReport】1.iReport与JasperReport基础
- .NET Core 1.0发布:微软开源跨平台大布局序幕
- 8 种提升 ASP.NET Web API 性能的方法
- ASP.NET 正则替换URL参数值
- Metasploit Framework 总结