/**
* 导出Excel
*
* @return
* @throws Exception
*/
public String export() throws Exception {
String sid = request.getParameter("sid");
response.setCharacterEncoding("utf-8");
response.setContentType("utf-8");
response.setContentType("application/vnd.ms-excel");
String fileName = "问卷调查发送记录表";// 设置excel文件的名字
if (!fileName.equals("")) {
String header = "attachment; filename="
+ new String((fileName + ".xls").getBytes(), "iso8859-1");
response.setHeader("Content-Disposition", header);
}
// 创建响应输出流对象
OutputStream out = response.getOutputStream();
SmssendRcorderDetail send = null;
QuestionnaireBaseInformation info = null;
List gList = iquery.querySmssendRcorderDetailById(Long.valueOf(sid));
List list = iquery.QueryQuesionnaireById(sid);
if (list != null && list.size() > 0) {
info = (QuestionnaireBaseInformation) list.get(0);
}
if (gList == null) {
return null;
}
try {
// 创建Excls文件
WritableWorkbook workbook = Workbook.createWorkbook(out);
// 创建工作簿(名称,索引)
WritableSheet ws = workbook.createSheet("问卷调查发送记录表", 0);
/*-------------------设置标题---------------------*/
// 设置字体
WritableFont font = new WritableFont(WritableFont.ARIAL, 11,
WritableFont.BOLD,// 加粗显示
false, UnderlineStyle.NO_UNDERLINE, // 设置下划线
jxl.format.Colour.RED);
// 设置单元格格式化对象
WritableCellFormat cellFormat = new WritableCellFormat(font);
// cellFormat.setAlignment(Alignment.CENTRE);// 显示字体居中
// cellFormat.setVerticalAlignment(VerticalAlignment.CENTRE);//
// 设置垂直居中
/*---------------设置表头---------------*/
// excel显示的表头
String[] titles = new String[] { "手机号", "姓名", "电表号", "短信发送时间" };
WritableFont fontTitle = new WritableFont(WritableFont.ARIAL, 11,
WritableFont.NO_BOLD,// 不加粗显示
false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
WritableCellFormat cellFormat_title = new WritableCellFormat(
fontTitle);// 设置样式
cellFormat_title.setAlignment(Alignment.CENTRE);
// 设置单元格背景色
cellFormat_title.setBackground(Colour.GREY_40_PERCENT);
for (int i = 0; i < titles.length; i++) {
// 参数: 列,行,内容,格式
Label title = new Label(i, 1, "" + titles[i], cellFormat_title);
ws.addCell(title);
}
/*----------------导出数据生成Excel---------------*/
WritableCellFormat cellFormat_data = new WritableCellFormat();// 设置样式
cellFormat_data.setAlignment(Alignment.CENTRE);
cellFormat_data.setBorder(Border.ALL, BorderLineStyle.THIN);
// 将要生成的列加载在excel中
String qtitle = "";
String qanswer = "";
for (int i = 0; i < gList.size(); i++) {
send = (SmssendRcorderDetail) gList.get(i);
ws.mergeCells(0, 0, 6, 0);// 合并单元格,其中的3为显示的行数减1
ws.mergeCells(0, 1, 6, 1);// 合并单元格,其中的3为显示的行数减1
qtitle = "问卷标题:" + send.getInvestigationTitle();// excel文件的头
if (info.getInvestigationType().equals("0")) {
qanswer = "调查问题与答案:【" + "单选】"
+ send.getInvestigationProblem()
+ send.getInvestigationAnswer();
} else {
qanswer = "调查问题与答案:【" + "自由回答】"
+ send.getInvestigationProblem()
+ send.getInvestigationAnswer();
}
Label motifs = new Label(0, 0, qtitle, cellFormat);
Label qmotifs = new Label(0, 1, qanswer, cellFormat);
ws.addCell(motifs);// 写入主题
ws.addCell(qmotifs);
Label phone = new Label(0, i + 2, send.getUserPhoneNo(),
cellFormat_data);
Label name = new Label(1, i + 2, send.getUserName(),
cellFormat_data);
Label syns = new Label(2, i + 2, send.getSynstatus(),
cellFormat_data);
Label time = new Label(3, i + 2, send.getSmsSendtime(),
cellFormat_data);
// 设置列宽
ws.setColumnView(0, 16); // 设置列宽,第1列
ws.setColumnView(3, 20);// 设置列宽,第4列
// 邦定要生成的字段
ws.addCell(phone);
ws.addCell(name);
ws.addCell(syns);
ws.addCell(time);
}
// 写成文件
workbook.write();
workbook.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.close();
response.flushBuffer();
return null;
}
<a href="<%=path%>/questionnaire/questionAction!export.action?sid=<s:property value='#question.businessNo' />">输出Excel</a>