一、用法介绍
需要导入一个jar包,名为-poi.jar;jar包的版本需在3.9,如果引入其它版本,如5.0以上,需要再在次基础上导入其它jar包,详情百度。
二、具体代码
/**
* @Author: SayHello
* @Date: 2022/11/8 19:21
* @Version: 1.0
* @Introduction:
*/
public class PoiTest {
public static void main(String[] args) throws IOException {
//1.创建一个工作簿
HSSFWorkbook sheets = new HSSFWorkbook();
//2.创建一个工作表
Sheet sheet = sheets.createSheet("王小明");
//3.创建行
Row row1 = sheet.createRow(0);
//4.创建该行的列
Cell cell1 = row1.createCell(0);
cell1.setCellValue("id");
Cell cell2 = row1.createCell(1);
cell2.setCellValue("接收者");
Cell cell3 = row1.createCell(2);
cell3.setCellValue("聊天内容");
Cell cell4 = row1.createCell(3);
cell4.setCellValue("发送者");
Cell cell5 = row1.createCell(4);
cell5.setCellValue("时间");
//得到输出流和设置输出地址
FileOutputStream fos = new FileOutputStream("E:\\王小明.xls");
//输出
sheets.write(fos);
//关闭流
fos.close();
System.out.println("文件生成完毕");
}
}
三、封装好的工具类
消息的格式自定义,这里的格式为:
你好#hello#hi&good#fine#haha(&分行、#分列)
/**
* @Author: SayHello
* @Date: 2022/11/8 17:39
* @Version: 1.0
* @Introduction: 该类用于导出文件
*/
@SuppressWarnings("all")
public class ExportService {
/**
* 该方法用于导入消息记录为excel文件
* @param content 消息记录
* @param dst 导出地址
* @throws IOException
*/
public void exportByExcel(String content,String dst) throws IOException {
//1.创建一个工作簿
HSSFWorkbook sheets = new HSSFWorkbook();
//2.创建 一个工作表
Sheet sheet = sheets.createSheet("聊天记录");
Row row = sheet.createRow(0);
row.createCell(0).setCellValue("id");
row.createCell(1).setCellValue("接收者");
row.createCell(2).setCellValue("聊天内容");
row.createCell(3).setCellValue("发送者");
row.createCell(4).setCellValue("时间");
row.createCell(5).setCellValue("消息类型");
row.createCell(6).setCellValue("消息状态");
String[] strings = content.split("&");
for (int i = 0; i < strings.length; i++) {
Row xRow = sheet.createRow(i+1);
String[] contents = strings[i].split("#");
for (int j = 0; j < contents.length; j++) {
xRow.createCell(j).setCellValue(contents[j]);
}
}
//生成一张表(IO流),03版本就是使用xls结尾
FileOutputStream fos = new FileOutputStream(dst);
//输出
sheets.write(fos);
//关闭流
fos.close();
}
/**
* 该方法用于导出消息记录为文本文件
* @param content 消息记录
* @param dst 导出地址
* @throws IOException
*/
public void exportByTxt(String content,String dst) throws IOException {
BufferedWriter bf = new BufferedWriter(new FileWriter(dst,true));
String header = "id\t\t接收者\t\t聊天内容\t\t发送者\t\t时间\t\t消息状态\t\t消息类型";
bf.write(header);
bf.write("\n");
String[] strings = content.split("&");
for (int i = 0; i < strings.length; i++) {
String[] contents = strings[i].split(" ");
for (int j = 0; j < contents.length; j++) {
bf.write(contents[j]+"\t\t");
}
bf.write("\n");
}
bf.flush();
bf.close();
}
/**
* 该方法用于导出消息记录为HTML文件
* @param content 消息记录
* @param dst 导出地址
* @throws IOException
*/
public void exportByHtml(String content,String dst) throws IOException {
BufferedWriter bf = new BufferedWriter(new FileWriter(dst,true));
StringBuilder builder = new StringBuilder();
builder.append("<!DOCTYPE html>\n" +
"<html lang=\"en\">\n" +
"<head>\n" +
" <meta charset=\"UTF-8\">\n" +
" <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n" +
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
" <title>Document</title>\n" +
"</head>\n" +
"<body>\n" +
" <table border=\"1\">\n" +
" <tr>\n" +
" <th>id</th>\n" +
" <th>接收者</th>\n" +
" <th>聊天内容</th>\n" +
" <th>发送者</th>\n" +
" <th>时间</th>\n" +
" <th>消息类型</th>\n" +
" <th>消息状态</th>\n" +
" </tr>\n");
String[] strings = content.split("&");
for (int i = 0; i < strings.length; i++) {
builder.append("<tr>");
String[] contents = strings[i].split("#");
for (int j = 0; j < contents.length; j++) {
builder.append("<td>"+contents[j]+"</td>");
}
builder.append("</tr>");
}
builder.append("</table></body></html>");
bf.write(builder.toString());
bf.flush();
bf.close();
}
}
四、导出效果图预览
1、文本文件(txt)
2、excel文件
3、HTML文件
五、资源文件
jar包和其它资源:
基于JavaSe、Jdbc、Mysql…excel、HTML文件
点击链接保存,或者复制本段内容,打开「阿里云盘」APP ,无需下载极速在线查看,视频原画倍速播放。
https://www.aliyundrive.com/s/ZvQJUztnvg2
提取码: lv62