与GridJs服务器端配合工作
Contents
[
Hide
]
与GridJs服务器端配合工作
0. 在配置中设置正确的文件夹路径
Config.setFileCacheDirectory
用于工作簿缓存文件(必需)。
Config.setPictureCacheDirectory
用于工作簿中的图像文件缓存(可选,默认值为文件缓存目录中的 _piccache)。
有关存储详细信息,请查看此 指南
1. 实现 GridCacheForStream
对于本地文件存储,这里有一个示例:
public class LocalFileCache extends GridCacheForStream {
@Override
public void saveStream(InputStream s, String uid) {
// make sure the directory is exist
String filepath = Paths.get(Config.getFileCacheDirectory(), "streamcache", uid.replace('/', '.')).toString();
try (FileOutputStream fos = new FileOutputStream(filepath.toString())) {
s.reset(); // Equivalent to s.Position = 0 in C#
byte[] buffer = new byte[1024];
int length;
while ((length = s.read(buffer)) > 0) {
fos.write(buffer, 0, length);
}
fos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public InputStream loadStream(String uid) {
String filepath = Paths.get(Config.getFileCacheDirectory(), "streamcache", uid.replace('/', '.')).toString();
try {
return new FileInputStream(filepath);
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
}
@Override
public boolean isExisted(String uid) {
String filepath = Paths.get(Config.getFileCacheDirectory(), "streamcache", uid.replace('/', '.')).toString();
return Files.exists(Paths.get(filepath));
}
@Override
public String getFileUrl(String uid) {
return "/GridJs2/GetFileUseCacheStream?id=" + uid;
}
}
2. 从电子表格文件向响应流写入 json。
GridJsWorkbook wbj = new GridJsWorkbook();
try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(response.getOutputStream())) {
wbj.importExcelFile(filePath.toString());
wbj.jsonToStream(gzipOutputStream, filename);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
3. 从电子表格文件中获取图像/形状
//Gridjs will automatically zip all the images/shapes into a zip stream and store it in cache using the cache implemention.
InputStream inputStream = GridJsWorkbook.CacheImp.loadStream(fileid);
4. 在缓存中更新电子表格文件
GridJsWorkbook gwb = new GridJsWorkbook();
//p is the update json,uid is the unique id for the spreadsheet
String ret = gwb.updateCell(p, uid);
5. 将电子表格文件保存在缓存中
GridJsWorkbook wb = new GridJsWorkbook();
//p is the update json,uid is the unique id for the spreadsheet
wb.mergeExcelFileFromJson(uid, p);
wb.saveToCacheWithFileName(uid, filename,password);
有关详细信息,请查看此处的示例: https://github.com/aspose-cells/Aspose.Cells-for-java/tree/master/Examples_GridJs