|
public class POI2ExcelTest {
SimpleDateFormat df = new SimpleDateFormat("yyyy.MM.dd");// 设置日期格式
@SuppressWarnings("deprecation")
@Transactional
public InputStream exportExcel() {
// 要导出的数据,这里我造的假数据来的。
List<Map<String, String>> datas = toMap(madeData());
HSSFWorkbook wb = new HSSFWorkbook(); // 创建excel
HSSFSheet sheet = wb.createSheet(df.format(new Date()) + "的数据"); // 创建子表名称
// 表头样式
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中
HSSFFont font = wb.createFont();
font.setFontName("宋体");// 设置字体名称
font.setFontHeightInPoints((short) 24);// 设置字号
font.setBold(true);// 加粗
style.setFont(font);
// 筛选条件样式
HSSFCellStyle style_s = wb.createCellStyle();
style_s.setAlignment(HSSFCellStyle.ALIGN_RIGHT);// 右对齐
style_s.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中
HSSFFont font_s = wb.createFont();
font_s.setFontName("宋体");// 设置字体名称
font_s.setFontHeightInPoints((short) 12);// 设置字号
font_s.setBold(true);// 加粗
style_s.setFont(font_s);
// 筛选条件值样式
HSSFCellStyle style_V = wb.createCellStyle();
style_V.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 左对齐
style_V.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中
HSSFFont font_V = wb.createFont();
font_V.setFontName("宋体");// 设置字体名称
font_V.setFontHeightInPoints((short) 12);// 设置字号
font_V.setBold(true);// 加粗
style_V.setFont(font_V);
// 字段样式
HSSFCellStyle style_C = wb.createCellStyle();
style_C.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
style_C.setBorderBottom(HSSFCellStyle.BORDER_THIN);// 下边框
style_C.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
style_C.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
style_C.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平居中
style_C.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中
HSSFFont font_C = wb.createFont();
font_C.setFontName("宋体");// 设置字体名称
font_C.setFontHeightInPoints((short) 11);// 设置字号
font_C.setBold(true);// 加粗
style_C.setFont(font_C);
// 数据样式
HSSFCellStyle style_D = wb.createCellStyle();
style_D.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平居中
style_D.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中
style_D.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
style_D.setBorderBottom(HSSFCellStyle.BORDER_THIN);// 下边框
style_D.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
style_D.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
HSSFFont font_D = wb.createFont();
font_D.setFontName("宋体");// 设置字体名称
font_D.setFontHeightInPoints((short) 11);// 设置字号
style_D.setFont(font_D);
// 创建表头标题
int rowIndex = 0;
HSSFRow headerRow = sheet.createRow(rowIndex);
HSSFCell headercell = headerRow.createCell(0);
// 合并单元格:起始行,终止行,起始列,终止列。
sheet.addMergedRegion(new CellRangeAddress(0, 2, 0, 5));
// 表标题
headercell.setCellValue("学生信息");
headercell.setCellStyle(style);
rowIndex += 3;
// 创建筛选数据
HSSFRow searchRow = sheet.createRow(rowIndex);
rowIndex += 2;
HSSFCell timeCell = searchRow.createCell(0);
timeCell.setCellValue("日期:");
timeCell.setCellStyle(style_s);
HSSFCell stimeCellV = searchRow.createCell(1);
stimeCellV.setCellValue(df.format(new Date()));
stimeCellV.setCellStyle(style_V);
// 创建字段数据
HSSFRow columRow = sheet.createRow(rowIndex);
columRow.setRowStyle(style);
String[] columns = { "学号", "姓名", "学院", "专业", "班级", "性别" };
int colum_i = 0;
for (String columName : columns) {
HSSFCell columCell = columRow.createCell(colum_i);
columCell.setCellValue(columName);
columCell.setCellStyle(style_C);
sheet.autoSizeColumn(colum_i);
colum_i++;
}
rowIndex++;
// 创建数据
for (Map<String, String> data : datas) {
HSSFRow dataRow = sheet.createRow(rowIndex);
int data_i = 0;
for (String colum : columns) {
HSSFCell dataCell = dataRow.createCell(data_i);
String val = data.get(colum);
dataCell.setCellValue(val != null ? val : "");
dataCell.setCellStyle(style_D);
sheet.autoSizeColumn(data_i);
data_i++;
}
rowIndex++;
}
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
FileOutputStream fis = new FileOutputStream("D:\\03.xls");
wb.write(fis);
fis.close();
wb.write(os);
os.close();
wb.close();
InputStream is = new ByteArrayInputStream(os.toByteArray());
return is;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private List<Map<String, String>> toMap(List<Student> students) {
List<Map<String, String>> list = new ArrayList<>();
for (Student student : students) {
Map<String, String> map = new HashMap<>();
map.put("学号", student.getNumber() != null ? student.getNumber() : "");
map.put("姓名", student.getName() != null ? student.getName() : "");
map.put("学院", student.getCollege() != null ? student.getCollege() : "");
map.put("专业", student.getMajor() != null ? student.getMajor() : "");
map.put("班级", student.getClasses() != null ? student.getClasses() : "");
map.put("性别", student.getSex() != null ? student.getSex() : "");
list.add(map);
}
return list;
}
private List<Student> madeData() {
List<Student> list = new ArrayList<>();
list.add(new Student("2014041743301", "saly", "计算机科学与工程学院", "计算机科学与技术", "计科143", "女"));
list.add(new Student("2014041743301", "张三", "计算机科学与工程学院", "计算机科学与技术", "计科143", "男"));
list.add(new Student("2014041743301", "李四", "计算机科学与工程学院", "计算机科学与技术", "计科143", "男"));
list.add(new Student("2014041743301", "王五", "计算机科学与工程学院", "计算机科学与技术", "计科143", "男"));
list.add(new Student("2014041743301", "赵六", "计算机科学与工程学院", "计算机科学与技术", "计科143", "男"));
return list;
}
}
|