用JAVA代码来导出EXCEL
有些情况下我们需要将数据导出到excel中
例子: 根据一个班级ID 来导出全班学生的成绩
两个实体类
班级实体类
Class.java
public class Class {
private int classId;//班级ID
private String className;//班级名称
。。。
}
}
学生实体类
Student.java
public class Student {
private int stuId;//学号
private String stuName;//姓名
private double scode;//得分
private String classID;//班级ID
。。。
}
导出excel方法(这里是struts框架的Action中的方法 可以直接调用保存excel)
public ActionForward exportExcel(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//假设掉用方法时 传入班级编号
String classId= request.getParameter("id");
Class class= classManager.getClassById(classId);//此方法自行实现s
if (class== null)
return null;
List<Student> list = stuManager .getStuListByClassId(classId);//此方法自行实现
String[] columnHeads = { "学号", "姓名", "得分"};
int columnsize = columnHeads.length, rowsize = list.size(), rowindex = 0;
// 创建一个HSSFWorbook对象s
HSSFWorkbook hWorkbook = new HSSFWorkbook();
// 创建一个HSSFSheet对象(相当于作业本的某一页)
HSSFSheet hSheet = hWorkbook.createSheet();
// 创建行(此行作为头)----------------创建头开始
HSSFRow hRow = hSheet.createRow(rowindex++);
//创建单元格(第一(0)个)
HSSFCell hCell = hRow.createCell((short) 0);
//设置字符集
hCell.setEncoding(HSSFCell.ENCODING_UTF_16);
//调用getCellStyle()方法获得一个格式对象
HSSFCellStyle cellStyle = getCellStyle(hWorkbook, (short) 320,(short) 700);
//将上面获得的格式对象给对应单元格
hCell.setCellStyle(cellStyle);
hCell.setCellValue("YYS学校"+class.getClassName()+"班成绩单");
//批量创建单元格-----------到后面合并单元格
for (int i = 1; i < columnsize; i++) {
hCell = hRow.createCell((short) i);
hCell.setCellStyle(cellStyle);
}
// 创建行----------------创建头结束
//创建表头 ( 学号 姓名 得分 )
hRow = hSheet.createRow(rowindex++);
cellStyle = getCellStyle(hWorkbook, (short) 220, (short) 600);
for (int i = 0; i < columnsize; i++) {
hCell = hRow.createCell((short) i);
hCell.setEncoding(HSSFCell.ENCODING_UTF_16);
hCell.setCellValue(columnHeads[i]);
hCell.setCellStyle(cellStyle);
}
//遍历所有学生
cellStyle = getCellStyle(hWorkbook, (short) 220, (short) 500);//设置单元格格式
for (int i = 0, j = 0; i < rowsize; i++, j = 0) {
hRow = hSheet.createRow(rowindex++);
Student stu = list.get(i);
// 学号
hCell = hRow.createCell((short) j);
hCell.setEncoding(HSSFCell.ENCODING_UTF_16);
hCell.setCellValue(stu.getStuId());
hCell.setCellStyle(cellStyle);
hSheet.setColumnWidth((short) j++, (short) 2000);
// 姓名
hCell = hRow.createCell((short) j);
hCell.setEncoding(HSSFCell.ENCODING_UTF_16);
hCell.setCellValue(stu.getStuName());
hCell.setCellStyle(cellStyle);
hSheet.setColumnWidth((short) j++, (short) 3000);
// 得分
hCell = hRow.createCell((short) j);
hCell.setEncoding(HSSFCell.ENCODING_UTF_16);
hCell.setCellValue(stu.getScode());
hCell.setCellStyle(cellStyle);
hSheet.setColumnWidth((short) j++, (short) 3000);
}
//合并单元格 四个参数的意思是(X1,Y1,X2,Y2)
hSheet.addMergedRegion(new Region(0, (short) 0, 0,
(short) (columnsize - 1)));
//固定表头(前一个参数代表列,后一个参数单表行)
hSheet.createFreezePane(0, 1);
response.setContentType("application/x-download");
String filename = URLEncoder.encode(class.getClassName()+ ".xls", "UTF-8");
response.addHeader("Content-Disposition", "attachment;filename="+ filename);
response.setContentType("application/msexcel"); // 定义输出类型
response.setContentType("UTF-8");
hWorkbook.write(response.getOutputStream());
response.getOutputStream().flush();
response.getOutputStream().close();
}
return null;
}
//设置样式方法
public HSSFCellStyle getCellStyle(HSSFWorkbook hWorkbook, short fontHeight,
short boldWeight) {
HSSFCellStyle cellStyle = hWorkbook.createCellStyle();
HSSFFont font = hWorkbook.createFont();
cellStyle = hWorkbook.createCellStyle();
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setTopBorderColor(HSSFColor.BLACK.index);
cellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
cellStyle.setRightBorderColor(HSSFColor.BLACK.index);
cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
font = hWorkbook.createFont();
font.setFontHeight(fontHeight);
font.setBoldweight(boldWeight);
font.setFontName("宋体");
cellStyle.setFont(font);
cellStyle.setWrapText(true);
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER)
return cellStyle;
}
摘自 xuliangwen的专栏
补充:软件开发 , Java ,