public static ExportFileViewModel ExportToCSVFile <T>(this List <T> objectList, string fileName) { ExportFileViewModel exportFileViewModel = new ExportFileViewModel(); string excelFormat = GetToCSVFormatValue(objectList); exportFileViewModel = ExportToCSVFileByFileStream(excelFormat, fileName); return(exportFileViewModel); }
private static ExportFileViewModel ExportToCSVFileByFileStream(string excelFormat, string fileName) { ExportFileViewModel exportFileViewModel = new ExportFileViewModel(); var memoryStream = new MemoryStream(); using (var streamWriter = new StreamWriter(stream: memoryStream, encoding: new UTF8Encoding(true))) { streamWriter.Write(excelFormat); } string fileNameWithExtension = $"Company-{fileName}-{DateTime.UtcNow.ToFormatedDateTimeForFileString()}.csv".ToLower(); exportFileViewModel.FileStream = memoryStream; exportFileViewModel.ContentType = "text/csv"; exportFileViewModel.FileName = fileNameWithExtension; return(exportFileViewModel); }
public static ExportFileViewModel ExportToCSVFileByCsvHelper <T>(this List <T> objectList, string fileName) { ExportFileViewModel exportFileViewModel = new ExportFileViewModel(); var csvConfiguration = new CsvConfiguration(new System.Globalization.CultureInfo("en-US")); var memoryStream = new MemoryStream(); using (var streamWriter = new StreamWriter(stream: memoryStream, encoding: new UTF8Encoding(true))) { using (var csvWriter = new CsvWriter(streamWriter, csvConfiguration)) { csvWriter.WriteRecords(objectList); } } string fileNameWithExtension = $"Company-{fileName}-{DateTime.UtcNow.ToFormatedDateTimeForFileString()}.csv".ToLower(); exportFileViewModel.FileStream = memoryStream; exportFileViewModel.ContentType = "text/csv"; exportFileViewModel.FileName = fileNameWithExtension; return(exportFileViewModel); }
public ActionResult ExportFileShape(ExportFileViewModel exportFile) { try { CheckFolder(); string IdGui = Guid.NewGuid().ToString(); //string filePath = Path.Combine(_hostingEnvironment.WebRootPath + "\\Document", "ThuaDat_" + IdGui + ".json"); //string filePath = Path.Combine(Server.MapPath("~/Document"), "ThuaDat_" + IdGui + ".json"); //System.IO.File.WriteAllText(filePath, exportFile.shapeJson); bool check = false; if (exportFile.category == "SHP") { //ShapeJsonViewModel shapeJsonViewModel = JsonConvert.DeserializeObject<ShapeJsonViewModel>(exportFile.shapeJson); //if (shapeJsonViewModel == null) //{ // return Json(new { code = "fail", result = "Lỗi không thể convert ShapeJson" }, JsonRequestBehavior.AllowGet); //} ////bool createshapefilebool = ExportShapefileDataWriter(shapeJsonViewModel); //if (!createshapefilebool) //{ // return Json(new { code = "fail", result = "Lỗi không thể tạo shapefile" }, JsonRequestBehavior.AllowGet); //} string namefile = "ThuaDat_" + IdGui + ".shp"; string filePathShape = Path.Combine(Server.MapPath("~/Document/ShapeFile"), "ThuaDat_" + IdGui + ".shp"); string filePathZip = Path.Combine(Server.MapPath("~/Document/ShapeFile"), "ThuaDat_" + IdGui + ".zip"); //check = gdalUtilities.convertJsonToShapeFile(filePath, filePathShape); check = gdalUtilities.ConvertJsonToShapeFileNew(exportFile.shapeJson, filePathShape, namefile); if (check) { string filesPathName = filePathShape.Substring(0, filePathShape.Length - 4); if (Directory.Exists(filesPathName)) { Directory.Delete(filesPathName, true); } return(Json(new { code = "ok", result = "ThuaDat_" + IdGui + ".zip" }, JsonRequestBehavior.AllowGet)); } } if (exportFile.category == "KML") { string filePathKML = Path.Combine(Server.MapPath("~/Document/KML"), "ThuaDat_" + IdGui + ".KML"); dynamic json = JsonConvert.DeserializeObject(exportFile.shapeJson); check = gdalUtilities.ConvertJsonToKML(json, filePathKML); if (check) { if (System.IO.File.Exists(filePathKML)) { System.IO.File.Delete(filePathKML); } } return(Json(new { code = "ok", result = "ThuaDat_" + IdGui + ".zip" }, JsonRequestBehavior.AllowGet)); } return(Json(true, JsonRequestBehavior.AllowGet)); } catch (Exception ex) { return(Json(new { code = "fail", result = ex.Message.ToString() }, JsonRequestBehavior.AllowGet)); throw; } }