Travailler avec GridJs côté serveur
Travailler avec GridJs côté serveur
1. ajouter un service dans ConfigureServices dans startup.cs
services.AddScoped<IGridJsService, GridJsService>();
2. Définir le chemin pour stocker les fichiers de cache
Vous pouvez choisir parmi les méthodes suivantes :
Option 1 : Définir GridJsOptions dans ConfigureServices dans Startup.cs
services.Configure<GridJsOptions>(options =>
{
options.FileCacheDirectory = TestConfig.TempDir;
});
Option 2 : Définir directement la propriété statique
Config.FileCacheDirectory = TestConfig.TempDir;
Option 3 : Définir votre propre règle de stockage du cache en implémentant GridCacheForStream
Pour le stockage de fichiers local, voici un exemple :
public class LocalFileCache : GridCacheForStream | |
{ | |
public LocalFileCache() | |
{ | |
string streampath = Path.Combine(Config.FileCacheDirectory, "streamcache"); | |
if (!Directory.Exists(streampath)) | |
{ | |
//create cache directory if not exists | |
Directory.CreateDirectory(streampath); | |
} | |
} | |
/// <summary> | |
/// Implement this method to savecache,save the stream to the cache object with the key id. | |
/// </summary> | |
/// <param name="s">the source stream </param> | |
/// <param name="uid">he key id.</param> | |
public override void SaveStream(Stream s, String uid) | |
{ | |
String filepath = Path.Combine(Config.FileCacheDirectory + Path.DirectorySeparatorChar + "streamcache", uid.Replace('/', '.')); | |
using (FileStream fs = new FileStream(filepath, FileMode.Create)) | |
{ | |
s.Position = 0; | |
s.CopyTo(fs); | |
} | |
} | |
/// <summary> | |
/// Implement this method to loadcache with the key uid,return the stream from the cache object. | |
/// </summary> | |
/// <param name="uid">the key id</param> | |
/// <returns>the stream from the cache</returns> | |
public override Stream LoadStream(String uid) | |
{ | |
String filepath = Path.Combine(Config.FileCacheDirectory + Path.DirectorySeparatorChar + "streamcache", uid.Replace('/', '.')); | |
FileStream fs = new FileStream(filepath, FileMode.Open); | |
return fs; | |
} | |
/// <summary> | |
/// implement the url in action controller to get the file | |
/// </summary> | |
/// <param name="uid">the key id</param> | |
/// <returns></returns> | |
public override String GetFileUrl(string uid) | |
{ | |
return "/GridJs2/GetFile?id=" + uid; | |
} | |
} |
Pour le stockage côté serveur, nous fournissons également un exemple. Veuillez vérifier :
https://github.com/aspose-cells/Aspose.Cells-for-.NET/blob/master/Examples_GridJs/Models/AwsCache.cs
Pour plus de détails sur le stockage, veuillez consulter ce guide
3. Implémenter les actions du contrôleur
1. Créer un contrôleur qui hérite de GridJsControllerBase
public class GridJs2Controller : GridJsControllerBase
2. Obtenir JSON dans l’action
Il y a deux façons d’obtenir des données JSON dans votre action :
Option 1 : Utiliser GridJsWorkbook
GridJsWorkbook wbj = new GridJsWorkbook();
Workbook wb = new Workbook(fullFilePath);
wbj.ImportExcelFile(wb);
String ret =wbj.ExportToJson(fileName);
Option 2 : Utiliser IGridJsService dans GridJsControllerBase
String uid= GridJsWorkbook.GetUidForFile(fileName)
StringBuilder ret= _gridJsService.DetailFileJsonWithUid(fullFilePath, uid);
Pour plus d’informations, vous pouvez consulter l’exemple ici : https://github.com/aspose-cells/Aspose.Cells-for-.NET/tree/master/Examples_GridJs