/**
* @instructions 如果目录不存在,首先创建
* @author jiyanle 2014-03-26
* @return
*/
public String createDirectory(String dirName){
File dir = new File(dirName);
if (dir.exists()) {
System.out.println("创建目录" + dirName + "失败,目标目录已经存在");
}else{
if (dir.mkdirs()) {
System.out.println("创建目录" + dirName + "成功!");
} else {
dirName = File.listRoots()[0]+"reports\\";
System.out.println("指定盘符不存在,无法创建,系统将其自动保存至" + dirName + "目录下!");
dir = new File(dirName);
if (dir.exists()) {
System.out.println("创建目录" + dirName + "失败,目标目录已经存在");
}else{
if (dir.mkdirs()) {
System.out.println("创建目录" + dirName + "成功!");
} else {
System.out.println("创建目录" + dirName + "失败!");
}
}
}
}
return dirName;
}
/**
* @instructions 导出Excel表格
* @author jiyanle 2014-03-26
* @return
*/
@SuppressWarnings("unchecked")
public String exportExcel(){
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
try{
//定义action返回ajax的xml文件的字符范围及返回类型
response.setContentType("text/xml; charset=utf-8");
response.setHeader("Cache-Contorl", "no-cache");
PrintWriter out = response.getWriter();
// 创建Excel的工作书册 Workbook,对应到一个excel文档
HSSFWorkbook workbook = new HSSFWorkbook();
// 创建Excel的工作sheet,对应到一个excel文档的tab
HSSFSheet sheet = workbook.createSheet("公共场所信息");
// 设置excel每列宽度
sheet.setColumnWidth(0, 50*256);
sheet.setColumnWidth(1, 15*256);
sheet.setColumnWidth(2, 30*256);
sheet.setColumnWidth(3, 15*256);
// 创建字体样式
HSSFFont font = workbook.createFont();
font.setFontName("Verdana");
font.setBoldweight((short) 100);
font.setFontHeight((short) 300);
// 创建单元格样式
HSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
style.setFont(font);// 设置字体
//---------需要导入的数据集----------
List resultList = (List) request.getSession().getAttribute("resultList");
//写入标题
HSSFRow row_1 = sheet.createRow(0);
HSSFCell cell_1 = row_1.createCell(0);
cell_1.setCellStyle(style);
cell_1.setCellValue("单位名称");
cell_1 = row_1.createCell(1);
cell_1.setCellStyle(style);
cell_1.setCellValue("姓名");
cell_1 = row_1.createCell(2);
cell_1.setCellStyle(style);
cell_1.setCellValue("许可证号");
cell_1 = row_1.createCell(3);
cell_1.setCellStyle(style);
cell_1.setCellValue("批准日期");
if (resultList != null && resultList.size() > 0) {
for (int i = 0; i < resultList.size(); i++) {
Ggcs ggcs = (Ggcs) resultList.get(i);
String ggcsDwmc = ggcs.getGgcsbjdDwmc();// 单位名称
String fddbr = ggcs.getGgcsbjdFzr();// 法定代表人姓名
String xkzh = ggcs.getGgcsbjdXkzhw() + "卫"
+ ggcs.getGgcsbjdXkzhz() + " 字["
+ ggcs.getGgcsbjdXkzhk() + "]第"
+ ggcs.getGgcsbjdXkzhh() + "号";
String pzrq = ggcs.getGgcsbjdXkzpzrq();// 批准日期
HSSFRow row_2 = sheet.createRow(i+1);
HSSFCell cell_2 = row_2.createCell(0);
cell_2.setCellValue(ggcsDwmc);
cell_2 = row_2.createCell(1);
cell_2.setCellValue(fddbr);
cell_2 = row_2.createCell(2);
cell_2.setCellValue(xkzh);
cell_2 = row_2.createCell(3);
cell_2.setCellValue(pzrq);
}
}
String dirName = "F:\\reports\\";//默认导出的路径
String fileName = "ggcsList.xls";//默认导出的文件名
dirName = this.createDirectory(dirName);
try {
FileOutputStream os = new FileOutputStream(dirName+fileName);
workbook.write(os);
os.close();
// request.setAttribute("exportInfo", "数据导出成功,系统将其保存至" + dirName + "目录下的" + fileName + "文件!");
out.write("数据导出成功,系统将其保存至" + dirName + "目录下的" + fileName + "文件!");
} catch (IOException e) {
// request.setAttribute("exportInfo", dirName + "目录下的" + fileName + "文件已存在且正在使用,若要覆盖,请先关闭当前文件后再导出!");
out.write("目录" + dirName + "下的" + fileName + "文件已存在且正在使用,若要覆盖,请先关闭当前文件后再导出!");
}
return null;
} catch (Exception e) {
e.printStackTrace();
log.error("公共场所导出Excel时出错!", e);
return "dbError";
}
}
本文出自 “猪会飞” 博客,请务必保留此出处http://jiyanle.blog.51cto.com/6932197/1384618
原文:http://jiyanle.blog.51cto.com/6932197/1384618