小脚本
.txt文件格式,每行存在五个字段以':::'分隔,对应excel每行五列:
WINDOWS_NT_IS_STARTING_UP:::Windows NT是启动:::系统:::系统:::[{"key":"eventId","values":["512"]}]
WINDOWS_IS_SHUTTING_DOWN:::窗户是关闭的:::系统:::系统:::[{"key":"eventId","values":["513"]}]
AN_AUTHENTICATION_PACKAGE_HAS_BEEN_LOADED_BY_THE_LOCAL_SECURITY_AUTHORITY:::身份验证包被当地安全部门:::系统:::系统:::[{"key":"eventId","values":["514"]}]
A_TRUSTED_LOGON_PROCESS_HAS_REGISTERED_WITH_THE_LOCAL_SECURITY_AUTHORITY:::一个可信登录过程与当地安全机关登记:::系统:::系统:::[{"key":"eventId","values":["515"]}]
INTERNAL_RESOURCES_ALLOCATED_FOR_THE_QUEUING_OF_AUDIT_MESSAGES_HAVE_BEEN_EXHAUSTED,_LEADING_TO_THE_LOSS_OF_SOME_AUDITS:::内部资源分配给审计消息的队列已经筋疲力尽,导致的损失一些审计:::系统:::系统:::[{"key":"eventId","values":["516"]}]
THE_AUDIT_LOG_WAS_CLEARED:::审计日志被清除:::系统:::系统:::[{"key":"eventId","values":["517"]}]
pom.xml依赖:
org.apache.poi
poi
3.14
org.apache.poi
poi-ooxml
3.14
变量定义:
private static final String XLS = "xls";
private static final String XLSX = "xlsx";
.txt读取与转换:
//读取本地文件
private static List
private static List
调用:
public static void main(String[] args) {
writeExcel(readFile(),5,"D:/工作簿1.xlsx");
}
写入Excel:
/**
* 写入Excel
* @param dataList 写入Excel 的数据
* @param cloumn 总列数
* @param finalXlsxPath 写入路径
*/
private static void writeExcel(List dataList, int cloumn,String finalXlsxPath){
OutputStream out = null;
try {
File finalXlsxFile = new File(finalXlsxPath);
Workbook workBook = getWorkbok(finalXlsxFile);
// workBook.setSheetName(0,"Windows Events");
// sheet 工作页
Sheet sheet = workBook.getSheetAt(0);
//获取Excel最后一行
int rowNumber = sheet.getLastRowNum();
//empty
for (int i = 1; i <= rowNumber; i++) {
Row row = sheet.getRow(i);
sheet.removeRow(row);
}
for (int j = 0; j < dataList.size(); j++) {
// 创建一行:从第二行开始,跳过属性列
Row row = sheet.createRow(j + 1);
// 得到要插入的每一条记录
Map dataMap = dataList.get(j);
String name = dataMap.get("name").toString();
String cnName = dataMap.get("cnName").toString();
String category = dataMap.get("Category").toString();
String subCategory = dataMap.get("subCategory").toString();
String matchingFields = dataMap.get("matching_fields").toString();
for (int k = 0; k <= cloumn; k++) {
// 在一行内循环
Cell first = row.createCell(0);
first.setCellValue(name);
Cell second = row.createCell(1);
second.setCellValue(cnName);
Cell third = row.createCell(2);
third.setCellValue(category);
Cell cell3 = row.createCell(3);
cell3.setCellValue(subCategory);
Cell cell4 = row.createCell(4);
cell4.setCellValue(matchingFields);
}
}
// 创建文件输出流,准备输出电子表格:这个必须有,否则你在sheet上做的任何操作都不会有效
out = new FileOutputStream(finalXlsxPath);
workBook.write(out);
} catch (Exception e) {
System.out.println("error error" + e);
} finally{
try {
if(out != null){
out.flush();
out.close();
System.out.println("数据导出成功");
}
} catch (IOException e) {
System.out.println("error error error" + e);
}
}
}
//判断Excel格式(在此有点鸡肋,当做功能拓展~_~)
public static Workbook getWorkbok(File file) throws IOException{
Workbook wb = null;
FileInputStream in = new FileInputStream(file);
if(file.getName().endsWith(XLS)){ //Excel 2003
wb = new HSSFWorkbook(in);
}else if(file.getName().endsWith(XLSX)){ // Excel 2007/2010
wb = new XSSFWorkbook(in);
}
return wb;
}
缺陷:导入的excel必须存在