这个是jxl 的jar包
链接:https://pan.baidu.com/s/1UPkn694HhiibkTQ-MxlmRA
提取码:jaqp
如果用的maven的话:
<!-- excel 引用包 --> <dependency> <groupId>net.sourceforge.jexcelapi</groupId> <artifactId>jxl</artifactId> <version>2.6.9</version> </dependency>
================================下面是 java 后端代码,此方法直接返回 void或者 String 都可以,这个是直接下载到 服务器上面
//设置excel的名称
String name="门店信息"+starDate+"-"+endDate+DateUtils.getNowDateStr("HHmmss");
//设置文件路径
String files="D:" + File.separator +"HEM"+ File.separator+"md"+File.separator+DateUtils.getNowDateStr("yyyy-MM-dd")+File.separator;
if(!FileUtil.exists(files)){
FileUtil.mkdirFolder(files);
}
//创建文件
File file = new File(files,name+".xls");
WritableWorkbook book = Workbook.createWorkbook(file);
WritableSheet sheet = book.createSheet("sheet", 0);
//设置列宽
String[] stylesline={"0,15","1,40","2,55","3,30","4,15"};//逗号前面是 列的编号,后面是 设置的宽度
sheet = ExcelStyle.lineStyle(sheet, stylesline);
//设置表头样式(加粗)
WritableCellFormat titleStyle = ExcelStyle.titleStyle();
//设置内容样式
WritableCellFormat contentStyle = ExcelStyle.contentStyle();
//设置表头内容
String[] titles={"门店编号","门店名称","门店地址","门店负责人和电话","是否删除"};
if (titles != null) {//设置表头内内容
for (int i = 0; i < titles.length; i++) {
String title = titles[i];
if (!StringUtils.isEmpty(title)) {//StringUtils.isNotBlank()
Label label = new Label(i, 0, titles[i],titleStyle);
sheet.addCell(label);
}
}
}
if(List!=null && List.size()>0){
//设置内容
for (int j = 0; j < List.size(); j++) {//设置每行单元格的内容
Label label = new Label(0, j+1, List.get(j).getStoreNo(),contentStyle);
sheet.addCell(label);
Label label1 = new Label(1, j+1, List.get(j).getFranchiserSalesName(),contentStyle);
sheet.addCell(label1);
Label label2 = new Label(2, j+1, List.get(j).getStoreAddress(),contentStyle);
sheet.addCell(label2);
Label label3 = new Label(3, j+1, List.get(j).getStorePrincipal(),contentStyle);
sheet.addCell(label3);
if("0".equals(String.valueOf(List.get(j).getIsDel()))){
Label label4 = new Label(4, j+1,"否",contentStyle);
sheet.addCell(label4);
}else if("1".equals(String.valueOf(List.get(j).getIsDel()))){
Label label5 = new Label(4, j+1,"是",contentStyle);
sheet.addCell(label5);
}
}
}
book.write();
book.close();
==================================================
//设置表头样式
public static WritableCellFormat titleStyle() throws WriteException{
WritableFont fonttitle = new WritableFont(WritableFont.createFont("宋体"), 14 ,WritableFont.BOLD);
WritableCellFormat wcf = new WritableCellFormat(fonttitle);
wcf.setAlignment(Alignment.CENTRE);//水平居中
wcf.setVerticalAlignment(VerticalAlignment.CENTRE);// 垂直居中
wcf.setWrap(true);//自动换行
return wcf;
}
//设置列宽
///设置内容的样式
public static WritableSheet lineStyle(WritableSheet sheet,String[] stylesline) throws WriteException{
for(int i=0;i<stylesline.length;i++){
if(!StringUtils.isEmpty(stylesline[i])){
String[] split = stylesline[i].split(",");
if(split.length>=2){//设置列宽
sheet.setColumnView(Integer.valueOf(split[0]),Integer.valueOf(split[1]));
}else if(split.length==1){//设置默认,列宽为100
sheet.setColumnView(Integer.valueOf(split[0]),100);
}
}
}
return sheet;
}