项目笔记:导出Excel功能设置导出数据样式

     /**
* 导出-新导出
*
* @return
* @throws IOException
*/
@OperateLogAnn(type = OperateEnum.EXPORT, hibInter = HibInterEnum.ONLOAD)
public String createXML() throws IOException {
try {
this.msg = RESULT_FAIL; area = areaService.queryAreaByCode(this.getRequest().getParameter("areaCode"));
ipRange = this.getRequest().getParameter("ipRange");
// 查询条件
Map<String, String> searchMap = new HashMap<String, String>();
if (StringUtils.isNotBlank(ip)) {
searchMap.put("ip", ip);
}
if (StringUtils.isNotBlank(mac)) {
searchMap.put("mac", mac);
}
if (StringUtils.isNotBlank(version)) {
searchMap.put("version", version);
}
if (StringUtils.isNotBlank(installStatus)) {
searchMap.put("installStatus", installStatus);
}
if (StringUtils.isNotBlank(startTime)) {
searchMap.put("startTime", startTime);
}
if (StringUtils.isNotBlank(endTime)) {
searchMap.put("endTime", endTime);
} List<TerminalInfo> terminalInfoTems = terminalInfoService.terminalL3List(String.valueOf(area.getId()), ipRange, searchMap); String logPath = this.getSession(false).getServletContext().getRealPath(SystemStatics.PATH_LOG_XML);
File xmlFile = LogXMLUtil.createExcel("terminalInfo", 5, terminalInfoTems, logPath);
// 为下载 做 准备
ActionContext.getContext().put("fileName", xmlFile.getName());
ActionContext.getContext().put("filePath", xmlFile.getPath());
this.msg = RESULT_SUCCESS;
} catch (Exception e) {
log.error("导出日志出错", e);
}
return "downLoadUI";
}

  引入这些包用于设置样式的

package com.vrv.paw.utils.export;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.List; import jxl.Workbook;
import jxl.format.Colour;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont; import org.dom4j.Document;
import org.dom4j.Element; import com.vrv.paw.domain.TerminalInfo;
import com.vrv.paw.utils.DateTypeConverterUtil;
import com.vrv.paw.utils.ReflectUtil;
import com.vrv.paw.utils.XMLUtil;

  新写的导出方法

  @SuppressWarnings("unchecked")
public static File createExcel(String rootElement ,int col,List<TerminalInfo> listObjects,String path ) throws IOException{
//动态获取Excel标题
Document document = XMLUtil.getDocument( path+"/"+"reportForm.xml");
Element domain = document.getRootElement();
Element log = domain.element( rootElement );
Element title = log.element("title");
String fileName = title.attributeValue("name");
fileName = fileName+"("+DateTypeConverterUtil.converterUtil2String(new Date(), "yyyy-MM-dd")+")";
List<Element> columns = log.element("head").elements("column");
String filePath = path+"/"+fileName+".xls";
//新建Excel文件
File excelFile=new File(filePath);
if(excelFile.exists()){
excelFile.delete();
}
excelFile.createNewFile();
//用JXL向新建的文件中添加内容
jxl.write.WritableSheet ws = null;
jxl.write.WritableWorkbook wwb = null;
OutputStream outf = new FileOutputStream(excelFile);
try {
wwb = Workbook.createWorkbook(outf);
ws = wwb.createSheet(fileName, );
//表头样式
WritableFont headFont = new WritableFont(WritableFont.TIMES,,WritableFont.BOLD);
WritableFont titleFont= new WritableFont(WritableFont.TIMES,,WritableFont.BOLD);
WritableFont dataFont = new WritableFont(WritableFont.TIMES,,WritableFont.NO_BOLD);
WritableCellFormat writHeadFont=new WritableCellFormat(headFont);
WritableCellFormat writTitleFont=new WritableCellFormat(titleFont);
WritableCellFormat writDataFont= null;//new WritableCellFormat(dataFont);
//把水平对齐方式指定为居中
writHeadFont.setAlignment(jxl.format.Alignment.CENTRE);
writTitleFont.setAlignment(jxl.format.Alignment.CENTRE);
//设置背景颜色
writHeadFont.setBackground(Colour.GOLD);
writTitleFont.setBackground(Colour.VERY_LIGHT_YELLOW);
//把垂直对齐方式指定为居中
writHeadFont.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
writTitleFont.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); ws.mergeCells(, , col, );
ws.addCell(new Label(, , fileName,writHeadFont));
String[] fileds = new String[columns.size()];
for (int i = ; i < columns.size(); i++) {
Element column = columns.get(i);
fileds[i]=column.attributeValue("attribute") ;
ws.addCell(new Label(i, , column.getTextTrim(),writTitleFont));
//设置单元格宽度
ws.setColumnView(i, );
}
TerminalInfo obj = null ;
for(int k = ; k < listObjects.size(); k++){
obj = listObjects.get(k);
obj.setDevOnlyId(obj.getTerOnlyId());
writDataFont=new WritableCellFormat(dataFont);
writDataFont.setAlignment(jxl.format.Alignment.CENTRE);
writDataFont.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
if (k%==) {
writDataFont.setBackground(Colour.ICE_BLUE);
}
for (int i = ; i < fileds.length; i++) {
Object field = ReflectUtil.doField( obj, fileds[i] ) ;
if("".equals(field.toString())){
field = "安装";
}
if("".equals(field.toString())){
field = "卸载";
}
if("".equals(field.toString())){
field = "更新";
}
if("".equals(field.toString())){
field = "上报心跳";
}
if("".equals(field.toString())){
field = "关机";
}
ws.addCell(new Label( i ,k+, field == null ? "" : field.toString(),writDataFont));
}
}
wwb.write();
wwb.close();
} catch (Exception e) {
e.printStackTrace();
}
return excelFile ;
}

项目笔记:导出Excel功能设置导出数据样式

上一篇:PHP导出Excel,设置表格样式,填充颜色等较为复杂样式


下一篇:Java语言进阶过程(转)