解析XML到Excel dom4j.jar poi.jar
/**
* 解析XML,把结果写⼊Excel
* @param xmlPath xml⽂件路径
* @param workbook Excel表
*
*/
@SuppressWarnings("unchecked")
private static void setAlarmInfoToExcelSheet(String xmlPath,
HSSFWorkbook workbook)
{
HSSFSheet sheet = SheetAt(0);// excel sheet
HSSFRow row; // 在sheet表格中创建⼀⾏
HSSFCell cell; // 在每⼀⾏中配置单元格
HSSFHyperlink link; // 每个单元格对应的链接
// 解析XML,遍历每个alarm,把结果写⼊excel单元格
Document document = XMLDocFromFile(xmlPath);
if (null == document)
{
return;
}
List<Node> nodeList = document.selectNodes("//alarm");
String strTemp = "";
for (int i = 0; i < nodeList.size(); i++)
{
Element alarm = (Element) (i);
// 从第4⾏开始写⼊,前⾯是模板⾥设置的表头
row = ateRow(i + LINK_START_ROW);
// 序号
cell = ateCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(i + 1);
// 错误级别
cell = ateCell(1);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
strTemp = alarm.attributeValue("level");
cell.setCellValue(strTemp);
// 告警ID
cell = ateCell(2);
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
strTemp = alarm.attributeValue("code");
cell.setCellValue(strTemp);
// 所在⽂件
Node filePathNode = alarm.selectSingleNode("./extends/filePath");
if (null != filePathNode)dom4j读取xml
{
strTemp = ((Element) filePathNode).attributeValue("value");
cell = ateCell(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)
if (null != reasonNode)
{
strTemp = Text();
cell = ateCell(4);
cell.setCellType(HSSFCell.CELL_TYPE_STRING); cell.setCellValue(strTemp);
}
// 修改提⽰
Node solution = alarm.selectSingleNode("./solution"); if (null != solution)
{
strTemp = Text();
cell = ateCell(5);
cell.setCellType(HSSFCell.CELL_TYPE_STRING); cell.setCellValue(strTemp);
}
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论