jxl导出excel
Posted zzlcome
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jxl导出excel相关的知识,希望对你有一定的参考价值。
action中: private HttpServletResponse response; String fileurl = "excle/test.xls"; String fileName = "fileName";
public void exportExcel(String fileurl, String fileName,
String title, HttpServletResponse response)
throws BiffException, IOException, WriteException
WritableWorkbook book = getExportHead(fileurl, fileName, response);
WritableSheet sheet = book.getSheet(0);
// 定义格式 字体 下划线 斜体 粗体 颜色
WritableFont sheetTitleFont = new WritableFont(WritableFont.createFont("宋体"), 22,WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
WritableCellFormat sheetTitleCellFormat = new WritableCellFormat(sheetTitleFont); // 单元格定义
sheetTitleCellFormat.setAlignment(Alignment.CENTRE);
WritableFont cellFont = new WritableFont(WritableFont.createFont("宋体"), 11,WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
WritableCellFormat cellFormat = new WritableCellFormat(cellFont); // 单元格定义
cellFormat.setWrap(true);
cellFormat.setAlignment(Alignment.CENTRE);
sheet.addCell(new Label(0, 0, fileName,sheetTitleCellFormat));//第一行标题
WritableCellFormat writableCellFormat = new WritableCellFormat(cellFormat);
String[] recordStr = new String[]"serNo","name","age";
int row = 2;//从第2行开始
List<Record> list = 查询数据;
if(list != null && list.size() > 0)
for(int i = 0;i < list.size() ;i++)
for(int col = 0;col < recordStr.length;col++)
String result = list.get(i).getStr(recordStr[col]);
sheet.addCell(new Label(col, row+i, result, cellFormat));
book.write();
book.close();
另外:
//TODO mergeCells(列,行,向左合并第几列,向下合并到第几行) sheet.mergeCells(0, 5, 3, 0);//合并第五行前三列
//TODO Label(列,行,内容,样式)
以上是关于jxl导出excel的主要内容,如果未能解决你的问题,请参考以下文章