boolean isCreate = false;
String[] pathInfo = classPath("S_CLASS");
String uploadPath = pathInfo[1]+"tmpFile/"; //文件临时存储路径
String[] classInfo = getClassInfo(classInfoId);
List planList = getPlanList(classInfoId);
String[] planTitle = new String[]{"日期","时间","课程名称","讲师","单位","职务/职称","学习方式","上课地点"};
planList.add(0,planTitle);
//构建临时的Excel文件
String tmpFilePath=uploadPath+"plan"+classInfo[0]+".xls";
File file = new File(tmpFilePath);
isCreate = writeExcel(file,classInfo[1],planList);
if(isCreate){
try{
SmartUpload myup = new SmartUpload();
myup.initialize(pageContext);
myup.setContentDisposition(null);
myup.downloadFile(tmpFilePath,"","“"+classInfo[1]+"”学习计划.xls");
}catch(Exception e){
Date date = new Date();
SimpleDateFormat sDFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sDFormat.format(date)+"\t文件下载异常");
e.printStackTrace();
out.print("<script language=\"javascript\">alert(\"导出文件下载异常\");window.location.href=\"mag_class_list.jsp?class_info_id="+classInfoId +"\";</script>");
}
}else{
out.print("<script language=\"javascript\">alert(\"导出文件创建失败\");window.location.href=\"mag_class_list.jsp?class_info_id="+classInfoId +"\";</script>");
}
代码片段下
/** Excel写文件功能,作为demo
* file 导出文件的名称
* sheetName Excel第一个sheet的名称
* fileContent 文件内容(第一行为表头信息)
*/
private boolean writeExcel(File file,String SheetName,List fileContent){
boolean ret = false;
try {
WritableWorkbook workbook = Workbook.createWorkbook(file); //以Excel表格打开文件
WritableSheet worksheet=workbook.createSheet(SheetName,0); //创建Sheet
//定义表头样式
WritableFont titlef = new WritableFont(WritableFont.createFont("宋体"), 10, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
WritableCellFormat TitleFormat = new WritableCellFormat (titlef);
TitleFormat.setAlignment(jxl.format.Alignment.CENTRE); //设置单元格水平对齐方式
TitleFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); //设置单元格垂直对齐方式
TitleFormat.setBorder(jxl.format.Border.TOP,jxl.format.BorderLineStyle.THIN); //设置单元格顶部边框样式
TitleFormat.setBorder(jxl.format.Border.LEFT,jxl.format.BorderLineStyle.THIN); //设置单元格左侧边框样式
TitleFormat.setBorder(jxl.format.Border.RIGHT,jxl.format.BorderLineStyle.THIN); //设置单元格右部边框样式
TitleFormat.setBorder(jxl.format.Border.BOTTOM,jxl.format.BorderLineStyle.THIN); //设置单元格底部边框样式
TitleFormat.setBackground(jxl.format.Colour.ICE_BLUE); //设置单元格背景颜色
//定义单元格样式
WritableFont tbFont = new WritableFont(WritableFont.createFont("宋体"), 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
WritableCellFormat ContentFormat = new WritableCellFormat (tbFont);
ContentFormat.setAlignment(jxl.format.Alignment.CENTRE);
ContentFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
ContentFormat.setBorder(jxl.format.Border.TOP,jxl.format.BorderLineStyle.THIN);
ContentFormat.setBorder(jxl.format.Border.LEFT,jxl.format.BorderLineStyle.THIN);
ContentFormat.setBorder(jxl.format.Border.RIGHT,jxl.format.BorderLineStyle.THIN);
ContentFormat.setBorder(jxl.format.Border.BOTTOM,jxl.format.BorderLineStyle.THIN);
CellView cellViewCol = new CellView();
cellViewCol.setSize(11*256); //设置列宽
CellView cellViewRowOne = new CellView();
cellViewRowOne.setSize(2*256); //设置第一行行高
CellView cellViewRow = new CellView();
cellViewRow.setSize(3*230); //设置行高
int allNum = fileContent.size();
Label label = null;
for(int i=0;i<allNum;i++){
String[] strArr = (String[])fileContent.get(i);
if(i == 0){
for(int j=0;j<strArr.length;j++){
label = new Label(j,i,strArr[j],TitleFormat);
worksheet.addCell(label);
worksheet.setColumnView(j, cellViewCol);//设置列宽
worksheet.setRowView(i,cellViewRowOne);//第一行独有行高
}
}else{
if("Y".equals(strArr[0])){
label = new Label(0,i,"自由",ContentFormat);
worksheet.addCell(label);
label = new Label(1,i,"自由",ContentFormat);
worksheet.addCell(label);
worksheet.setColumnView(0,cellViewCol);//设置列宽
worksheet.setColumnView(1,cellViewCol);//设置列宽
worksheet.setRowView(i,cellViewRow);//设置行高
}else if("N".equals(strArr[0])){
label = new Label(0,i,strArr[1],ContentFormat);
worksheet.addCell(label);
label = new Label(1,i,strArr[2]+"-"+strArr[3],ContentFormat);
worksheet.addCell(label);
worksheet.setColumnView(0,cellViewCol);//设置列宽
worksheet.setColumnView(1,cellViewCol);//设置列宽
worksheet.setRowView(i,cellViewRow);//设置行高
}
for(int j=4;j<strArr.length;j++){
if(j == 8){
label = new Label(j-2,i,learnStyle(strArr[j]),ContentFormat);
}else{
label = new Label(j-2,i,strArr[j],ContentFormat);
}
worksheet.addCell(label);
worksheet.setColumnView(j,cellViewCol);//设置列宽
worksheet.setRowView(i,cellViewRow);//设置行高
}
}
label = null;
}
workbook.write();
workbook.close();
ret = true;
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}