dom4j.jar poi.jar
/**
* 解析XML,把结果写入Excel
* @param xmlPath xml文件路径
* @param workbook Excel表
*
*/
@SuppressWarnings("unchecked")
private static void setAlarmInfoToExcelSheet(String xmlPath,
HSSFWorkbook workbook)
{
HSSFSheet sheet = workbook.getSheetAt(0);// excel sheet
HSSFRow row; // 在sheet表格中创建一行
HSSFCell cell; // 在每一行中配置单元格
HSSFHyperlink link; // 每个单元格对应的链接
// 解析XML,遍历每个alarm,把结果写入excel单元格
Document document = XmlUtils.getXMLDocFromFile(xmlPath);
if (null == document)
{
return;
}
List<Node> nodeList = document.selectNodes("//alarm");
String strTemp = "";
for (int i = 0; i < nodeList.size(); i++)
{
Element alarm = (Element) nodeList.get(i);
// 从第4行开始写入,前面是模板里设置的表头
row = sheet.createRow(i + LINK_START_ROW);
// 序号
cell = row.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(i + 1);
// 错误级别
cell = row.createCell(1);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
strTemp = alarm.attributeValue("level");
cell.setCellValue(strTemp);
// 告警ID
cell = row.createCell(2);
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
strTemp = alarm.attributeValue("code");
cell.setCellValue(strTemp);
// 所在文件
Node filePathNode = alarm.selectSingleNode("./extends/filePath");
if (null != filePathNode)
{
strTemp = ((Element) filePathNode).attributeValue("value");
cell = row.createCell(3);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(strTemp.substring(strTemp.lastIndexOf("\\") + 1));
link = new HSSFHyperlink(HSSFHyperlink.LINK_URL);
link.setAddress("CI_HOME/"
+ strTemp.substring(strTemp.lastIndexOf("\\") + 1));
cell.setHyperlink(link);
}
// 原因
Node reasonNode = alarm.selectSingleNode("./reason");
if (null != reasonNode)
{
strTemp = reasonNode.getText();
cell = row.createCell(4);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(strTemp);
}
// 修改提示
Node solution = alarm.selectSingleNode("./solution");
if (null != solution)
{
strTemp = solution.getText();
cell = row.createCell(5);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(strTemp);
}
}
}