public HttpResponseMessage Upload() { HttpPostedFile file = HttpContext.Current.Request.Files["school"]; ErrMsg msg = new ErrMsg(); IImport import = ExcelFactory.Instance().GetExcelImporter(new eh.impls.configurations.ExcelConfiguration(1, 0, 0), msg); IList <School> list = SchoolDto.ToList(import.Import <SchoolDto>(file.InputStream)); if (msg.Count != 0) { return(ToJson(msg.GetErrors(), status_code: 0, msg: "fail")); } else { try { Service.Add(list); return(ToJson("success")); } catch (SqlException ex) { msg.AddErrMsg(ex.Message); return(ToJson(msg.GetErrors(), status_code: 0, msg: "fail")); } } }
public void TestImport() { ErrMsg msg = new ErrMsg(); IImport import = ExcelFactory.Instance().GetExcelImporter(new ExcelConfiguration(1, 0, 0), msg); FileStream fs = new FileStream(@"D:\projects\yueyouyuebei\src\TravelAgent.Web\TravelAgent.WebAPI\template\school.xls", FileMode.Open); IList <School> list = import.Import <School>(fs); if (msg.Count != 0) { msg.GetErrors().ForEach(s => { Console.WriteLine(s); }); return; } foreach (School item in list) { Console.WriteLine(item.ToString()); } }
public HttpResponseMessage Download() { IList <ReferencesSchool> list = Service.GetAll(); IList <RefsFileDto> dtos = RefsFileDto.ToDtos(list); ErrMsg msg = new ErrMsg(); ExcelConfiguration cfg = new ExcelConfiguration(); cfg.TemplatePath = ConfigurationManager.AppSettings["refsDataPath"]; cfg.TemplateRowIndex = 1; IExport export = ExcelFactory.Instance().GetExcelExporter(cfg, msg); byte[] data = export.Export <RefsFileDto>(dtos); MemoryStream ms = new MemoryStream(data); HttpResponseMessage res = new HttpResponseMessage(HttpStatusCode.OK); res.Content = new StreamContent(ms); res.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); res.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "refs_file.xlsx" }; return(res); }