与GridJs服务器端配合工作

与GridJs服务器端配合工作

1. 在startup.cs的ConfigureServices中添加服务

   services.AddScoped<IGridJsService, GridJsService>();

2. 设置缓存文件存储路径

你可以选择以下任意方法:

选项1:在Startup.cs的ConfigureServices中设置GridJsOptions

   services.Configure<GridJsOptions>(options =>
	{
		options.FileCacheDirectory = TestConfig.TempDir;
	});

选项2:直接设置静态属性

   Config.FileCacheDirectory = TestConfig.TempDir;

选项3:通过实现GridCacheForStream定义你自己的缓存存储规则

对于本地文件存储,这里有一个示例:

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

对于服务器端存储,我们还提供了示例。请查看:

https://github.com/aspose-cells/Aspose.Cells-for-.NET/blob/master/Examples_GridJs/Models/AwsCache.cs

关于存储的更多详细信息,请参考指南

3. 实现控制器动作

1. 创建一个扩展自GridJsControllerBase的控制器

public class GridJs2Controller : GridJsControllerBase

2. 在动作中获取JSON

在你的操作中获取JSON数据有两种方法:

选项1:使用GridJsWorkbook

GridJsWorkbook wbj = new GridJsWorkbook();
Workbook wb = new Workbook(fullFilePath); 
wbj.ImportExcelFile(wb);
String ret =wbj.ExportToJson(fileName);

选项2:在GridJsControllerBase中使用IGridJsService

 String uid= GridJsWorkbook.GetUidForFile(fileName)
 StringBuilder ret= _gridJsService.DetailFileJsonWithUid(fullFilePath, uid);

有关详细信息,请查看此处的示例: https://github.com/aspose-cells/Aspose.Cells-for-.NET/tree/master/Examples_GridJs