附上demo。感激不尽。
<script type="text/javascript">
function exportExcel(){
Ext.MessageBox.confirm("提示","您是否要导出Excel表格吗?",function(btnId){
if(btnId == 'yes'){
Ext.MessageBox.wait("正在导出Excel表格,请稍等...", "提示"); Ext.Ajax.request({
url:'accessrecord_exportExcel',
method:'post',
success:function(response,options){ Ext.Msg.alert('提示','成功导出Excel表格!');
},
failure:function(response,options){
Ext.Msg.alert('提示','发生错误,未能导出Excel表格!');
}
});
}
});
}
</script>
需要导入 jxl.jar 这个jar包
/**
* 导出excel
* request和response :无需解释,fileName:文件名,retList:数据list里面存的是map,titleList:标题就是首列文字啊什么的,columnList:根据这个list去map中取值也就是说这个是map里面KEY的一个集合
*/
public static void print(HttpServletRequest request,
HttpServletResponse response, String fileName,
List<Map<String, Object>> retList, List titleList, List columnList)
throws Exception {
final String userAgent = request.getHeader("USER-AGENT");
if (userAgent.contains("Mozilla")) {// google,火狐浏览器
fileName = new String(fileName.getBytes(), "ISO8859-1");
} else {
fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
}
response.addHeader("Content-Disposition", "attachment;filename="
+ fileName);
response.setContentType("application/octet-stream");
OutputStream os = response.getOutputStream();
WritableWorkbook workbook = Workbook.createWorkbook(os);
// 创建新的一页
WritableSheet sheet = workbook.createSheet("First Sheet", 0);
// 获取内容
for (int i = 0; i < titleList.size(); i++) {
Label label = new Label(i, 0, (String) titleList.get(i));
sheet.addCell(label);
}
for (int i = 0; i < retList.size(); i++) {
Map map = retList.get(i);
for (int j = 0; j < columnList.size(); j++) {
Object cellText = map.get(columnList.get(j));
if (cellText == null) {
cellText = "";
}
Label label = new Label(j, i + 1, cellText.toString());
sheet.addCell(label);
}
}
workbook.write();
workbook.close();
os.close();
}
这是我在项目里面找的一个UTIL ,随便写了点注释,有看不懂的可以联系我 Q593921602
http://blog.csdn.net/tffboke/article/details/51785500
给你个例子
@SuppressWarnings({ "deprecation", "unchecked" })
@RequestMapping("export-newStudentList")
@ResponseBody
protected String exportNewCommer(EmployeeQuery query,
HSSFWorkbook workbook, HttpServletRequest request,
HttpServletResponse response) throws Exception {
try {
response.reset();
// 获得国际化语言
RequestContext requestContext = new RequestContext(request);
String CourseCompany = requestContext.getMessage("manage-student");
response.setContentType("APPLICATION/vnd.ms-excel;charset=UTF-8");
// 注意,如果去掉下面一行代码中的attachment; 那么也会使IE自动打开文件。
response.setHeader(
"Content-Disposition",
"attachment; filename="
+ java.net.URLEncoder.encode(
DateUtil.getExportDate() + ".xls", "UTF-8"));
OutputStream os = response.getOutputStream();// new
query.setNeedFilte(true);
query.setJoinEndDate(DateUtil.toStringWidthoutHHMMSS(DateUtil
.addDayDate(new Date(), -7)));
EmployeeOrganizationRefQuery equery = new EmployeeOrganizationRefQuery();
equery.setEmployeeCode(CurrentUserUtil.getCurrentUserName());
List eomlist = employeeOrganizationRefService
.getEmployeeOrganizationRefList(equery);
// EmployeeOrganizationRefModel
// eom=employeeOrganizationRefService.getByEmployeeCode(CurrentUserUtil.getCurrentUserName());
if (eomlist.size() == 1) {
query.setStoreNo(eomlist.get(0).getUnitId());
}
// if (eom!=null) {
// query.setStoreNo(eom.getUnitId());
// }
List list = employeeService.getfreshList(query);
// 产生Excel表头
HSSFSheet sheet = workbook.createSheet(DateUtil.getExportDate()
+ CourseCompany);
sheet.setDefaultColumnWidth((short) 17);
// 创建属于上面Sheet的Row,参数0可以是0~65535之间的任何一个,
HSSFRow header = sheet.createRow(0); // 第0行
HSSFCellStyle style = workbook.createCellStyle();
// 设置样式
style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
String employeeCode = requestContext.getMessage("employeeCode");
String employeeName = requestContext.getMessage("employeeName");
String division = requestContext.getMessage("division");
String section = requestContext.getMessage("section");
String joinDate = requestContext.getMessage("joinDate");
// 表头名称数组
String[] headerArr = new String[] { employeeCode, employeeName,
division, section, joinDate };
// 产生标题列
HSSFCell cell;
for (int i = 0; i < headerArr.length; i++) {
cell = header.createCell((short) i);
cell.setCellStyle(style);
cell.setCellValue(headerArr[i]);
}
// 迭代数据
if (list != null && list.size() > 0) {
int rowNum = 1;
for (EmployeeModel history : list) {
HSSFRow row = sheet.createRow(rowNum++);
row.createCell((short) 0).setCellValue(
history.getEmployeeCode());
row.createCell((short) 1).setCellValue(history.getName());
row.createCell((short) 2).setCellValue(
history.getDivisiName());
row.createCell((short) 3).setCellValue(
history.getSectionName());
if (history.getJoinDate() != null) {
row.createCell((short) 4).setCellValue(
DateUtil.toString(history.getJoinDate()));
} else {
row.createCell((short) 4).setCellValue("");
}
}
}
workbook.write(os);
os.close();
} catch (IOException e) {
e.printStackTrace();
return "false";
}
return "true";
}
http://blog.csdn.net/fiangasdre/article/details/51741580 看看我这篇文章 希望对你有帮助
标识 | 内容 | 创建时间 |
---|---|---|
1 | excel导出01 | 2015-07-22 |
2 | excel导出02 | 2015-07-22 |
onclick="javascript:method1('targetTable')" />
就是这个链接自己看看吧 http://download.csdn.net/download/zsbiubiu/8923033