WritableWorkbook wwb = null;
// 创建可写入的Excel工作簿
filePath = this.getRequest().getServletContext().getRealPath("/")+filePath;
File file=new File(filePath);
//以fileName为文件名来创建一个Workbook
wwb = Workbook.createWorkbook(file);
// 创建工作表
WritableSheet ws = wwb.createSheet("Test Shee 1", 0);
/**
* 定义单元格样式
*/
WritableFont wf_title = new WritableFont(WritableFont.ARIAL, 12,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK); // 定义格式 字体 粗体 斜体 下划线 颜色
WritableFont wf_titlebt = new WritableFont(WritableFont.ARIAL, 12,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED);//必填项格式
WritableCellFormat wcf_title = new WritableCellFormat(wf_title); // 单元格定义
WritableCellFormat wcf_titlebt = new WritableCellFormat(wf_titlebt); // 单元格定义
wcf_title.setWrap(true);
wcf_title.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式
wcf_title.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); // 设置对齐方式
wcf_title.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK); //设置边框
wcf_titlebt.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式
wcf_titlebt.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); // 设置对齐方式
wcf_titlebt.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK); //设置边框
WritableFont wf_table = new WritableFont(WritableFont.ARIAL, 10,
WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK); // 定义格式 字体 粗体 斜体 下划线 颜色
WritableCellFormat wcf_table = new WritableCellFormat(wf_table); // 单元格定义
wcf_table.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式
wcf_table.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); // 设置对齐方式
wcf_table.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK); //设置边框
ws.mergeCells(0, 0, list.size()-1, 0);
for(int i=0;i<list.size();i++){
ws.setColumnView(i, 20); // 设置列的宽度
}
Label title20=new Label(0,0,“恶趣味权威”。",wcf_titlebt);
ws.addCell(title20);
//写进文档
wwb.write();
// 关闭Excel工作簿对象
wwb.close();
//下载模板
fileDownload(response,filePath,fileName);
fielDownload 方法:
public void fileDownload(HttpServletResponse response, String filePath,String fileName){
byte[] data;
try {
data = FileUtil.toByteArray2(filePath);
fileName = URLEncoder.encode(fileName, "UTF-8");
response.reset();
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
response.addHeader("Content-Length", "" + data.length);
response.setContentType("application/octet-stream;charset=UTF-8");
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
outputStream.write(data);
outputStream.flush();
outputStream.close();
response.flushBuffer();
} catch (IOException e) {
logger.error("下载文件过程中,文件流异常!!");
e.printStackTrace();
}
}