效果:
jsp 页面 用的Bootstrap :
<li class="dropdown">
<a href="javascript:void(0);" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-file-excel-o"></i></a>
<ul class="dropdown-menu" role="menu">
<li><a href="javascript:void(0);" id="excel">excel导出 </a></li>
</ul>
</li>
表格是用的jquery 的dataTable ,js :
// 导出
$("#excel").click(function(){
// 查询条件:
var account = $('#account').val();
var riceCount = $('#riceCount').val();
var format = $('#format').val();
var regioin = $('#regioin').val();
var url = "/order/excelBegFlowInfo?account="+
account+"&riceCount="+riceCount+"&format="+format+"®ioin="+regioin;
if ($("#datatable tbody tr").text() == "表中数据为空") {
$.tooltip("没有可导出的数据", false);
return;
}
kk = url;
})
后台调用部分:
/**
* 导出
* @throws Exception
*/
@RequestMapping("/excelBegFlowInfo")
public void excelBegFlowInfo(HttpServletResponse response, BegFlowInfo info) throws Exception {
// 要导出的列表数据
List<BegFlowInfo> totalList = _begFlowOrderService.selectBegFlowInfo(info);
if (CollectionUtils.isEmpty(totalList)) {
return;
}
List<List<Object>> list = new ArrayList<List<Object>>();
for (BegFlowInfo begFlowInfo : totalList) {
List<Object> dataList = new ArrayList<Object>();
dataList.add(begFlowInfo.getId() == null ? "":begFlowInfo.getId());
dataList.add(begFlowInfo.getCreateDate() == null ? "":begFlowInfo.getCreateDate());
dataList.add(begFlowInfo.getAccount() == null ? "":begFlowInfo.getAccount());
dataList.add(begFlowInfo.getCarrier() == null ? "":begFlowInfo.getCarrier());
dataList.add(begFlowInfo.getRegioin() == null ? "":begFlowInfo.getRegioin());
dataList.add(begFlowInfo.getFormat() == null ? "" :begFlowInfo.getFormat());
dataList.add(begFlowInfo.getPrice() == null ? "":begFlowInfo.getPrice());
dataList.add(begFlowInfo.getType() == null ? "":begFlowInfo.getType().getName());
dataList.add(begFlowInfo.getRiceCount() == null ? "0":begFlowInfo.getRiceCount());
dataList.add(begFlowInfo.getIdentifyCode() == null ? "":begFlowInfo.getIdentifyCode());
dataList.add(begFlowInfo.getSmsCommand() == null ? "":begFlowInfo.getSmsCommand());
dataList.add(begFlowInfo.getUsableDate() == null ? "":begFlowInfo.getUsableDate());
dataList.add(giveCount == null ? "":giveCount);
list.add(dataList);
}
//表头
String[] headers = new String[]{"序号","xxx","xxx","XXX","XXX","XXX","XXX","XXX","XXX",
"XXX","XXXX","XXXX"};
// list 是要导出的表数据
HSSFWorkbook workbook = ExcelUtil.excelOut(headers, list);
try{
response.reset(); //清除response中的缓存信息
response.setHeader("Content-Disposition", "attachment; filename=" + new String("订单管理.csv".getBytes("gbk"), "iso8859-1"));
response.setContentType("application/vnd.ms-excel;");
response.setCharacterEncoding("utf-8");
workbook.write(response.getOutputStream());
} catch (Exception e){
e.printStackTrace();
}
}xxx","xxx","XXX","XXX","XXX","XXX","XXX","XXX",
"XXX","XXXX","XXXX"};
// list 是要导出的表数据
HSSFWorkbook workbook = ExcelUtil.excelOut(headers, list);
try{
response.reset(); //清除response中的缓存信息
response.setHeader("Content-Disposition", "attachment; filename=" + new String("订单管理.csv".getBytes("gbk"), "iso8859-1"));
response.setContentType("application/vnd.ms-excel;");
response.setCharacterEncoding("utf-8");
workbook.write(response.getOutputStream());
} catch (Exception e){
e.printStackTrace();
}
}
POI 方式-excle导出工具类实现:
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
* Excel工具
* @author jiangyu
* @date
*/
public class ExcelUtil {
public static HSSFWorkbook excelOut(String[] cloumName, List<List<Object>> list){
//声明一个工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
//生成一个表格
HSSFSheet sheet = workbook.createSheet();
//设置表格默认列宽度为20个字符
sheet.setDefaultColumnWidth(20);
//生成一个样式,用来设置标题样式
HSSFCellStyle style = workbook.createCellStyle();
// 表头居中
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//生成一个字体
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short) 12); // 字体高度
font.setFontName(" 黑体 "); // 字体
//把字体应用到当前的样式
style.setFont(font);
// 生成并设置另一个样式,用于设置内容样式
HSSFCellStyle style2 = workbook.createCellStyle();
style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 生成另一个字体
HSSFFont font2 = workbook.createFont();
font2.setFontName(" 黑体 "); // 字体
// 把字体应用到当前的样式
style2.setFont(font2);
HSSFRow row = sheet.createRow(0);
for(int i = 0; i < cloumName.length; i++){
//单元格
HSSFCell cellHead = row.createCell(i);
cellHead.setCellStyle(style);
HSSFRichTextString text = new HSSFRichTextString(cloumName[i]);
cellHead.setCellValue(text);
}
for (int i = 0; i < list.size(); i++){
row = sheet.createRow(i + 1);
List<Object> dataList = list.get(i);
for (int j = 0; j < dataList.size(); j++) {
// 表格内容样式设置
HSSFCell cellHead = row.createCell(j);
cellHead.setCellStyle(style2);
HSSFRichTextString text = new HSSFRichTextString(String.valueOf(dataList.get(j)));
// 为空
if(text == null || text.toString() == ""){
cellHead.setCellValue("");
}
// 整数,不为电话
else if(ValidateUtils.isInteger(String.valueOf(text))
&& !(StringUtils.startsWith(String.valueOf(text),"1")
&& String.valueOf(text).length() == 11)){
cellHead.setCellValue(Integer.parseInt(String.valueOf(text)));
}
// 有小数、或为电话
else if(ValidateUtils.isDouble(dataList.get(j).toString())){
cellHead.setCellValue(Double.parseDouble(String.valueOf(text)));
}
// 字符串
else{
cellHead.setCellValue(String.valueOf(text));
}
}
}
return workbook;
}
}