代码前景:实现一个可以读取Excel文件的小功能,并能通过数据去计算和值(类似功能)
![在这里插入图片描述](https://img-blog.csdnimg.cn/8d641d1e34a544feb2ebc569a38ab260.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDMxMjM3OA==,size_16,color_FFFFFF,t_70)
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
public class Main extends JFrame {
private JButton btn;
private JPanel contentPane;
private JTextField textField;
private FileOutputStream out = null;
private JTextField retrunTextField;
public Main(){
setTitle("选择文件");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 400);
contentPane=new JPanel();
contentPane.setBorder(new EmptyBorder(100,5,5,5));
contentPane.setLayout(new BorderLayout(0,0));
setContentPane(contentPane);
JPanel panel1=new JPanel();
panel1.setBounds(5,100,800,100);
JPanel panel3=new JPanel();
panel3.setBounds(5,100,800,100);
contentPane.add(panel3, BorderLayout.CENTER);
contentPane.add(panel1,BorderLayout.NORTH);
textField=new JTextField();
panel1.add(textField);
textField.setPreferredSize(new Dimension (400,40));
retrunTextField=new JTextField();
panel3.add(retrunTextField);
retrunTextField.setPreferredSize(new Dimension (400,40));
final JButton btn=new JButton("选择文件");
btn.setPreferredSize(new Dimension (100,40));
panel1.add(btn);
btn.setBackground(Color.GREEN);
final JButton btn2=new JButton("选择目录");
btn2.setPreferredSize(new Dimension (100,40));
panel3.add(btn2);
btn2.setBackground(Color.GREEN);
JPanel panel2=new JPanel();
contentPane.add(panel2, BorderLayout.SOUTH);
JButton okBtn=new JButton("确定");
okBtn.setPreferredSize(new Dimension(100,40));
panel2.add(okBtn);
setVisible(true);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(true);
int returnVal = chooser.showOpenDialog(btn);
System.out.println("returnVal="+returnVal);
if (returnVal == JFileChooser.APPROVE_OPTION) {
String filepath = chooser.getSelectedFile().getAbsolutePath();
System.out.println(filepath);
textField.setText(filepath);
}
}
});
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser jfc = new JFileChooser();
jfc.setDialogTitle("请选择要导出目录");
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int result = jfc.showOpenDialog(null);
File file = null;
if(JFileChooser.APPROVE_OPTION == result) {
file = jfc.getSelectedFile();
if (!file.isDirectory()) {
JOptionPane.showMessageDialog(null, "你选择的目录不存在");
return;
}
String path = file.getAbsolutePath();
System.out.println(path);
retrunTextField.setText(path);
}
}
});
okBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String filepath=textField.getText();
String returnFilepath=retrunTextField.getText()+"\\"+LocalDate.now()+".xlsx";
if("".equals(filepath)||filepath==null){
JOptionPane.showMessageDialog(getContentPane(), "请先选择文件~",
"警告", JOptionPane.WARNING_MESSAGE);
return;
}
String suffix=filepath.substring(filepath.lastIndexOf(".")+1);
System.out.println(suffix);
if(!(suffix.equals("xlsx")||(suffix.equals("xls")))){
JOptionPane.showMessageDialog(getContentPane(), "请选择Excel文件~",
"警告", JOptionPane.WARNING_MESSAGE);
return;
}
try {
openFile(filepath,returnFilepath);
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
}
public static String getFilePath() {
JFileChooser jf = new JFileChooser();
jf.setVisible(true);
jf.setDialogTitle("选择文件");
int result = jf.showOpenDialog(null);
if (result == JFileChooser.CANCEL_OPTION) {
return "没有选择文件";
} else {
String path = jf.getSelectedFile().getAbsolutePath();
return path;
}
}
public void openFile(String filePath,String returnFilePath) throws IOException {
File file = new File((filePath));
File returnFile = new File(returnFilePath);
FileInputStream fileInputStream = new FileInputStream(file);
System.out.println(filePath);
System.out.println(returnFilePath);
XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);
FileOutputStream fileOutputStream = new FileOutputStream(returnFile);
queryRows(workbook,fileOutputStream);
}
public static java.util.List<Row> queryRows(XSSFWorkbook workbook,FileOutputStream out){
List<Row> list=new ArrayList<>();
XSSFSheet sheet = workbook.getSheetAt(0);
for(int i=0;i<=sheet.getLastRowNum();i++){
Cell cell = sheet.getRow(i).createCell(1);
cell.setCellValue("zzzzz");
}
try {
if (out != null) {
out.flush();
}
if (workbook != null) {
workbook.write(out);
}
if (out != null) {
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
public static void main(String[] args) {
new Main();
}
}