例如:
$("#export").click(function() {
var m = sensitiveWordsLightGrid.grid.getSelections();
if (m.length == 0) {
Ext.Msg.confirm("提示", "确定要全部导出吗?", function(btn, path) {
$.ajax({
type: "POST",
url: getSitePath() + "/xxx/xxx_exportAllData.action",
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
success: function(response) {
window.location.href= getSitePath()+"/xxx/xxx_download.action";
}
});
}.createDelegate(this));
} else {
Ext.Msg.confirm("提示", "确定执行此项操作吗?", function(btn, path) {
if (btn == 'yes') {
var d = sensitiveWordsLightGrid.grid.getSelections();
var f = '?data=',
temp = "[";
var g = d.length;
if (sensitiveWordsLightGrid.gridconfig.removeCondition) {
for (var i = 0; i < g; i++) {
temp += Ext.encode(Ext.lbj.FormUtils.serialSameFields(sensitiveWordsLightGrid.gridconfig.metaData, sensitiveWordsLightGrid.gridconfig.removeCondition, d[i]));
temp += ","
}
f += temp.slice(0, temp.length - 1) + "]";
$.ajax({
type: "POST",
url: getSitePath() + "/xxx/xxxx_exportSensitiveWords.action" + f,
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
success: function(response) {
window.location.href= getSitePath()+"/xxx/xxxx_download.action";
}
});
}
}
}.createDelegate(this));
}
});
/**
* 获取临时目录存放临时文件
*/
public String createTemporaryFile() {
String savePath = ServletActionContext.getServletContext().getRealPath("/file/xxx");
return savePath;
}
/**
* 删除临时文件
*/
public void deleteTemporaryFile() {
String savePath = ServletActionContext.getServletContext().getRealPath("/file/xxx/delete.xls");
File fileDelete = new File(savePath);
if (fileDelete.exists()) {
fileDelete.delete();
}
}
/**
* 导出所有的数据
*
* @throws IOException
*/
public void exportAllData() throws IOException {
List<xxxx> domains = zzzz.findByWhere(null);
// 创建工作薄
HSSFWorkbook workbook = new HSSFWorkbook();
// 创建工作表
HSSFSheet sheet = workbook.createSheet("sheet1");
HSSFRow rows0 = sheet.createRow(0);
// 创建表头
rows0.createCell(0).setCellValue("xxx");
rows0.createCell(1).setCellValue("xxx");
rows0.createCell(2).setCellValue("xxxx");
for (int i = 0; i < domains.size(); i++) {
HSSFRow rows = sheet.createRow(i + 1);
rows.createCell(0).setCellValue("xxxx");
rows.createCell(1).setCellValue("xxxx");
rows.createCell(2).setCellValue("xxxx");
}
String savePath = createTemporaryFile();
File xlsFile = new File(savePath + "/delete.xls");
FileOutputStream xlsStream = new FileOutputStream(xlsFile);
workbook.write(xlsStream);
xlsStream.close();
this.send(SUCCESS);
}
/**
* 批量导出部分数据
*
* @throws IOException
*/
public void exportSensitiveWords() throws IOException {
List<SensitiveWordsVO> voList = JsonUtils.json2List(data, SensitiveWordsVO.class);
// 创建工作薄
HSSFWorkbook workbook = new HSSFWorkbook();
// 创建工作表
HSSFSheet sheet = workbook.createSheet("sheet1");
HSSFRow rows0 = sheet.createRow(0);
// 创建表头
rows0.createCell(0).setCellValue("xxxx");
rows0.createCell(1).setCellValue("xxxx");
rows0.createCell(2).setCellValue("xxxx");
for (int i = 0; i < voList.size(); i++) {
HSSFRow rows = sheet.createRow(i + 1);
rows.createCell(0).setCellValue("xxxx");
rows.createCell(1).setCellValue("xxxx");
rows.createCell(2).setCellValue("xxxx");
}
String savePath = createTemporaryFile();
File xlsFile = new File(savePath + "/delete.xls");
FileOutputStream xlsStream = new FileOutputStream(xlsFile);
workbook.write(xlsStream);
xlsStream.close();
this.send(SUCCESS);
}
/**
* 下载的公共部分
*
* @param savePath
* @param fileName
*/
public void publicDownload(String savePath, String fileName) {
try {
// 要下载的文件
InputStream is = new FileInputStream(savePath + "/" + fileName);
// 下载到客户端
HttpServletResponse response = ServletActionContext.getResponse();
OutputStream os = response.getOutputStream();
// 弹出下载的框filename:提示用户下载的文件名
response.addHeader("content-disposition", "attachment;filename=" + DateUtils.date2String(new Date(), DateUtils.FULL_DATE_FORMAT_NO_SEPARATOR) + ".xls");
byte[] b = new byte[1024];
int size = is.read(b);
while (size > 0) {
os.write(b, 0, size);
size = is.read(b);
}
is.close();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}