public class ExportExcel {
private static ExportExcel exportExcel = new ExportExcel();;
private ExportExcel() {
}
public static ExportExcel getInstance() {
return exportExcel;
}
/**
* 通过一个List<String[]>和String[]和sheetName以及路径path导出一个.xls的excel文件 返回导出的情况。
*
* @param list
* List<Object>就是传入一个从数据库查出的List信息
* @param head
* head这是传入一个数组,这个数组是每列的列名
* @param sheetName
* @return
*/
public static boolean exportToExcel(String path, ArrayList list,
String[] head, String sheetName, String tableName) {
boolean falg = true;
String folder = path + File.separator;
File f = new File(folder);
if (!f.exists()) {
f.mkdirs();
}
WritableWorkbook wwb = null;
try {
File excel = new File(folder + tableName + ".xls");
if (excel.exists()) {
excel.delete();
}
wwb = Workbook.createWorkbook(excel);
WritableSheet sheet = wwb.createSheet(sheetName, 0);
makeSheet(sheet, list, head);
wwb.write();
} catch (Exception e) {
falg = false;
e.printStackTrace();
} finally {
try {
if (wwb != null) {
wwb.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return falg;
}
/**
* 通过一个List<String[]>和String[] 打造一个WritableSheet
*
*/
public static WritableSheet makeSheet(WritableSheet sheet,ArrayList list, String[] head) throws RowsExceededException,
WriteException {
for (int i = 0; i < head.length; i++) {
Label labelHead = new Label(i, 0, head[i]);
sheet.addCell(labelHead);
}
// if(list==null){System.out.println("集合为空");}
for (int j = 0; j < list.size(); j++) {
Object[] obj = (Object[]) list.get(j);
for (int k = 0; k < obj.length; k++) {
String s = obj[k] + "";
sheet.addCell(new Label(k, j + 1, s));
}
}
return sheet;
}
public static void writeToClient(HttpServletResponse response, String path)
throws IOException {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
InputStream fis = null;
OutputStream fos = response.getOutputStream();
File uploadFile = new File(path);
fis = new FileInputStream(uploadFile);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
// 弹出下载对话框的关键代码
response.setHeader("Content-disposition", "attachment;filename="
+ URLEncoder.encode(path, "utf-8"));
int bytesRead = 0;
// 输入流进行先读,然后用输出流去写
byte[] buffer = new byte[8192];
while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.flush();
fis.close();
bis.close();
fos.close();
}
}
===============后台调用方法-==============
StreamResponse onSubmitFromExportExcelForm(){
String[] head = new String[]{"序号","车牌号","门限车度","实际车速"};
String floder=message.get("excelSavePath")+File.separator+DateUtil.dateFormat(Calendar.getInstance().getTime());
String tablename="zhang"+Calendar.getInstance().getTimeInMillis();
ExportExcel.exportToExcel(floder, overSpeedDataToExcelList, head, "超速报警数据列表",tablename);
String filename=tablename+".xls";
return new MyExcelStreamResponse(floder,filename);
}
=============创建一个类========
public class MyExcelStreamResponse implements StreamResponse{
private String filename;
private String floder;
public MyExcelStreamResponse(String floder,String filename){
this.floder=floder;
this.filename=filename;
}
public String getContentType(){
return "application/vnd.ms-excel";
}
public InputStream getStream(){
try {
return new BufferedInputStream(new FileInputStream(floder+File.separator+filename));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
public void prepareResponse(Response response){
response.setHeader("Content-disposition","attachment;filename="+ filename );
}
}
===================还需要指定保存位置==========
建立一个属性文件:*.properties ,放入Excel保存路径 excelSavePath=D:\\excel\\yyyy
这就是Excel的数据导出功能