页面 attr方法可以更改属性
function fileDownLoad(){
if(window.confirm("确定要下载吗?")){
$("#showColumnList").attr("action","cultivaterFileDownLoad.do");
$('#showColumnList').submit();
}
}
action
public void cultivaterFileDownLoad() throws Exception {
Map<String, String> fileMap = null;
Admin admin = (Admin) session().getAttribute(IConstants.SESSION_ADMIN);
String order_code = request().getParameter("order_code");
String userName = request().getParameter("username");
String shopName = request().getParameter("shopName");
String mobilePhone = request().getParameter("mobilePhone");
String startTime = request().getParameter("startTime");
String endTime = request().getParameter("endTime");
long order_state = Convert.strToLong(request().getParameter("order_state"), -1);
String path = session().getServletContext().getRealPath("/");
// 文件下载
fileMap = orderService.cultivaterFileDownLoad(order_code,startTime,endTime,userName,shopName,order_state,admin.getUserName(),path);
String finalPath = path + "download/" + fileMap.get("filePath");
orderService.downLoadFile(response(),finalPath);
}
service
文件下载
public Map<String, String> cultivaterFileDownLoad(String order_code, String endTime, String startTime,String username,
String shopName, long order_state, String userName2, String path) throws SQLException {
Map<String, String> fileMap = null;
Connection conn = null;
List<Map<String, Object>> resultList = null;
String filename = null;
String folder = "/download";
String dirpath = null;
String filepath = null;
File file = null;
try{
conn = MySQL.getConnection();
File dir = new File(path + folder);
if(!dir.exists()){
dir.mkdir();
}
dirpath = dir.getPath();
file = new File(dir.getPath() + "/" + userName2+UUID.randomUUID() + ".xls");
if(!file.exists()){
file.createNewFile();
filename = file.getName();
}
resultList = orderDao.cultivaterFileDownLoad(conn,order_code,startTime,endTime,username,shopName,order_state,path);
createExeclFile(resultList, file.getAbsolutePath());
}catch(Exception e){
e.printStackTrace();
conn.rollback();
}finally{
if(null!=conn){
conn.close();
}
}
filepath = dirpath+"/"+filename;
fileMap = new HashMap<String,String>();
//获取更改后文件名
String changename = filename+".rar";
fileMap.put("filePath",changename);
fileMap.put("folder",folder);
File2ZIP.fileToZip(filepath, filepath.substring(0, filepath.lastIndexOf("/")),
filepath.substring(filepath.lastIndexOf("/"), filepath.length()) + ".rar");
if(file.exists()){
file.delete();
}
return fileMap;
}
DAO //查询订单数据
@SuppressWarnings("null")
public List<Map<String, Object>> cultivaterFileDownLoad(Connection conn, String order_code,String endTime, String startTime,
String username, String shopName, long order_state, String path) throws SQLException, DataException {
List<Map<String, Object>> resultList = null;
StringBuffer sql = new StringBuffer("");
sql.append("select a.order_code,a.order_time,a.order_price,a.order_state,b.username,b.mobilePhone,c.shop_name from t_order a left join t_user b on a.user_id=b.id left join t_shop_info c on a.shop_id=c.shop_id WHERE 1 = 1
");
if(order_state !=-1){
sql.append(" and a.order_state = " + order_state);
}
if(null!=order_code&&!"".equals(order_code.trim())){
sql.append(" and a.order_code = '" + order_code+"'");
}
if (startTime!=null && !startTime.equals("") &&!startTime.equals("-1") && endTime!=null && !endTime.equals("") && !endTime.equals("-1")) {
sql.append(" and a.order_time BETWEEN '"+startTime + "' AND '"+endTime+"'");
}
if(null!=username&&!"".equals(username.trim())){
sql.append(" and b.username = '" + username+"'");
}
if(null!=shopName&&!"".equals(shopName.trim())){
sql.append(" and c.shop_name = '" + shopName+"'");
}
DataSet ds = MySQL.executeQuery(conn, sql.toString());
ds.tables.get(0).rows.genRowsMap();
resultList = ds.tables.get(0).rows.rowsMap;
return resultList;
}
//service 创建模板
private void createExeclFile( List<Map<String, Object>> resultList, String filePath){
String[] title = { "编号", "订单编号", "订单时间", "订单金额", "用户名称","店铺名称","手机号","订单状态"};
try {
WritableWorkbook wwb;
OutputStream os = new FileOutputStream(filePath);
wwb = Workbook.createWorkbook(os);
WritableFont font1 = new WritableFont(WritableFont.ARIAL,12,WritableFont.NO_BOLD);
WritableCellFormat wc = new WritableCellFormat(font1);
wc.setAlignment(Alignment.CENTRE);
wc.setBorder(Border.ALL, BorderLineStyle.THIN);
WritableSheet sheet = wwb.createSheet("订单信息", 0);
sheet.setColumnView(1, 20);
sheet.setColumnView(2, 70);
sheet.setColumnView(3, 40);
sheet.setColumnView(4, 40);
sheet.setColumnView(5, 40);
sheet.setColumnView(6, 40);
sheet.setColumnView(7, 40);
sheet.setColumnView(8, 40);
Label label;
for (int i = 0; i < title.length; i++) {
label = new Label(i, 0, title[i], wc);
sheet.addCell(label);
}
jxl.write.Number number;
for (int j = 0; j < resultList.size(); j++) {
number = new jxl.write.Number(0, j + 1, j + 1, wc);
sheet.addCell(number);
label = new Label(1, j + 1, (String) resultList.get(j).get("order_code"), wc);
sheet.addCell(label);
label = new Label(2, j + 1, (String) resultList.get(j).get("order_time").toString(), wc);
sheet.addCell(label);
label = new Label(3, j + 1, (String) resultList.get(j).get("order_price").toString(), wc);
sheet.addCell(label);
label = new Label(4, j + 1, (String) resultList.get(j).get("username"), wc);
sheet.addCell(label);
label = new Label(5, j + 1, (String) resultList.get(j).get("shop_name"), wc);
sheet.addCell(label);
label = new Label(6, j + 1, (String) resultList.get(j).get("mobilePhone"), wc);
sheet.addCell(label);
number = new jxl.write.Number(7, j + 1, (Integer) resultList.get(j).get("order_state"), wc);
if(number.getValue()==0.0){
sheet.addCell(new Label(7, j + 1, "待付款", wc));
}else if(number.getValue()==1.0){
sheet.addCell(new Label(7, j + 1, "已付款", wc));
}else if(number.getValue()==2.0){
sheet.addCell(new Label(7, j + 1, "已发货", wc));
}else if(number.getValue()==3.0){
sheet.addCell(new Label(7, j + 1, "已完成", wc));
}else if(number.getValue()==4.0){
sheet.addCell(new Label(7, j + 1, "已取消", wc));
}
// sheet.addCell(number);
}
wwb.write();
wwb.close();
} catch (Exception e) {
e.printStackTrace();
}
}
service 下载格式
public boolean downLoadFile(HttpServletResponse response, String finalPath) {
boolean flag = true;
FileInputStream fileInputStream = null;
ServletOutputStream os = null;
try{
String fileName = finalPath.substring(finalPath.lastIndexOf("/")+1,finalPath.length());
fileInputStream = new FileInputStream(finalPath);
response.setCharacterEncoding("UTF-8");
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition","attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
int i;
os = response.getOutputStream();
while ((i = fileInputStream.read()) != -1) {
os.write(i);
}
os.flush();
}catch(Exception e){
flag = false ;
e.printStackTrace();
}finally{
try {
fileInputStream.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return flag;
}