本文主要是介绍Java读取excel表格添加到Jtable中,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
- 1,本文在查看了网上一些猿友的代码!本猿用了30分钟终于调试出来了一个可以运行的程序,程序写的比价粗糙
-
-
- import javax.swing.*;
import java.awt.*;
import javax.swing.table.DefaultTableModel;
import java.io.File;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.Cell;
import jxl.CellType;
import jxl.LabelCell;
import jxl.NumberCell;
import jxl.Sheet;
import jxl.Workbook;
import java.io.*;
import jxl.read.biff.BiffException;
public class Exceltest extends JFrame{
DefaultTableModel model=new DefaultTableModel();
JScrollPane scrollPane = new JScrollPane();
JTable table;
public Exceltest(){
setLayout(new BorderLayout());
model = new DefaultTableModel(new Object[][] {{ null, null, null, null },
{ null, null, null, null } },
new String[] {"分类号","资产名称","规格","型号","单价","数量","总金额",
});
//model.setRowCount(0);// 清除原有行
table=new JTable(model);
table.setRowHeight(20);
JScrollPane sp=new JScrollPane(table);
add(sp,BorderLayout.CENTER);
read();
setTitle("审核信息");
setSize(1000, 600);
setVisible(true);
}
public void read() {
Object[] object = new Object[table.getColumnCount()];
try {
Workbook book = Workbook.getWorkbook(new File("res.xls"));
//获得第一个工作表对象
Sheet sheet = book.getSheet(0);
//得到第一列第一行的单元格
int rows = sheet.getRows();
int column = sheet.getColumns();
for (int i=0;i<rows;i++){
for (int j=0;j<column;j++){
/// Cell cell1 = sheet.getCell(j, i);
// String result = cell1.getContents();
// System.out.print(result+",");
Cell c00 = sheet.getCell(j,i);
if(c00.getType() == CellType.LABEL)
{
LabelCell labelc00 = (LabelCell)c00;
object[j] = labelc00.getString();
//jTable.setValueAt(labelc00.getString(), i-1, j);
}else if(c00.getType() == CellType.NUMBER)
{
Double numd;
int numi;
NumberCell numc10 = (NumberCell)c00;
numd = new Double(numc10.getValue());
numi = numd.intValue();
object[j] = numi;
// jTable.setValueAt(numi, i-1, j);
}
}
model.addRow(object);
System.out.println(object[1]+" "+object[3]+" "+object[4]+" "+object[5]+" "+object[6]+" "+object[12]);
System.out.println("-----------------------------");
}
book.close();
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String args[]){
new Exceltest();
}
}
本文系本人原创,转载请标明出处
这篇关于Java读取excel表格添加到Jtable中的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!