/**
* 导出-新导出
*
* @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, 0);
//表头样式
WritableFont headFont = new WritableFont(WritableFont.TIMES,18,WritableFont.BOLD);
WritableFont titleFont= new WritableFont(WritableFont.TIMES,12,WritableFont.BOLD);
WritableFont dataFont = new WritableFont(WritableFont.TIMES,10,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(0, 0, col, 0);
ws.addCell(new Label(0, 0, fileName,writHeadFont));
String[] fileds = new String[columns.size()];
for (int i = 0; i < columns.size(); i++) {
Element column = columns.get(i);
fileds[i]=column.attributeValue("attribute") ;
ws.addCell(new Label(i, 1, column.getTextTrim(),writTitleFont));
//设置单元格宽度
ws.setColumnView(i, 30);
}
TerminalInfo obj = null ;
for(int k = 0; 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%2==0) {
writDataFont.setBackground(Colour.ICE_BLUE);
}
for (int i = 0; i < fileds.length; i++) {
Object field = ReflectUtil.doField( obj, fileds[i] ) ;
if("0".equals(field.toString())){
field = "安装";
}
if("1".equals(field.toString())){
field = "卸载";
}
if("4".equals(field.toString())){
field = "更新";
}
if("5".equals(field.toString())){
field = "上报心跳";
}
if("6".equals(field.toString())){
field = "关机";
}
ws.addCell(new Label( i ,k+2, field == null ? "" : field.toString(),writDataFont));
}
}
wwb.write();
wwb.close();
} catch (Exception e) {
e.printStackTrace();
}
return excelFile ;
}