html:
<a href="javascript:;" type='button' οnclick="importExcel()">数据导出</a>
js:
function importExcel(){
var url = '';
var form = $("<form></form>").attr("action", url).attr("method", "post");
form.append($("<input></input>").attr("type", "hidden").attr("name", "duration").attr("value", test));
form.appendTo('body').submit().remove();
}
java:
/**
* 输出文件
*
* @param response
* @throws IOException
*/
protected static void print(HttpServletResponse response, HSSFWorkbook workbook) throws IOException {
String fileNameGb = new String(fileName.getBytes("gb2312"), "ISO8859-1");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
workbook.setSelectedTab(0);
workbook.write(baos);
response.setContentType("application/x-download;charset=utf-8");
response.addHeader("Content-Disposition", "attachment;filename=" + fileNameGb + ".xls");
OutputStream os = response.getOutputStream();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
byte[] b = new byte[1024];
while ((bais.read(b)) > 0) {
os.write(b);
}
bais.close();
os.flush();
os.close();
}
拼接excel数据就不贴出来了。