Exemplo n.º 1
0
 public async Task <IHttpActionResult> UploadFile()
 {
     try
     {
         //得到上传的文件
         HttpPostedFile file = HttpContext.Current.Request.Files[0];
         //转DataTable
         DataTable dt = NopiHelper.ExcelToTable(file.InputStream, 0, 0, Path.GetExtension(file.FileName));
         //通过DataTable转实体
         //List<ModelDemo> list = NopiHelper.Mapper<ModelDemo>(dt);
         return(await Task.FromResult(Ok(new { errcode = 0, data = "成功" })));
     }
     catch (Exception ex)
     {
         return(await Task.FromResult(Ok(new { errcode = 0, data = ex.Message })));
     }
 }
Exemplo n.º 2
0
        public async Task <HttpResponseMessage> Test()
        {
            // DataTable dt = NopiHelper.Mapper<TestA>();
            DataTable dt = new DataTable();
            //导出的名字,需要带扩展名 *.xls   *.xlsx
            string fileName = "测试.xls";
            Stream stream   = NopiHelper.StreamFromDataTable(dt, fileName);
            //设置状态
            HttpResponseMessage httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK);

            //内容
            httpResponseMessage.Content = new StreamContent(stream);
            //类型
            httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.ms-excel");
            //响应内容的值,附件形式
            httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = HttpUtility.UrlEncode(Path.GetFileName(fileName))
            };
            return(await Task.FromResult(httpResponseMessage));
        }