excel 表頭

public static String createCourseUnitExl(String name, String filePath,List<CourseUnit> list ) {
String[] head0 = new String[] { "年級及專業", "總人數", "大班數", "小班數", "課程編號","課程", "總學時",
"總學分", "已完成","本學期" ,"周學時","上課起止周次","任課教師","教學組長","備註"};
String[] head1 = new String[] { "學時", "學分", "總學時", "理論","實驗" ,"課外","學分","理論","實驗"};
String[] head2 = new String[] { "姓名", "職稱", "學時", "姓名", "職稱", "學時" };
String[] headnum0 = new String[] { "0,2,0,0", "0,2,1,1","0,2,2,2","0,2,3,3","0,2,4,4","0,2,5,5","0,2,6,6","0,2,7,7","0,0,8,9","0,0,10,14"
,"0,2,15,15","0,2,16,16","0,0,17,22","0,2,23,23","0,2,24,24" };//對應excel中的行和列,下表從0開始{"開始行,結束行,開始列,結束列"}
String[] headnum1 = new String[] {"1,2,8,8", "1,2,9,9", "1,2,10,10","1,2,11,11","1,2,12,12","1,2,13,13","1,2,14,14","1,1,17,19"
,"1,1,20,22"};
// String[] headnum2 = new String[] { "3,3,19,19", "3,3,20,20", "3,3,21,21","3,3,22,22" };
try {
HSSFWorkbook wb = reportXls(list,head0,headnum0, head1, headnum1,head2);
// 第六步,將文件存到指定位置
String newFileName = DateUtil.DateTimeToStringNOSSS(new Date());
String path = filePath + File.separator + newFileName + ".xls";
FileOutputStream fout = new FileOutputStream(path);
wb.write(fout);
fout.close();
return path;
} catch (Exception e) {
e.printStackTrace();
}
return "";
}

public static HSSFWorkbook reportXls( List<CourseUnit> dataList, String[] head0, String[] headnum0,
String[] head1, String[] headnum1, String[] head2)
throws Exception {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("任務書");// 建立一個表
// 普通單元格樣式(中文)
HSSFFont font2 = workbook.createFont();
font2.setFontName("宋體");
font2.setFontHeightInPoints((short) 12);
HSSFCellStyle style1 = workbook.createCellStyle();
style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
style1.setWrapText(true); // 換行
style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
HSSFCellStyle style2 = workbook.createCellStyle();
style2.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下邊框
style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左邊框
style2.setBorderTop(HSSFCellStyle.BORDER_THIN);//上邊框
style2.setBorderRight(HSSFCellStyle.BORDER_THIN);//右邊框
style2.setFont(font2);
style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
style2.setWrapText(true); // 換行
style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
sheet.setDefaultRowHeight((short)360);//設置行高

HSSFRow row = sheet.createRow(0);
row.setHeight((short) 0x349);
HSSFCell cell = row.createCell(0);
for (int i = 0; i < 24; i++) {
cell = row.createCell(i);
if(i<8)
cell.setCellValue(head0[i]);
if(i==8)
cell.setCellValue(head0[8]);
if(i==10)
cell.setCellValue(head0[9]);
if(i==15)
cell.setCellValue(head0[10]);
if(i==16)
cell.setCellValue(head0[11]);
if(i==17)
cell.setCellValue(head0[12]);
}
//動態合併單元格
for (int i = 0; i < headnum0.length; i++) {
String[] temp = headnum0[i].split(",");
Integer startrow = Integer.parseInt(temp[0]);
Integer overrow = Integer.parseInt(temp[1]);
Integer startcol = Integer.parseInt(temp[2]);
Integer overcol = Integer.parseInt(temp[3]);
sheet.addMergedRegion(new CellRangeAddress(startrow, overrow,
startcol, overcol));
}
//設置合併單元格的參數並初始化帶邊框的表頭(這樣作能夠避免由於合併單元格後有的單元格的邊框顯示不出來)
row = sheet.createRow(1);
for (int i = 0; i < 24; i++) {
cell = row.createCell(i);
cell.setCellStyle(style2);
if(i > 7 && i< 15) {
cell.setCellValue(head1[i-6]);
}
if(i==17) cell.setCellValue(head1[7]);
if(i==20) cell.setCellValue(head1[8]);
}
//動態合併單元格
for (int i = 0; i < headnum1.length; i++) {
String[] temp = headnum1[i].split(",");
Integer startrow = Integer.parseInt(temp[0]);
Integer overrow = Integer.parseInt(temp[1]);
Integer startcol = Integer.parseInt(temp[2]);
Integer overcol = Integer.parseInt(temp[3]);
sheet.addMergedRegion(new CellRangeAddress(startrow, overrow,
startcol, overcol));
}
row = sheet.createRow(2);
for (int i = 0; i < 24; i++) {
cell = row.createCell(i);
if(i > 16 && i< 23) {
cell.setCellValue(head2[i-17]);
}
}

// 設置列值-內容
int i = 3,r=3;
for (int index = 0;index<dataList.size();index++) {
CourseUnit unit = dataList.get(index);
List<CourseUnitTeacher> list= unit.getTeacherList();
int size = list.size();
r+=size;
row = sheet.createRow(i );
List<String> itemList = unit.getItems();
for (int j = 0; j < size; j++) {
row = sheet.createRow(i);
i++;
for (int k = 0; k < 24; k++) {
cell = row.createCell(k);
if (k<17&&j==0){
cell.setCellValue(itemList.get(k));
sheet.addMergedRegion(new CellRangeAddress(i-1, r-1 , k,k));
}else{
setTeacher(list.get(j),cell,k);
}
cell.setCellStyle(style1);
}
}
}
return workbook;

}
public static void setTeacher(CourseUnitTeacher t, HSSFCell cell,int k ){
if(t.getCourseCategory() == CourseCategoryEnum.THEORY.getCategory()){
if(k==17)
cell.setCellValue(t.getTeacherName());
if(k==18)
cell.setCellValue(t.getPositionName());
if(k==19)
cell.setCellValue(t.getClassHour());
}else if(t.getCourseCategory() ==CourseCategoryEnum.LAB.getCategory()){
if(k==20)
cell.setCellValue(t.getTeacherName());
if(k==21)
cell.setCellValue(t.getPositionName());
if(k==22)
cell.setCellValue(t.getClassHour());
}

}excel

相關文章
相關標籤/搜索