您的位置:首页 > 编程语言 > Java开发

数据迁出

2015-12-29 10:56 465 查看
1.jsp

<button type="button" id="exportBtn1" class="btn btn-mini btn-primary" rel="tooltip" data-placement="top" data-original-title="导出模版" data-html="true">
<i class="fa fa-download">导出模版</i>
</button>


2.js

//导出.xsl按钮
$("#exportBtn1").click(function(){
$.SmartMessageBox({
title:"系统提示:",
content:"<br><br>正在下载数据,导出完毕后点击确认关闭",
buttons:"[确认]"
})
location.href = "../../xxx/exportFile.action";
})


3.java

//导出
public String exportFile() throws Exception{
//导出数据
//String filename = assetsDetectService.exportData(planId,type);

//导出模版
String filename = flowDAO.exportTemplate();
downloadFile(filename, false);
return NONE;
}

/**
* 下载文件
*
* @param del
*                ,是否需要删除,如果是true那么下载之后将删除文件
* @throws IOException
*/
protected void downloadFile(String path, boolean del) throws IOException {
HttpServletResponse response = getResponse();
response.setCharacterEncoding("gb2312");

response.reset();//
response.setContentType("application/x-download");
int last1 = path.lastIndexOf("\\");
int last2 = path.lastIndexOf("/");
String filedisplay = path.substring((last1 > last2 ? last1 : last2) + 1);
filedisplay = StrConvert.convert(filedisplay, "gbk", "iso-8859-1");
response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);

OutputStream outp = null;
FileInputStream in = null;
java.io.File fp = new java.io.File(path);
try {
outp = response.getOutputStream();
in = new FileInputStream(fp);

byte[] b = new byte[1024];
int i = 0;

while ((i = in.read(b)) > 0) {
// Debug.println("1024个字节溜出");
outp.write(b, 0, i);
}
outp.flush();
in.close();
outp.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
in.close();
in = null;
}
if (outp != null) {
outp.close();
outp = null;
}
if (del) {
fp.delete();
}
}
}
实现层

//导出模版
public String exportTemplate(){
File file = new File(this.path);
if(!file.exists())
file.mkdirs();

String filename= path + (new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss")).format(new Date())+".xls";
try
{
WritableWorkbook wb = Workbook.createWorkbook(new File(filename));
WritableSheet sheet = wb.createSheet("sheet1",0);
//为(0,0),(0,1)单元格写入内容
String tableHead = "x:";
String tableHead2 = "xx";
sheet.addCell(new Label(0,0,tableHead));
sheet.addCell(new Label(1,0,tableHead2));

wb.write();
wb.close();
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
return filename;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java