js:
var spread = null;
$(function () {
spread = new GC.Spread.Sheets.Workbook(document.getElementById(‘ss’), {sheetCount: 1});
ImportFile(); //待调用的函数
});
function ImportFile() {
var excelIO = new GC.Spread.Excel.IO();
//var excelUrl = $("#aexecl").attr("model");
var excelUrl = "${ctx}/a/aexecl/download?id=" + $("#aexecl").attr("model");
var oReq = new XMLHttpRequest();
oReq.open('get', excelUrl, true);
oReq.responseType = 'blob';
oReq.onload = function () {
var blob = oReq.response;
excelIO.open(blob, LoadSpread, function (message) {
console.log(message);
});
};
oReq.send(null);
}
function LoadSpread(json) {
var jsonData = json;
spread.fromJSON(jsonData);
}
**注意:需要先导入spreadjs.js文件**
html
java:
@RequestMapping(value = “download”)
public void download(String id, HttpServletResponse response) throws Exception {
try {
String psth = “”;
// psth 是指欲下载的文件的路径。
File file = new File(psth);
// 取得文件名。
String filename = file.getName();
// 取得文件的后缀名。
// String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(psth));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
response.addHeader(“Content-Disposition”, “attachment;filename=” + new String(filename.getBytes()));
response.addHeader(“Content-Length”, “” + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType(“application/octet-stream”);
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
此方法是下载文件的方法,可用于下载当前execl文件;