JSP主要代码页面:
<a href="<%=path %>/excelAction.action">
<div id="page17_jButton1" class="vjbutton vjbutton_c_style1 border_radius_3">
<div class="vjbutton_txtR"><span class="btniconcum1"><img src="${pageContext.request.contextPath}/images/jexcel.png" width="16" height="16"/></span><span class="btniconcum2"><img src="${pageContext.request.contextPath}/images/jexcel.png" width="16" height="16"/></span><span class="btntxt"></span>导出指导记录</div>
</div>
</a>
Struts配置:
<!-- 用Excel表格导出数据 -->
<action name="excelAction" class="excelAction">
</action>
Ation处理:
public class ExcelAction extends ActionSupport{
List<Online_answerInfo>malist;
private MassageDao answerdao;
Map map=null;
public MassageDao getAnswerdao() {
return answerdao;
}
public void setAnswerdao(MassageDao answerdao) {
this.answerdao = answerdao;
}
public String execute()throws Exception
{
map=ActionContext.getContext().getSession();
String user=(String)map.get("user");
System.out.println("user:"+user);
malist=answerdao.queryMassage(user);
String []tableHeader={"提问时间","提问问题","回答问题时间","解答"};
short cellNumber=(short)tableHeader.length;//表的列数
HSSFWorkbook workbook = new HSSFWorkbook(); //创建一个excel
HSSFCell cell = null; //Excel的列
HSSFRow row = null; //Excel的行
HSSFCellStyle style = workbook.createCellStyle(); //设置表头的类型
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//设置水平居中显示
// style.setAlignment(HSSFCellStyle.VERTICAL_CENTER);//设置垂直居中显示
style.setWrapText(true);//设置自动换行
style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
HSSFCellStyle style1 = workbook.createCellStyle(); //设置数据类型
style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// style1.setAlignment(HSSFCellStyle.VERTICAL_CENTER);
style1.setFillForegroundColor((short) 13);// 设置背景色
style1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style1.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
style1.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
style1.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
style1.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
HSSFFont font = workbook.createFont(); //设置字体
HSSFFont font1 = workbook.createFont();
HSSFSheet sheet = workbook.createSheet("sheet1"); //创建一个sheet
HSSFHeader header = sheet.getHeader();//设置sheet的头
try {
if(malist.size() < 1 ){
header.setCenter("查无资料");
}else{
header.setCenter("问答表");
row = sheet.createRow(0);
row.setHeight((short)600);
for(int k = 0;k < cellNumber;k++){
cell = row.createCell((short) k);//创建第0行第k列
cell.setCellValue(tableHeader[k]);//设置第0行第k列的值
sheet.setColumnWidth((short)k,(short)8000);//设置列的宽度
font.setColor(HSSFFont.COLOR_RED); // 设置单元格字体的颜色.
font.setFontHeight((short)350); //设置单元字体高度
style1.setFont(font);//设置字体风格
cell.setCellStyle(style1);
}
for(int i = 0 ;i < malist.size() ;i++){
Online_answerInfo online = (Online_answerInfo)malist.get(i);//获取student对象
row = sheet.createRow((i + 1));//创建第i+1行
row.setHeight((short)400);//设置行高
if(online.getQuestion_time() != null){
cell = row.createCell((short) 0);//创建第i+1行第0列
cell.setCellValue(online.getQuestion_time());//设置第i+1行第0列的值
cell.setCellStyle(style);//设置风格
}
if(online.getQuestion() != null){
cell = row.createCell((short) 1); //创建第i+1行第1列
cell.setCellValue(online.getQuestion());//设置第i+1行第1列的值
cell.setCellStyle(style); //设置风格
}
//由于下面的和上面的基本相同,就不加注释了
if(online.getAnswer_time() != null){
cell = row.createCell((short) 2);
cell.setCellValue(online.getAnswer_time());
cell.setCellStyle(style);
}
if(online.getAnswer()!= null){
cell = row.createCell((short) 3);
cell.setCellValue(online.getAnswer());
cell.setCellStyle(style);
}
font1.setColor(HSSFColor.VIOLET.index);
style.setFont(font1);//设置字体风格
cell.setCellStyle(style);
}
}
} catch (Exception e) {
e.printStackTrace();
}
HttpServletResponse response = null;//创建一个HttpServletResponse对象
OutputStream out = null;//创建一个输出流对象
try {
response = ServletActionContext.getResponse();//初始化HttpServletResponse对象
out = response.getOutputStream();//
String headerStr ="学生问答表";
headerStr =new String(headerStr.getBytes("gb2312"), "ISO8859-1");//headerString为中文时转码
response.setHeader("Content-disposition","attachment; filename="+ headerStr+".xls");//filename是下载的xls的名,建议最好用英文
response.setContentType("application/msexcel;charset=UTF-8");//设置类型
response.setHeader("Pragma","No-cache");//设置头
response.setHeader("Cache-Control","no-cache");//设置头
response.setDateHeader("Expires", 0);//设置日期头
workbook.write(out);
out.flush();
workbook.write(out);
} catch (IOException e) {
e.printStackTrace();
}finally{
try{
if(out!=null){
out.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
return null;
}
}