“如果有人有任何解决方案,请参阅下面的代码并帮助我?
在此代码中没有错误,它的工作正常,但我想按照我的要求添加代码,如: -
我有一张excel表,其中包含考试信息。 SNo,主题,主题,段落是此表的列。
我想在第一行(标题/标题)中验证Excel表格中存在的所有列,或者不表示第一行中的SNo,主题,主题,段落是否存在
我做了一些代码,比如首先浏览excels表并阅读,我也想验证第一行(Heading)请看下面的代码并帮助我: -
喜欢有人有任何解决方案吗?
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class ExcelToJTable extends JFrame {
static JTable table;
static JScrollPane scroll;
static Vector headers = new Vector();
static DefaultTableModel model = null;
static Vector data = new Vector();
static JButton jbClick;
static JFileChooser jChooser;
static int tableWidth = 0;
static int tableHeight = 0;
public ExcelToJTable() {
// TODO Auto-generated constructor stub
super("Import Excel To JTable");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(Color.white);
jChooser = new JFileChooser();
jbClick = new JButton("Select Excel File");
buttonPanel.add(jbClick, BorderLayout.CENTER);
jbClick.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
jChooser.showOpenDialog(null);
File file = jChooser.getSelectedFile();
if(!file.getName().endsWith("xls")){
JOptionPane.showMessageDialog(null, "Please Select Only Excel File.", "Error", JOptionPane.ERROR_MESSAGE);
}else {
fillData(file);
model = new DefaultTableModel(data, headers);
tableWidth = model.getColumnCount()*150;
tableHeight = model.getRowCount()*25;
table.setPreferredSize(new Dimension(tableWidth, tableHeight));
table.setModel(model);
}
}
});
table = new JTable();
table.setAutoCreateRowSorter(true);
model = new DefaultTableModel(data, headers);
table.setModel(model);
table.setBackground(Color.pink);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setEnabled(false);
table.setRowHeight(25);
table.setRowMargin(4);
tableWidth = model.getColumnCount()*150;
tableHeight = model.getRowCount()*25;
table.setPreferredSize(new Dimension(tableWidth, tableHeight));
scroll = new JScrollPane(table);
scroll.setBackground(Color.pink);
scroll.setPreferredSize(new Dimension(300, 300));
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
getContentPane().add(buttonPanel, BorderLayout.NORTH);
getContentPane().add(scroll, BorderLayout.CENTER);
setSize(600, 600);
setResizable(true);
setVisible(true);
}
void fillData(File file) {
Workbook workbook = null;
try {
try {
workbook = Workbook.getWorkbook(file);
} catch (IOException ex) {
Logger.getLogger(ExcelToJTable.class.getName()).log(
Level.SEVERE, null, ex);
}
Sheet sheet = workbook.getSheet(0);
headers.clear();
for (int i = 0; i < sheet.getColumns(); i++) {
Cell cell1 = sheet.getCell(i, 0);
headers.add(cell1.getContents());
}
data.clear();
for (int j = 1; j < sheet.getRows(); j++) {
Vector d = new Vector();
for (int i = 0; i < sheet.getColumns(); i++) {
Cell cell = sheet.getCell(i, j);
d.add(cell.getContents());
}
d.add("\n");
data.add(d);
}
} catch (BiffException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new ExcelToJTable();
}
}