package function;
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.xssf.usermodel.*;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.Attribute;
public class XMLtoExcelGroup {
private static XSSFWorkbook wb;
public static void main(String[] args) {
String file = "xml的文件地址";
writeExcel(file);
}
@SuppressWarnings({ "unchecked" })
public static Map<String, List<List<String>>> analysisXml(String xmlpath) {
Map<String, List<List<String>>> sheetMap = new LinkedHashMap<String, List<List<String>>>();
List<List<List<String>>> testCaseLists = new ArrayList<List<List<String>>>();
SAXReader reader = new SAXReader();
try {
Document document = reader.read(new File(xmlpath));
Element root = document.getRootElement();
List<Element> rootlist = root.elements();
for (Element testcase : rootlist) {
List<Element> testCaseList = testcase.elements();
List<List<String>> testDateLists = new ArrayList<List<String>>();
testCaseLists.add(testDateLists);
sheetMap.put(testcase.attributeValue("name"), testDateLists);
for (Element testdata : testCaseList) {
List<Attribute> attributes = testdata.attributes();
List<String> testDataValue = new ArrayList<String>();
testDateLists.add(testDataValue);
for (Attribute att : attributes) {
testDataValue.add(att.getValue());
}
}
}
} catch (DocumentException e) {
System.out.println("xml file type error! ");
}
return sheetMap;
}
/**
* @author:
* @function: xml write in excel
* @param: path
* @exception: IOException
* @return: void
* @throws IOException
* @throws DocumentException
*/
@SuppressWarnings({ "rawtypes", "static-access", "unchecked" })
public static void writeExcel(String file) {
wb = new XSSFWorkbook();
XSSFFont font = wb.createFont();
font.setFontHeightInPoints((short) 24);
font.setFontName("宋体");
font.setColor(font.COLOR_NORMAL);
font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
XSSFCellStyle style = wb.createCellStyle();
style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
style.setFont(font);
Map<String, List<List<String>>> sheetMap = analysisXml(file);
Iterator iterator = sheetMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
XSSFSheet sheet = creatSheetHead(entry.getKey().toString());
List<List<String>> dateLists = (List<List<String>>) entry.getValue();
for (int j = 1; j < dateLists.size() + 1; j++) {
XSSFRow row = sheet.createRow(j);
List<String> dataValue = (List<String>) dateLists.get(j - 1);
for (int i = dataValue.size() - 1; i >= 0; i--) {
XSSFCell cell = row.createCell(dataValue.size() - i - 1);
cell.setCellValue(dataValue.get(i));
sheet.autoSizeColumn(i);
}
}
}
createExcel("f://file//模板6.xlsx");
}
/**
* @author:
* @function: create excel
* @param: path
* @exception: IOException
* @return: void
*/
public static void createExcel(String path) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
wb.write(os);
} catch (IOException e) {
e.printStackTrace();
}
byte[] xlsx = os.toByteArray();
File file = new File(path);
OutputStream out = null;
try {
out = new FileOutputStream(file);
try {
out.write(xlsx);
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/**
* @author:
* @function: create sheet's head
* @param: groups
* @exception: null
* @return: XSSFSheet
*/
private static XSSFSheet creatSheetHead(String sheetName) {
XSSFSheet sheet = wb.createSheet(sheetName);
XSSFRow rowhead = sheet.createRow(0);
rowhead.setHeight((short) 500);
List<String> sheetHead = new ArrayList<String>();
sheetHead.add(0, "description");
sheetHead.add(1, "key");
sheetHead.add(2, "group");
for (int j = 0; j < sheetHead.size(); j++) {
XSSFCell cellhead = rowhead.createCell(j);
cellhead.setCellValue(sheetHead.get(j));
sheet.autoSizeColumn(j);
}
return sheet;
}
}