JAVA关于excel文件的操作问题

代码入下,想知道如何对打开的excel文件添加按钮实现对excel内容的排序、插入、删除功能。
《outexcel类》
package Count;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.swing.JFileChooser;
import javax.swing.JTextArea;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class OutExcel {
private JTextArea area;
private String fileName;

public OutExcel(JTextArea area, String fileName) {
this.area = area;
this.fileName = fileName;
}

public void parseExcel() {
File file = new File(fileName);
String[][] result = null;
try {
result = getData(file, 1);
} catch (IOException e) {
e.printStackTrace();
}
int rowLength = result.length;
for (int i = 0; i < rowLength; i++) {
for (int j = 0; j < result[i].length; j++) {
area.append(result[i][j] + "\t\t");
}
area.append("\n");
}
}

public static String[][] getData(File file, int ignoreRows)
throws FileNotFoundException, IOException {
List result = new ArrayList();
int rowSize = 0;
BufferedInputStream in = new BufferedInputStream(new FileInputStream(
file));

POIFSFileSystem fs = new POIFSFileSystem(in);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFCell cell = null;
for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) {
HSSFSheet st = wb.getSheetAt(sheetIndex);

for (int rowIndex = ignoreRows; rowIndex <= st.getLastRowNum(); rowIndex++) {
HSSFRow row = st.getRow(rowIndex);
if (row == null) {
continue;
}
int tempRowSize = row.getLastCellNum() + 1;
if (tempRowSize > rowSize) {
rowSize = tempRowSize;
}
String[] values = new String[rowSize];
Arrays.fill(values, "");
boolean hasValue = false;
for (short columnIndex = 0; columnIndex <= row.getLastCellNum(); columnIndex++) {
String value = "";
cell = row.getCell(columnIndex);
if (cell != null) {
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
value = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
if (date != null) {
value = new SimpleDateFormat("yyyy-MM-dd")
.format(date);
} else {
value = "";
}
} else {
value = new DecimalFormat("0").format(cell
.getNumericCellValue());
}
break;
case HSSFCell.CELL_TYPE_FORMULA:

                        if (!cell.getStringCellValue().equals("")) {
                            value = cell.getStringCellValue();
                        } else {
                            value = cell.getNumericCellValue() + "";
                        }
                        break;
                    case HSSFCell.CELL_TYPE_BLANK:
                        break;
                    case HSSFCell.CELL_TYPE_ERROR:
                        value = "";
                        break;
                    case HSSFCell.CELL_TYPE_BOOLEAN:
                        value = (cell.getBooleanCellValue() == true ? "Y"
                                : "N");
                        break;
                    default:
                        value = "";
                    }
                }
                if (columnIndex == 0 && value.trim().equals("")) {
                    break;
                }
                values[columnIndex] = rightTrim(value);
                hasValue = true;
            }
            if (hasValue) {
                result.add(values);
            }
        }
    }
    in.close();
    String[][] returnArray = new String[result.size()][rowSize];
    for (int i = 0; i < returnArray.length; i++) {
        returnArray[i] = (String[]) result.get(i);
    }
    return returnArray;
}

public static String rightTrim(String str) {
    if (str == null) {
        return "";
    }
    int length = str.length();
    for (int i = length - 1; i >= 0; i--) {
        if (str.charAt(i) != 0x20) {
            break;
        }
        length--;
    }
    return str.substring(0, length);
}

}

《openexcel类》

package Count;

import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.filechooser.FileNameExtensionFilter;

public class OpenExcel extends JFrame implements ActionListener {
private JButton button;
private JButton button1;
private JButton button2;
private JScrollPane pane;
private JTextArea area;
public OpenExcel() {
super("学生管理系统");
button = new JButton("选择Excel文件");
button.addActionListener(this);
area = new JTextArea();
pane = new JScrollPane(area);
button1=new JButton("排序");

    this.add(button, BorderLayout.NORTH);


    this.add(pane);
    this.setSize(300, 300);
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
    addWindowListener(new WindowAdapter() {
           public void windowClosing(WindowEvent e) {
            int a = JOptionPane.showConfirmDialog(null, "确定关闭吗?", "温馨提示",
              JOptionPane.YES_NO_OPTION);
            if (a == 0) {  
             System.exit(0);  //关闭
            }
           } 
          });

}
// 按钮事件
public void actionPerformed(ActionEvent e) {
    JFileChooser chooser = new JFileChooser();// 文件选择对话框
    chooser.setAcceptAllFileFilterUsed(false);// 取消所有文件过滤项
    chooser.setFileFilter(new FileNameExtensionFilter("Excel文件", "xls"));// 设置只过滤扩展名为.xls的Excel文件
    int i = chooser.showOpenDialog(this);// 打开窗口
    if (i == JFileChooser.APPROVE_OPTION) {
        this.setLocation(0, 0);
        this.setSize(Toolkit.getDefaultToolkit().getScreenSize());
        new OutExcel(area, chooser.getSelectedFile().getAbsolutePath())
                .parseExcel();
    }
}
public static void main(String[] args) throws Exception {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    new OpenExcel();

}

}

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐