问题:
考虑到有这样一个文本
需要将文本格式 .txt,输出成 Excel 表能打开的 .xls 格式
.
展示
点击转换到桌面或者转换到原文件夹,而后生成同名的.xls文件。
过程
核心代码是处理将文本拿到的数据转换成Excel认识的格式。
我们需要用到apache的 com.springsource.org.apache.poi-3.0.2.FINAL.jar 包中的HSSFWorkbook 类来处理Excel。
HSSFWorkbook 的最基本基本用法。
//exlel写操作
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet(name);
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow(0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
实现代码如下
核心处理 Test类
public static void change(File file,String newPath) throws IOException{
HSSFWorkbook wb = new HSSFWorkbook();// 建立新HSSFWorkbook对象
HSSFSheet sheet = wb.createSheet("new sheet");// 建立新的sheet对象
HSSFRow row = sheet.createRow((short) 0);// 建立新行
HSSFCell cell = row.createCell((short) 0);
FileReader fin = new FileReader(file);
FileOutputStream fos =new FileOutputStream(newPath);
int i, rownum = 0, charnum = 0;
String str="";
try {
while ((i = fin.read()) != -1) {
str=str+String.valueOf((char)i);
if (str.endsWith(",")||str.endsWith(",")) {
charnum++;
cell = row.createCell((short)charnum );
str="";
} else if (str.endsWith("\n")) {
rownum++;
row = sheet.createRow((short) rownum);// 建立新行
charnum=0;
cell = row.createCell((short)charnum );
str="";
}
cell.setCellValue(str);
}
wb.write(fos);
} catch (IOException e) {
e.printStackTrace();
} finally {
fos.close();
fin.close();
}
}
用到Java的swing做一个简单的图形界面。
把选择要改变的文件路径和改变后的文件路径传给Test类处理。
ChooserFream 类
public class ChooserFream implements ActionListener{
JFrame frame=new JFrame("文件选择器");
JTabbedPane tabPane=new JTabbedPane();//选项卡布局
Container con=new Container();//布局1
JLabel label1=new JLabel("选择目录或文件");
JLabel labelSuccess=new JLabel("");
JTextField text1=new JTextField();
JButton button1=new JButton("选择");
JButton button2=new JButton("转换到原文件夹");
JButton button3=new JButton("转换到桌面");
JFileChooser jfc=new JFileChooser();//文件选择器
File file=null;
String newPath=null;
FileSystemView fsv = FileSystemView.getFileSystemView();
File com=fsv.getHomeDirectory(); //这便是读取桌面路径的方法了
public ChooserFream(){
jfc.setCurrentDirectory(com);//文件选择器的初始目录定为桌面
//下面两行是取得屏幕的高度和宽度
double lx=Toolkit.getDefaultToolkit().getScreenSize().getWidth();
double ly=Toolkit.getDefaultToolkit().getScreenSize().getHeight();
frame.setLocation(new Point((int)(lx/2)-150,(int)(ly/2)-150));//设定窗口出现位置
frame.setSize(400,200);//设定窗口大小
frame.setContentPane(tabPane);//设置布局
//下面设定标签等的出现位置和高宽
label1.setBounds(10,10,100,20);
text1.setBounds(120,10,160,20);
button1.setBounds(300,10,60,20);
button2.setBounds(10,40,140,20);
button3.setBounds(170,40,140,20);
labelSuccess.setBounds(20,70,100,20);
button1.addActionListener(this);//添加事件处理
button2.addActionListener(this);
button3.addActionListener(this);
con.add(label1);
con.add(text1);
con.add(button1);
con.add(button2);
con.add(button3);
con.add(labelSuccess);
con.add(jfc);
tabPane.add("目录/文件选择",con);//添加布局1
frame.setVisible(true);//窗口可见
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//使能关闭窗口,结束程序
}
public void actionPerformed(ActionEvent e){//事件处理
if(e.getSource().equals(button1)){//判断触发方法的按钮是哪个
//jfc.setFileSelectionMode(1);//设定只能选择到文件夹
// jfc.setFileSelectionMode(0);设定只能选择到文件
int state=jfc.showOpenDialog(null);//此句是打开文件选择器界面的触发语句
if(state==1){
return;//撤销则返回
}
else{
file=jfc.getSelectedFile();//f为选择到的目录
text1.setText(file.getAbsolutePath());
}
}
if(e.getSource().equals(button2)){//转换到原文件夹
try {
newPath=file.getAbsolutePath();
newPath=newPath.replace(".txt", ".xls");
new Test1().change(file,newPath);
labelSuccess.setText("修改成功");
} catch (IOException e1) {
e1.printStackTrace();
}
}
if(e.getSource().equals(button3)){//转换到桌面
try {System.out.println("asfadadas");
String newName=file.getName().replace(".txt", ".xls");
newPath=com.getAbsolutePath()+"\\"+newName;
System.out.println(newPath);
new Test1().change(file,newPath);
labelSuccess.setText("修改成功");
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
public static void main(String[] args) {
new ChooserFream();
}
}
jar用exe4j包打包成.exe文件
成功实现后打包成.exe文件。
具体我就不写了,参考下这位兄弟的代码吧。
https://blog.csdn.net/com_it/article/details/77771754
那exe4j是需要注册码的
A-XVK258563F-1p4lv7mg7sav
A-XVK209982F-1y0i3h4ywx2h1
A-XVK267351F-dpurrhnyarva
A-XVK204432F-1kkoilo1jy2h3r
A-XVK246130F-1l7msieqiwqnq
A-XVK249554F-pllh351kcke50
A-XVK238729F-25yn13iea25i
A-XVK222711F-134h5ta8yxbm0
A-XVK275016F-15wjjcbn4tpj
A-XVK275016F-15wjjcbn4tpj