Swing批量重命名并导出excel
源代码:
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JSpinner;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.Button;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
/**
* @ClassName: RenameFiles
* @Description: 获取文件列表的过滤器
* @author CC
* @date 2016-4-11 下午4:47:54
*
*/
public class RenameFiles extends JFrame {
/**
*
*/
private static final long serialVersionUID = 4534371106024773867L;
private final class ExtNameFileFilter implements FileFilter {
private String extName;
public ExtNameFileFilter(String extName) {
this.extName = extName;// 保存文件扩展名
}
@Override
public boolean accept(File pathname) {
// 过滤文件扩展名
if (pathname.getName().toUpperCase()
.endsWith(extName.toUpperCase()))
return true;
return false;
}
}
private JPanel contentPane;
private JTextField forderField; //路径
private JTextField templetField; //模版
private File dir;
private JTable table; //列出旧文件名和新文件名
private JTextField extNameField; //扩展名
private JSpinner startSpinner; //开始编号
private JButton export;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
RenameFiles frame = new RenameFiles();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public RenameFiles() {
setResizable(false);
setTitle("文件批量重命名");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 383, 409);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[] { 72, 54, 60, 87, 91, 0 };
gbl_contentPane.rowHeights = new int[] { 25, 25, 10, 25, 24, 25, 2,
216, 0 };
gbl_contentPane.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0,
Double.MIN_VALUE };
gbl_contentPane.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, Double.MIN_VALUE };
contentPane.setLayout(gbl_contentPane);
// JLabel label = new JLabel();
// label.setText("文件批量重命名模块:");
// GridBagConstraints gbc_label = new GridBagConstraints();
// gbc_label.fill = GridBagConstraints.VERTICAL;
// gbc_label.insets = new Insets(0, 0, 5, 5);
// gbc_label.gridwidth = 3;
// gbc_label.gridx = 1;
// gbc_label.gridy = 0;
// contentPane.add(label, gbc_label);
JLabel label_1 = new JLabel();
label_1.setText("文件路径:");
GridBagConstraints gbc_label_1 = new GridBagConstraints();
gbc_label_1.anchor = GridBagConstraints.EAST;
gbc_label_1.fill = GridBagConstraints.VERTICAL;
gbc_label_1.insets = new Insets(0, 0, 5, 5);
gbc_label_1.gridx = 0;
gbc_label_1.gridy = 1;
contentPane.add(label_1, gbc_label_1);
forderField = new JTextField();
forderField.setText("");
GridBagConstraints gbc_forderField = new GridBagConstraints();
gbc_forderField.fill = GridBagConstraints.HORIZONTAL;
gbc_forderField.insets = new Insets(0, 0, 5, 5);
gbc_forderField.gridwidth = 3;
gbc_forderField.gridx = 1;
gbc_forderField.gridy = 1;
contentPane.add(forderField, gbc_forderField);
JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
do_button_actionPerformed(e);
}
});
button.setText("浏览");
GridBagConstraints gbc_button = new GridBagConstraints();
gbc_button.anchor = GridBagConstraints.NORTHWEST;
gbc_button.insets = new Insets(0, 0, 5, 0);
gbc_button.gridx = 4;
gbc_button.gridy = 1;
contentPane.add(button, gbc_button);
JSeparator separator_1 = new JSeparator();
GridBagConstraints gbc_separator_1 = new GridBagConstraints();
gbc_separator_1.fill = GridBagConstraints.BOTH;
gbc_separator_1.insets = new Insets(0, 0, 5, 0);
gbc_separator_1.gridwidth = 5;
gbc_separator_1.gridx = 0;
gbc_separator_1.gridy = 2;
contentPane.add(separator_1, gbc_separator_1);
JLabel label_5 = new JLabel();
label_5.setText("使用#可以指定数字计数占几位:"); //使用*可以插入原文件名
GridBagConstraints gbc_label_5 = new GridBagConstraints();
gbc_label_5.fill = GridBagConstraints.VERTICAL;
gbc_label_5.insets = new Insets(0, 0, 5, 0);
gbc_label_5.gridwidth = 5;
gbc_label_5.gridx = 0;
gbc_label_5.gridy = 3;
contentPane.add(label_5, gbc_label_5);
JLabel label_3 = new JLabel();
label_3.setText(" 模板:");
GridBagConstraints gbc_label_3 = new GridBagConstraints();
gbc_label_3.anchor = GridBagConstraints.EAST;
gbc_label_3.fill = GridBagConstraints.VERTICAL;
gbc_label_3.insets = new Insets(0, 0, 5, 5);
gbc_label_3.gridx = 0;
gbc_label_3.gridy = 4;
contentPane.add(label_3, gbc_label_3);
templetField = new JTextField();
templetField.setText("###");
GridBagConstraints gbc_templetField = new GridBagConstraints();
gbc_templetField.anchor = GridBagConstraints.SOUTH;
gbc_templetField.fill = GridBagConstraints.HORIZONTAL;
gbc_templetField.insets = new Insets(0, 0, 5, 5);
gbc_templetField.gridwidth = 3;
gbc_templetField.gridx = 1;
gbc_templetField.gridy = 4;
contentPane.add(templetField, gbc_templetField);
JLabel label_4 = new JLabel();
label_4.setText("开始于:");
GridBagConstraints gbc_label_4 = new GridBagConstraints();
gbc_label_4.fill = GridBagConstraints.VERTICAL;
gbc_label_4.insets = new Insets(0, 0, 5, 5);
gbc_label_4.gridx = 0;
gbc_label_4.gridy = 5;
contentPane.add(label_4, gbc_label_4);
startSpinner = new JSpinner();
GridBagConstraints gbc_startSpinner = new GridBagConstraints();
gbc_startSpinner.fill = GridBagConstraints.HORIZONTAL;
gbc_startSpinner.insets = new Insets(0, 0, 5, 5);
gbc_startSpinner.gridx = 1;
gbc_startSpinner.gridy = 5;
contentPane.add(startSpinner, gbc_startSpinner);
JLabel label_2 = new JLabel();
label_2.setText(" 扩展名:");
GridBagConstraints gbc_label_2 = new GridBagConstraints();
gbc_label_2.fill = GridBagConstraints.HORIZONTAL;
gbc_label_2.insets = new Insets(0, 0, 5, 5);
gbc_label_2.gridx = 2;
gbc_label_2.gridy = 5;
contentPane.add(label_2, gbc_label_2);
JButton startButton = new JButton();
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
do_startButton_actionPerformed(e);
}
});
extNameField = new JTextField();
extNameField.setText("mp4");
GridBagConstraints gbc_extNameField = new GridBagConstraints();
gbc_extNameField.fill = GridBagConstraints.HORIZONTAL;
gbc_extNameField.insets = new Insets(0, 0, 5, 5);
gbc_extNameField.gridx = 3;
gbc_extNameField.gridy = 5;
contentPane.add(extNameField, gbc_extNameField);
startButton.setText("开始");
GridBagConstraints gbc_startButton = new GridBagConstraints();
gbc_startButton.anchor = GridBagConstraints.NORTH;
gbc_startButton.insets = new Insets(0, 0, 5, 0);
gbc_startButton.gridx = 4;
gbc_startButton.gridy = 5;
contentPane.add(startButton, gbc_startButton);
// JSeparator separator_2 = new JSeparator();
// GridBagConstraints gbc_separator_2 = new GridBagConstraints();
// gbc_separator_2.anchor = GridBagConstraints.NORTH;
// gbc_separator_2.fill = GridBagConstraints.HORIZONTAL;
// gbc_separator_2.insets = new Insets(0, 0, 5, 0);
// gbc_separator_2.gridwidth = 5;
// gbc_separator_2.gridx = 0;
// gbc_separator_2.gridy = 6;
// contentPane.add(separator_2, gbc_separator_2);
JScrollPane scrollPane = new JScrollPane();
GridBagConstraints gbc_scrollPane = new GridBagConstraints();
gbc_scrollPane.fill = GridBagConstraints.BOTH;
gbc_scrollPane.gridwidth = 5;
gbc_scrollPane.gridx = 0;
gbc_scrollPane.gridy = 7;
contentPane.add(scrollPane, gbc_scrollPane);
export = new JButton();
export.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
exportTable(table, new File("results.xls"));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
export.setText("导出excel");
export.setVisible(false);
GridBagConstraints gbc_export = new GridBagConstraints();
gbc_export.insets = new Insets(0, 0, 5, 0);
gbc_export.gridx = 2;
gbc_export.gridy = 6;
contentPane.add(export, gbc_export);
table = new JTable();
table.setModel(new DefaultTableModel(new Object[][] {}, new String[] {
"旧文件名", "新文件名" }));
scrollPane.setViewportView(table);
}
/**
* 浏览按钮的事件处理方法
*
* @param e
*/
protected void do_button_actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();// 创建文件选择器
// 设置只选择文件夹
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int option = chooser.showOpenDialog(this);// 显示打开对话框
if (option == JFileChooser.APPROVE_OPTION) {
dir = chooser.getSelectedFile();// 获取选择的文件夹
} else {
dir = null;
}
forderField.setText(dir + "");// 显示文件夹信息
}
/**
* 开始按钮的事件处理方法
*
* @param e
*/
protected void do_startButton_actionPerformed(ActionEvent e)
{
String templet = templetField.getText();// 获取模板字符串
if (templet.isEmpty()) {
JOptionPane.showMessageDialog(this, "请确定重命名模板", "信息对话框",
JOptionPane.WARNING_MESSAGE);
return;
}
// 获取表格数据模型
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.setRowCount(0);// 清除表格数据
int bi = (Integer) startSpinner.getValue();// 获取起始编号
int index = templet.indexOf("#");// 获取第一个“#”的索引
String code = templet.substring(index);// 获取模板中数字占位字符串
// 把模板中数字占位字符串替换为指定格式
templet = templet.replace(code, "%0" + code.length() + "d");
String extName = extNameField.getText().toLowerCase();
if (extName.indexOf(".") == -1)
extName = "." + extName;
// 获取文件中文件列表数组
File[] files = dir.listFiles(new ExtNameFileFilter(extName));
for (File file : files) {// 变量文件数组
// 格式化每个文件名称
String name = String.format(templet, bi++) + extName;
// 把文件的旧名称与新名称添加到表格的数据模型
model.addRow(new String[] { file.getName(), name });
File parentFile = file.getParentFile();// 获取文件所在文件夹对象
File newFile = new File(parentFile, name);
file.renameTo(newFile);// 文件重命名
}
export.setVisible(true);
}
public void exportTable(JTable table, File file)throws IOException
{
TableModel model = table.getModel();
FileWriter out = new FileWriter(file);
for(int i=0; i < model.getColumnCount(); i++) {
out.write(model.getColumnName(i) + "\t");
}
out.write("\n");
for(int i=0; i< model.getRowCount(); i++) {
for(int j=0; j < model.getColumnCount(); j++) {
out.write(model.getValueAt(i,j).toString()+"\t");
}
out.write("\n");
}
out.close();
System.out.println("成功导出在工程目录下,文件名: " + file);
}
}
Swing批量重命名并导出excel相关推荐
- 图片如何批量重命名?一步一步教会你
爱拍照的小伙伴应该都知道,无论是手机还是相机拍出来的照片,导入电脑后,它的默认名称都是一串长长的字符.不仅让桌面看起来杂乱无章,还会给我们在查找图片时带来诸多的不便.其实我们可以通过软件对这些图片进行 ...
- Excel文件批量重命名
顺便推荐一款重命名软件ReNamer,下载地址:https://renamer.en.softonic.com/ 目标: 将文件夹下的文件名批量更改为指定格式. 一.批量获取文件名 1. 进入文件 ...
- 使用pandas批量重命名(指定Excel中的对应列)
tags: Python Pandas Excel Tips 写在前面 今天有个朋友问我有没有一种简单的方法将Excel中的学号和姓名列作为文件名, 批量重命名100个Word文档, 下面是具体的文件 ...
- 利用EXCEL批量重命名文件
今天遇到了需要重命名多个文件的情况,在网上查找后发现两种方法还比较有效,在此做下记录. 1.软件 利用现有的软件进行批量处理,这里使用的软件是 advancedrenamer 官网下载地址,使用起来很 ...
- Python+Excel:批量重命名多个工作簿
应用场景 使用Python变成实现批量重命名文件的前提是文件名是规则的,如1月.2月.3月等,或者含有相同关键词的.如下图所示: 代码实现及注释 from pathlib import Path fo ...
- Excel如何批量重命名文件
今天跟大家分享一下如何利用Excel批量重命名文件 1.首先打开Excel软件 2.然后我们点击下图选项 3.依次点击[工作表],[重命名文件] 4.单击[选择文件],选择要处理的文件 5.然后在[新 ...
- Excel 批量重命名照片
理历史照片的时候发现,用文件夹进行分类之后,还有很多照片,如果继续分类,就会导致每个文件夹照片过少,查看不便,但是如果不分类,手机原始的命名方式没有办法满足查看需求,故而,产生了对照片进行批量重命名的 ...
- Python课程作品设计——基于EXCEL的批量重命名工具
基于EXCEL的批量重命名工具 import os,pandas,tkinter,tkinter.messagebox from tkinter import *class AR_GUI():def ...
- python 读取excel 批量 重命名文件夹
python 读取excel 批量重命名 文件夹 需求 使用的库 旧颜和新欢 过程 renamefile.py operateExcel.py 最后打包生成exe 需求 接到老婆大人说帮同事写一个 有 ...
最新文章
- SpringBoot 2.x 使用 JWT(JSON Web Token)
- AI、区块链和机器人:技术会让未来的工作发生什变化?
- 学python的总结_为什么那么多自学Python的后来都放弃了,总结起来就这些原因
- html asp textbox,ASP.NET中 TextBox 文本输入框控件的使用方法
- braintree php 开发,php – 如何为使用Braintree和静态方法调用的端点编写集成测试...
- 君威u0073故障码解决_顽疾修复过程,君威没倒挡的变速箱故障就是这样修好
- SpringBoot中快速实现邮箱发送
- php mysql 简单,你想不到的最简单php操作MySQL
- Android环绕地球动画,手机也能带你进入360°全景立体影音世界?---杜比全景声体验全接触...
- 小朋友适合读增广贤文么,增广贤文适合多大的孩子看?
- Android实现网络视频播放
- mysql周报内容范文_Mysql报表查询实例(日报|周报|月报|时间差自动计算)
- 大雨瓢泼!多地告急!告诉你雨天行车的全部秘密。
- mac 挂载 EFI 分区
- 软件测试方法和技术第一章——引论
- 从新东方讲师到AI框架工程师,我的历次职业转折|OneFlow U
- 详细说明如何配置蜻蜓Q系统的直播功能完美实现直播和观赏直播,推拉流配置完整说明一颗优雅草科技伊凡写
- MIT林达华对数学的思考
- 如何快速判断一个数是否为16的倍数
- Chrome 控制台实用指南
热门文章
- 《 ERP高级计划》书的解读之二APS算法分析之单一:内点方法(蔡颖)(转)
- 东南大学计算机视觉博士招生,基于计算机视觉的手势识别及人机交互技术的应用研究...
- VC++ 扫雷游戏的界面框架(“雷”按钮),不得不佩服那些码农
- 一加ace竞速版和一加9rt哪个值得买 两者配置对比
- Win10删除Xbox
- 累了、倦了、撑不下去了,静下心来看一看
- 多线程私有数据pthread_key_create
- HTML/CSS-花样边框案例
- 天池比赛notebook
- markdown文章发布到csdn和微信公众号