点击下载
//导出
$("#export").click(function (){
if (!$("#startDate").val()){
layer.msg("请选择开始日期")
return
}
if (!$("#endDate").val()){
layer.msg("请选择结束日期")
return;
}
window.location.href = baseUrl + "/giftPurchase/enterAccountDetailsExport"
})
//创建 wordBook
Workbook workbook = ExcelExport.createWorkBook(maps,keys,(short)3);
//画表头
hartHead(workbook,workbook.getSheet("sheet1"),title);
try {
//执行下载
ExcelExport.doloadExcel(workbook,response,"自采礼品入账明细");
} catch (IOException e) {
e.printStackTrace();
}
画表头
private void hartHead(Workbook workbook, Sheet sheet1, String title) {
//单元格位置设定
//开始行,结束列,开始行,结束列
//这里是第一行第一列 占6行
CellRangeAddress cellAddresses = new CellRangeAddress(0, 0, 0, 6);
//第二行,第一列
CellRangeAddress cellAddresses1 = new CellRangeAddress(1, 2, 0, 0);
//第二行,第二列
CellRangeAddress cellAddresses2 = new CellRangeAddress(1, 2, 1, 1);
//第二行,第三列
CellRangeAddress cellAddresses3 = new CellRangeAddress(1, 2, 2, 2);
//设置默认列宽
sheet1.setDefaultColumnWidth(20);
//添加单元格位置设定
sheet1.addMergedRegion(cellAddresses);
sheet1.addMergedRegion(cellAddresses1);
sheet1.addMergedRegion(cellAddresses2);
sheet1.addMergedRegion(cellAddresses3);
//设置字体样式
Font fontHead = workbook.createFont();
fontHead.setBold(true);
fontHead.setFontHeight((short) 360);
//创建一个样式
CellStyle cellStyle = workbook.createCellStyle();
//添加字体样式
cellStyle.setFont(fontHead);
//对齐方式
cellStyle.setAlignment(HorizontalAlignment.CENTER);
//垂直对齐
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
//开启换行
cellStyle.setWrapText(true);
//创建字体
Font otherHeadFont = workbook.createFont();
//字体样式
otherHeadFont.setFontHeight((short)230);
otherHeadFont.setBold(true);
//创建样式
CellStyle otherHeadcs = workbook.createCellStyle();
//添加字体样式
otherHeadcs.setFont(otherHeadFont);
//对齐方式
otherHeadcs.setAlignment(HorizontalAlignment.CENTER);
//垂直对齐方式
otherHeadcs.setVerticalAlignment(VerticalAlignment.CENTER);
//开启换行
otherHeadcs.setWrapText(true);
//创建行
Row row = sheet1.createRow(0);
//高度
row.setHeight((short)600);
//创建列 已继承上面设定的单元格 cellAddresses
Cell cell = row.createCell(0);
//内容
cell.setCellValue(title);
//样式
cell.setCellStyle(cellStyle);
//创建行
Row row1 = sheet1.createRow(1);
//行高
row1.setHeight((short)450);
//创建列 列位置已在上面设定cellAddresses1
Cell cell0 = row1.createCell(0);
cell0.setCellValue("礼品名称");
cell0.setCellStyle(otherHeadcs);
//创建列 列位置已在上面设定 cellAddresses2
Cell cell1 = row1.createCell(1);
cell1.setCellValue("入账数量");
cell1.setCellStyle(otherHeadcs);
//创建列 列位置已在上面设定 cellAddresses3
Cell cell2 = row1.createCell(2);
cell2.setCellValue("采购日期");
cell2.setCellStyle(otherHeadcs);
}
创建 Workbook
/**
*
* @param list 要操作的数据数据
* @param keys 列的key
* @param startRow 开始行
* @return
*/
public static Workbook createWorkBook(List<Map> list, String[] keys, short startRow) {
//创建excel工作牌
XSSFWorkbook workbook = new XSSFWorkbook();
//创建一个sheet(页),并命名
XSSFSheet sheet = workbook.createSheet("sheet1");
sheet.setDefaultColumnWidth(10);
sheet.setDefaultRowHeight((short) 500);
if (list.isEmpty()||keys==null){
return workbook;
}
//设置每列的值
//设置每行的值
for (int i = startRow,a=0; a < list.size(); i++,a++) {
//Row行,Cell方格,Row和Cell都是从0开始计数的
//创建一行,在页sheet上
XSSFRow row1 = sheet.createRow(i);
row1.setHeight((short) 600);
//在row1上上创建一个方格
for (int j = 0; j < keys.length; j++) {
XSSFCell cell = row1.createCell(j);
XSSFCellStyle c = workbook.createCellStyle();
c.setVerticalAlignment(VerticalAlignment.CENTER);
c.setAlignment(HorizontalAlignment.CENTER);
cell.setCellValue(list.get(a).get(keys[j]) == null ? " " : list.get(a).get(keys[j]).toString());
cell.setCellStyle(c);
}
}
return workbook;
}
//下载 Workbook 文件
public static void doloadExcel(Workbook wb,HttpServletResponse response, String fileName) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
//将转换成的Workbook对象通过流形式下载
wb.write(os);
} catch (IOException e) {
e.printStackTrace();
}
byte[] content = os.toByteArray();
InputStream is = new ByteArrayInputStream(content);
// 设置response参数,可以打开下载页面
response.reset();
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename="+ new String((fileName + ".xlsx").getBytes(), "iso-8859-1"));
ServletOutputStream out = response.getOutputStream();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (final IOException e) {
throw e;
} finally {
if (bis != null) {
bis.close();
}
if (bos != null) {
bos.close();
}
}
}