java下载excel文件

1.首先创建Excel文件类

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ExportContract {
public static void expotLog(List<TabContract> logList,HttpServletResponse response) {
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("合同管理列表");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int) 0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("合同编号");
cell.setCellStyle(style);
cell = row.createCell((short) 1);
cell.setCellValue("合同名称");
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setCellValue("对方名称");
cell.setCellStyle(style);
cell = row.createCell((short) 3);
cell.setCellValue("状态");
cell.setCellStyle(style);
cell = row.createCell((short) 4);
cell.setCellValue("类型");
cell = row.createCell((short) 5);
cell.setCellValue("金额");
cell.setCellStyle(style);
cell = row.createCell((short) 6);
cell.setCellValue("创建者");
cell.setCellStyle(style);
cell = row.createCell((short) 7);
cell.setCellValue("创建时间");
cell.setCellStyle(style);
// 第五步,写入实体数据 实际应用中这些数据从数据库得到,
List list = logList;
for (int i = 0; i < list.size(); i++) {
row = sheet.createRow((int) i + 1);
TabContract tabContract = (TabContract) list.get(i);
// 第四步,创建单元格,并设置值
row.createCell((short) 0).setCellValue(tabContract.getCode());
row.createCell((short) 1).setCellValue(tabContract.getName());
row.createCell((short) 2).setCellValue(tabContract.getOthername());
if("0".equals(tabContract.getContractStatus())){
row.createCell((short) 3).setCellValue("未提交");
}else if("1".equals(tabContract.getContractStatus())){
row.createCell((short) 3).setCellValue("审批中");
}else if("2".equals(tabContract.getContractStatus())){
row.createCell((short) 3).setCellValue("驳回");
}else if("3".equals(tabContract.getContractStatus())){
row.createCell((short) 3).setCellValue("通过");
}

row.createCell((short) 4).setCellValue(tabContract.getContracttype());
row.createCell((short) 5).setCellValue(tabContract.getMoney());
row.createCell((short) 6).setCellValue(tabContract.getCreateBy().getName());
row.createCell((short) 7).setCellValue(tabContract.getCreateDate());
}
// 第六步,下载excel  
        
        OutputStream out = null;  
        String uuid=UUID.randomUUID().toString();
        try {        
            out = response.getOutputStream();    
            response.setContentType("application/vnd.ms-excel; charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;fileName="+uuid+".xls");
            response.setCharacterEncoding("utf-8");
            wb.write(response.getOutputStream());
        HSSFWorkbook wb1 = new HSSFWorkbook();
            wb1.write(out);    
        } catch (Exception e) {    
            e.printStackTrace();    
        } finally {      
            try {       
                out.close();      
            } catch (IOException e) {      
                e.printStackTrace();    
            }      
        }
}

}

2.jsp页面使用checkbox复选框,将选中的数据传递到controller层

 if(confirm("确定要导出数据吗?")) {  
var obj = document.getElementsByName("checkbox");//选择所有name="interest"的对象,返回数组    
  if ($(":checkbox[name=checkbox]:checked").size() == 0) {
alert("请至少选择一条记录进行删除操作!");
return;
}
            var s='';//如果这样定义var s;变量s中会默认被赋个null值
            for(var i=0;i<obj.length;i++){
                 if(obj[i].checked) //取到对象数组后,我们来循环检测它是不是被选中
                 s+=obj[i].value+',';   //如果选中,将value添加到变量s中    
            }
            s=s.substring(0, s.length-1);
            self.location="${ctx}/contract/tabContract/exportcontract?json="+s;

}

此处不能使用ajax向后台传递数据,不然浏览器将无法弹出选择下载地址的窗口


猜你喜欢

转载自blog.csdn.net/angel_hhm/article/details/79555159