jxl excel导出

来源:互联网 发布:工信部域名备案系统 编辑:程序博客网 时间:2024/06/04 19:47

大家想用jxl导出的,希望这篇文章对大家有所帮助 。注:这个代码是同事写的被我拿过来了,不要群殴我欧

WritableWorkbook wwb = null;
// 创建可写入的Excel工作簿 当前时间+随机数防止人数过多资源写入时出问题
String fileName = "D://" + System.currentTimeMillis()
+ new Random().nextInt(10000) + ".xls";
try {
File file = new File(fileName);
if (!file.exists()) {
file.createNewFile();
}
// 以fileName为文件名来创建一个Workbook
wwb = Workbook.createWorkbook(file);
// 创建工作表
WritableSheet ws = wwb.createSheet("Test Shee 1", 0);
//设置列的宽度
ws.setColumnView(0, 15);
ws.setColumnView(1, 15);
ws.setColumnView(2, 15);
ws.setColumnView(3, 15);
ws.setColumnView(4, 15);
//定义单元格样式
WritableFont wf_title = new WritableFont(WritableFont.ARIAL, 20,  
WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,  
                   jxl.format.Colour.BLACK); // 定义格式 字体 下划线 斜体 粗体 颜色
WritableFont wf_head = new WritableFont(WritableFont.ARIAL, 11,  
                   WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,  
                   jxl.format.Colour.BLACK); // 定义格式 字体 下划线 斜体 粗体 颜色 
WritableFont wf_table = new WritableFont(WritableFont.ARIAL, 11,  
               WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,  
               jxl.format.Colour.BLACK); // 定义格式 字体 下划线 斜体 粗体 颜色

WritableCellFormat wcf_title = new WritableCellFormat(wf_title); // 单元格定义  
wcf_title.setBackground(jxl.format.Colour.WHITE); // 设置单元格的背景颜色  
       wcf_title.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式  
       wcf_title.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK);
       
       WritableCellFormat wcf_head1 = new WritableCellFormat(wf_head);   
       wcf_head1.setBackground(jxl.format.Colour.GRAY_25);  
       wcf_head1.setAlignment(jxl.format.Alignment.CENTRE);   
       wcf_head1.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK);
       
       WritableCellFormat wcf_table1 = new WritableCellFormat(wf_table);   
       wcf_table1.setAlignment(jxl.format.Alignment.CENTRE);  
       wcf_table1.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK);
       
// 要插入到的Excel表格的行号,默认从0开始
Label ryheName = new Label(0, 0, "人员合同明细",wcf_title);// 表示第
Label cfdssqnames = new Label(0, 1, "序号",wcf_head1);// 表示第
Label mddssqnames = new Label(1, 1, "合同编号",wcf_head1);
Label hwlxName = new Label(2, 1, "姓名",wcf_head1);
Label clsl = new Label(3, 1, "性别",wcf_head1);
Label xllx = new Label(4, 1, "身份证号",wcf_head1);
ws.mergeCells(0,0,11,0);
ws.addCell(ryheName);
ws.addCell(cfdssqnames);
ws.addCell(mddssqnames);
ws.addCell(hwlxName);
ws.addCell(clsl);
ws.addCell(xllx);

//查询的数据结果
for (int i = 0; i < listresult.size(); i++) {
Map<String, Object> map = listresult.get(i);
// WorkContractExcel wc=(WorkContractExcel)map.entrySet();
// System.out.println(map.get("name"));
// 下面的一系列if是防止数据为null导致 后面的.toString出异常
Label CfdssqName = new Label(0, i + 2,i+1+"",wcf_table1 );
ws.addCell(CfdssqName);

if (map.get("code") != null) {//合同编号
Label MddssqName = new Label(1, i + 2, map.get("code").toString(),wcf_table1);
ws.addCell(MddssqName);
}else{
Label MddssqName = new Label(1, i + 2, "",wcf_table1);
ws.addCell(MddssqName);
}
if (map.get("name") != null) {//姓名
Label hwlxNames = new Label(2, i + 2, map.get("name").toString(),wcf_table1);
ws.addCell(hwlxNames);
}else{
Label hwlxNames = new Label(2, i + 2, "",wcf_table1);
ws.addCell(hwlxNames);
}
if (map.get("bSex") != null) {//性别
Label clsls = new Label(3, i + 2, map.get("bSex").toString(),wcf_table1);
ws.addCell(clsls);
}else{
Label clsls = new Label(3, i + 2, "",wcf_table1);
ws.addCell(clsls);
}
if (map.get("pinCode") != null) {//身份证号
Label xllxs = new Label(4, i + 2, map.get("pinCode").toString(),wcf_table1);
ws.addCell(xllxs);
}else{
Label xllxs = new Label(4, i + 2, "",wcf_table1);
ws.addCell(xllxs);
}

}
// 写进文档
wwb.write();
// 关闭Excel工作簿对象
wwb.close();
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 下面为下载
try {
File file = new File(fileName);
if (!file.exists()) {


return null;
}
// 取得文件名。
String filename = file.getName();
// 取得文件的后缀名。
@SuppressWarnings("unused")
String ext = filename.substring(filename.lastIndexOf(".") + 1)
.toUpperCase();
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(
fileName));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename="
+ new String(filename.getBytes()));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(
response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
 

原创粉丝点击