java outlook 中的excel 转成excel文件

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>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值