java文件导出Excel:
1. 声明一个类 编写导出样式
public class ExcelExportUtils {
public static synchronized void ExportExcel(String sheetName,String[] headers,List<Object[]> data,OutputStream out){
//声明一个工作簿
HSSFWorkbook workbook = new HSSFWorkbook()
//生成一个表格
HSSFSheet sheet = workbook.createSheet(sheetName)
//设置表格默认列宽度为15个字符
sheet.setDefaultColumnWidth(20)
//生成一个样式,用来设置标题样式
HSSFCellStyle style = workbook.createCellStyle()
//设置这些样式
style.setBorderBottom(BorderStyle.THIN)
style.setBorderLeft(BorderStyle.THIN)
style.setBorderRight(BorderStyle.THIN)
style.setBorderTop(BorderStyle.THIN)
style.setAlignment(HorizontalAlignment.CENTER_SELECTION)
style.setFillForegroundColor(HSSFColorPredefined.SKY_BLUE.getIndex())
style.setFillPattern(FillPatternType.ALT_BARS)
//生成一个字体
HSSFFont font = workbook.createFont()
font.setColor(HSSFColorPredefined.BLACK.getIndex())
font.setBold(true)
font.setFontHeight((short)300)
//把字体应用到当前的样式
style.setFont(font)
HSSFCellStyle style2 = workbook.createCellStyle()
style2.setVerticalAlignment(VerticalAlignment.CENTER)
style2.setAlignment(HorizontalAlignment.CENTER)
//产生表格标题行
HSSFRow row = sheet.createRow(0)
row.setHeight((short)400)
for(int i = 0
HSSFCell cell = row.createCell(i)
cell.setCellStyle(style)
HSSFRichTextString text = new HSSFRichTextString(headers[i])
cell.setCellValue(text)
}
for (int i=0
row = sheet.createRow(i+1)
for(int j = 0
HSSFCell cell =row.createCell(j)
cell.setCellStyle(style2)
cell.setCellValue(String.valueOf(data.get(i)[j]))
}
}
try {
workbook.write(out)
} catch (IOException e) {
e.printStackTrace()
}
}
public static void main(String[] args) throws FileNotFoundException {
OutputStream ops= new FileOutputStream("E:/aaa.xls")
String[] heades= {"c","a","b","gg",}
List<Object[]> list = new ArrayList<>()
String [] arr = {"1","2","3","3"}
list.add(arr)
ExportExcel("测试",heades,list,ops)
}
}
2. 在controller层编写实现代码
@RequestMapping("exportExcel")
@ResponseBody
public void ExportExcel(String plantCod, String time, HttpServletResponse res) {
res.setContentType("octets/stream");
String excelName = "测试导出功能";
OutputStream out = null;
try {
res.addHeader("Content-Disposition", "attachment;filename=" + new String(excelName.getBytes("gb2312"), "ISO8859-1") + ".xls");
out = res.getOutputStream();
String[] heades = { ""};
List<Map<String, Object>> listMap = new ArrayList<>();
List<Object[]> list = new ArrayList<>();
listMap = energyReportService.queryEnergyReport(plantCod,time);
for(int i = 0; i<listMap.size(); i++){
for(Map map:listMap){
Object [] obj = {map.get("")};
list.add(obj);
}
ExcelExportUtils.ExportExcel("", heades, list, out);
out.flush();
}
ExcelExportUtils.ExportExcel("aaa", heades, list, out);
out.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3. 在jsp页面添加点击事件
function exportExcel(){
var cod = $('#input-plants').find("option:selected").attr('data-id');
var time = $('#energyDate').datetimepicker().data().date;
window.location.href = getBasePath()+"/energyreport/exportExcel?
heatId=${heatId}&plantCod="+cod+"&time="+time;
}