加载依赖文件
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version>
</dependency>
下载文件
/**
* 下载文件
* @throws Exception
*/
public static void downFile() throws Exception{
List<User> list = new ArrayList<>();
list.add(new User("雨院长","yupeng@163.com","测试","地址"));
list.add(new User("张博士","boshi@163.com","测试","地址"));
list.add(new User("李教授","jiaoshou@163.com","测试","地址"));
list.add(new User("徐校长","xiaozhang@163.com","测试","地址"));
list.add(new User("杨老湿","laoshi@163.com","测试","地址"));
String url = "/Users/xxx/Desktop/文件管理/";
File ula = new File(url+"/abc.xlsx");
if (!ula.exists()){
ula.createNewFile();
}
System.out.println(ula.getPath());
ExcelWriter writer = ExcelUtil.getWriter();
writer.renameSheet("急急鹏的故事"); //设置sheet的名称
writer.addHeaderAlias("name","姓名");
writer.addHeaderAlias("email","邮箱");
writer.addHeaderAlias("part","部分");
writer.addHeaderAlias("ads","地址");
writer.merge(3,"测试信息");
CellStyle headCellStyle = writer.getStyleSet().getHeadCellStyle(); // 设置标题样式
setSheetStyle(headCellStyle,writer);
writer.write(list,true);
OutputStream out = new FileOutputStream(ula.getPath());
writer.flush(out,true);
writer.close();
out.close();
System.out.println("执行结束!");
}
// 设置表格样式
/**
* 添加表格样式
* @param headCellStyle
* @param writer
*/
public static void setSheetStyle(CellStyle headCellStyle,ExcelWriter writer){
//writer.merge(0, 0, 0, 1,"中心文档", true); // 设置合并单元格
headCellStyle.setAlignment(HorizontalAlignment.CENTER); //水平居中
headCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); //垂直居中
Font font = writer.createFont(); //设置内容字体
font.setBold(true); //加粗
font.setColor((short) 6); // 颜色设置
font.setFontHeightInPoints((short)12); //设置标题字体大小
headCellStyle.setFont(font);
}
// 内部类
class User{
private String name;
private String email;
private String part;
private String ads;
public User(String name,String email,String part,String ads){
this.name = name;
this.email = email;
this.part = part;
this.ads = ads;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPart() {
return part;
}
public void setPart(String part) {
this.part = part;
}
public String getAds() { return ads; }
public void setAds(String ads) {
this.ads = ads;
}
}
上传文件
/**
* 上传文件
* @throws Exception
*/
public static void updateFile() throws Exception{
String url = "/Users/xxx/Desktop/文件管理/";
File ula = new File(url+"/abc.xlsx");
InputStream fis = new FileInputStream(ula);
ExcelReader reader = ExcelUtil.getReader(fis);
// huTool读取excel 1:表示表格头所在行,2:从第几行开始读取,2147483647:行的最大值
// 因为自定义了表格头别名,所以只能使用map接收,如果没有设置别名,可以使用实体接收
List<Map<String,Object>> readAll = reader.read(1,2,2147483647);
// 打印读取的文件信息
System.out.println(readAll);
// 数据处理,略...
}
修改文件名称
/**
* 修改文件的名称
*/
public static void updateFileName() throws Exception{
String url = "/Users/xxx/Desktop/文件管理/我的文档";
File file = new File(url);
File[] files = file.listFiles();
List<File> filesList = Arrays.asList(files);
for (File fileNode: filesList) {
// String fileNameTest = "这是一个视频文件[这是广告信息].mp4";
String name = fileNode.getName();
int index = name.indexOf("[");
if (-1 != index){
// 截取 "[" 之前的文件名称
String substring = name.substring(0, index);
name = substring+".mp4";
String newName = url + File.separator + name;
File newFile = new File(newName);
// 重命名
fileNode.renameTo(newFile);
}
}
}