[技术分享] 20171205_excel导出_poi操作excel的基本操作
- 前端:这里不能用ajax请求,ajax请求无法弹出下载保存对话框
- 想办法触发下面的函数
function exportExcel(){
var localObj = window.location;
var contextPath = localObj.pathname.split("/")[1];
var basePath = localObj.protocol + "//" + localObj.host + "/"+ contextPath; window.location.href=basePath+"excel/export";
}
- 后端:poi基本操作
- 创建一个工作薄
HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("Campaign");
- 创建一行
HSSFRow row = sheet.createRow((int) 0);
- 创建一个单元格
HSSFCell cell = row.createCell(0);
- 合并单元格
int firstRow, int lastRow, int firstCol, int lastCol
起始行号,终止行号, 起始列号,终止列号
将第0行到第1行中的第2,3,4单元格合并
sheet.addMergedRegion(new CellRangeAddress(0, 1, 2, 4));
- 设置列宽
30厘米*256像素
0表示第0列
sheet.setColumnWidth(0, 256*30);
- 设置样式
水平居中:
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
垂直居中:
HSSFCellStyle style = wb.createCellStyle();
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
- 字体操作
HSSFCellStyle style1 = wb.createCellStyle(); Font font = wb.createFont(); font.setBoldweight(Font.BOLDWEIGHT_BOLD); //粗体 font.setFontHeightInPoints((short) 16); //设置字体大小 font.setColor(HSSFColor.RED.index); //设置字体颜色