• .Net Core 导入导出数据


            

         使用 Magicodes.IE.Excel包
         官网地址:https://docs.xin-lai.com/2020/02/12/%E7%BB%84%E4%BB%B6/Magicodes.IE/Magicodes.IE%202.0%E5%8F%91%E5%B8%83/
    /// <summary> /// 导出数据 /// </summary> /// <returns></returns> public async Task<string> HSCodeExcel(HSCodeExcel excel) {string sql = "SELECT * FROM HSCode WHERE [Id] IN (" + excel.Id + ")";var da = await QuerySql<config_archives_HSCodeExcel>(sql); if (da.Count == 0) { throw new SjzyException("查询出现异常", ResponseEnum.NotAcceptable); } string date = "_" + DateTime.Now.ToString("yyyyMMddhhmmss"); var filePath = Path.Combine(Path.GetFullPath("../Excel"), excel.ExcelName + date + ".xlsx"); IExporter exporter = new ExcelExporter(); var result = await exporter.Export(filePath, da); return result.FileName; }
         使用 DotNetCore.NPOI包
    /// <summary> /// 导入数据 /// </summary> /// <param name="formCollection"></param> /// <returns></returns> public async Task<bool> HSCodeExcels(IFormFile formCollection,int UserId) { var file = formCollection.FileName; //文件名称 string filePhysicalPath = Path.GetFullPath("../Excels/"); //文件存储文件夹 if (!Directory.Exists(filePhysicalPath)) //判断上传文件夹是否存在,若不存在,则创建 { Directory.CreateDirectory(filePhysicalPath); //创建文件夹 } string date = DateTime.Now.ToString("yyyyMMddhhmmss")+ "_"; string path = filePhysicalPath + date + file; using (FileStream fs = System.IO.File.Create(path)) { formCollection.CopyTo(fs); fs.Flush(); } IWorkbook workbook = null; //新建IWorkbook对象 FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read); if (path.IndexOf(".xlsx") > 0) // 2007版本 { workbook = new XSSFWorkbook(fileStream); //xlsx数据读入workbook } else if (path.IndexOf(".xls") > 0) // 2003版本 { workbook = new HSSFWorkbook(fileStream); //xls数据读入workbook } ISheet sheet = workbook.GetSheetAt(0); //获取第一个工作表 IRow row;//新建当前工作表行数据 List<config_archives_HSCode> config_Archives = new List<config_archives_HSCode>(); for (int i = 0; i < sheet.LastRowNum; i++) //对工作表每一行 { row = sheet.GetRow(i+1); //row读入第i行数据 if (row != null) { config_archives_HSCode HSCode = new config_archives_HSCode(); HSCode.HSCode = row.GetCell(0).ToString(); HSCode.CName = row.GetCell(1).ToString(); HSCode.EName = row.GetCell(2).ToString(); HSCode.CreationTime = DateTime.Now; config_Archives.Add(HSCode); } } fileStream.Close(); workbook.Close(); var da = await Add(config_Archives); return da > 0; }
  • 相关阅读:
    动态规划之解决01背包问题 【转载】
    暴力搜索解0-1背包问题 【转载】
    回溯法之地图着色
    贪心算法之最小生成树
    贪心算法之最短路径
    HMM隐马尔科夫模型
    变分贝叶斯
    Leetcode好的微博
    HMM隐马尔科夫模型浅析
    向量的协方差矩阵浅析
  • 原文地址:https://www.cnblogs.com/s666/p/14894476.html
Copyright © 2020-2023  润新知