package com.cheng;
import com.github.liaochong.myexcel.core.HtmlToExcelFactory;
import org.apache.poi.ss.usermodel.Workbook;
import org.simplejavamail.outlookmessageparser.OutlookMessageParser;
import org.simplejavamail.outlookmessageparser.model.OutlookMessage;
import javax.swing.*;
import java.io.File;
import java.io.FileOutputStream;
public class EmailExcelConverter {
public static void main(String[] args) throws Exception{
String sourcePath = JOptionPane.showInputDialog(".msg文件源目录,例如:E:\\document\\source");
String targetPath = JOptionPane.showInputDialog(".xls文件目标目录,例如:例如:E:\\document\\target");
if (sourcePath.endsWith("\\")) {
sourcePath = sourcePath.substring(0, sourcePath.length() -1);
}
if (targetPath.endsWith("\\")) {
targetPath = targetPath.substring(0, targetPath.length() -1);
}
System.out.println(sourcePath);
System.out.println(targetPath);
toExcel(sourcePath, targetPath);
}
public static void toExcel(String sourcePath, String targetPath) throws Exception {
File excelDir = new File(targetPath);
if(!excelDir.exists()) {//如果文件夹不存在
excelDir.mkdir();//创建文件夹
}
File msgFiles = new File(sourcePath);
File[] files = msgFiles.listFiles();
for(File msgFile : files) {
String filename = msgFile.getName();
if (filename.endsWith(".msg")) {
OutlookMessageParser parser = new OutlookMessageParser();
//将msg文件解析成OutlookMessage对象
OutlookMessage msg = parser.parseMsg(msgFile);
//将html解析成Workbook对象
Workbook wb = HtmlToExcelFactory.readHtml(msg.getConvertedBodyHTML()).useDefaultStyle().build();
//将Workbook写出到excel文件
filename = msgFile.getName().substring(0, filename.lastIndexOf("."));
File excelFile = new File(targetPath + "/" + filename + ".xls");
try (FileOutputStream outputStream = new FileOutputStream(excelFile)) {
wb.write(outputStream);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.cheng</groupId>
<artifactId>process_outlook_email</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.simplejavamail</groupId>
<artifactId>outlook-message-parser</artifactId>
<version>1.7.8</version>
<exclusions>
<!-- 排除poi,避免与myexcel冲突 -->
<exclusion>
<artifactId>poi</artifactId>
<groupId>org.apache.poi</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.liaochong</groupId>
<artifactId>myexcel</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>