.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; }

 

上一篇:使用VB2005开发外接程序Connect类中需要了解的一些事件


下一篇:c# – 如何接受函数引用作为参数?