📄 readxls.java
字号:
package com.read;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
public class ReadXls extends JFrame implements MouseListener {
private JButton jb1 = new JButton("浏览");
private JButton jb2 = new JButton("读取");
private JTextField jtf = new JTextField(30);
private JList jl = new JList();
private JFileChooser jfc = new JFileChooser();
//显示界面
public void read()
{
JPanel jp = new JPanel();
jp.add(jtf);
String[] str = {"请选择你的Excel表"};
jl.setListData(str);
jl.setVisibleRowCount(1);
JScrollPane jsp = new JScrollPane(jl);
jp.add(jsp);
jp.add(jb1);
jp.add(jb2);
this.add(jp);
this.pack();
this.setVisible(true);
jb1.addMouseListener(this);
jb2.addMouseListener(this);
}
/**
* 读取相同的Sheet方法
*/
public void readSheetBySame(String path,int sheet)
{
try {
InputStream is = new FileInputStream(path);//写入到FileInputStream
Workbook wb = Workbook.getWorkbook(is); //得到工作薄
Sheet st = wb.getSheet(sheet);//得到传过来的工作薄中的工作表
System.out.println();
} catch (Exception e){
e.printStackTrace();
}
}
/**
* 读取不相同的Sheet方法
*/
public void readSheetByDisa(String path,int sheet)
{
try {
InputStream is = new FileInputStream(path);//写入到FileInputStream
Workbook wb = Workbook.getWorkbook(is); //得到工作薄
Sheet st = wb.getSheet(sheet);//得到工作薄中的第一个工作表
int rsRows = st.getRows(); //得到excel的总行数
for(int i =0;i<rsRows;i++)
{
Cell cell1 = st.getCell(0,i);//获得第一列的值
if(cell1.getContents().equals("绝对量"))
{
for(int j = 4;j<rsRows;j++)
{
if(st.getCell(0,j).getContents().equals("增长速度"))
{
break;
}
this.findDiGui(path, sheet,j,"绝对量");
}
}
if(cell1.getContents().equals("增长速度"))
{
for(int j = 11;j<rsRows;j++)
{
if(st.getCell(0,j).getContents().equals("绝对量"))
{
break;
}
this.findDiGui(path, sheet,j,"增长速度");
}
}
}
} catch (Exception e){
e.printStackTrace();
}
}
/**
* 打印具体的数据
*/
public void findDiGui(String path,int sheet,int rowNum,String a)
{
try{
InputStream is = new FileInputStream(path);//写入到FileInputStream
Workbook wb = Workbook.getWorkbook(is); //得到工作薄
Sheet st = wb.getSheet(sheet);//得到工作薄中的第一个工作表
Cell cell0 = st.getCell(0, rowNum);
System.out.println(a+" "+cell0.getContents());
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* 为JList赋值
*/
public void fuzhi(String path)
{
try {
InputStream is = new FileInputStream(path);//写入到FileInputStream
Workbook wb = Workbook.getWorkbook(is); //得到工作薄
String[] name = wb.getSheetNames();//读取工作表的名称
jl.setListData(name);//为JList赋值
}
catch(Exception e) {
//e.printStackTrace();
System.out.println(e+"11111111");
}
}
/**
* 事件处理方法
*/
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == jb1)
{
jfc.setToolTipText("打开");
int r = jfc.showOpenDialog(this);
String path = null;
if (r == JFileChooser.APPROVE_OPTION)
{
path = jfc.getSelectedFile().getAbsolutePath();//得到文件的路
jtf.setText(path);
fuzhi(path);
}
}
if(e.getSource() == jb2)
{
this.readSheetByDisa(jtf.getText(),7);
}
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -