看,一个小例子。超简单。
import javaxswingJFrame;
import javaxswingJTable;
public class TestDriver extends JFrame{
private JTable table;
public TestDriver(String s){
super(s);
thistable = new JTable(5,5);
thisadd(table);
thispack();
thissetVisible(true);
thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
}
public static void main(String[] args){
TestDriver t = new TestDriver("这是一个表格");
}
}
从数据库读出数据
然后用JSP页面显示出来
形成一个表格
------解决方案--------------------
后台数据放在List,传到前台。
前台用jtsl的foreach
或者struts2的iterator遍历出来,也可以采用java的代码<%%>。
首先这个可以用Java实现,如果要实现类似这样的效果需要引入Java Excel API类库,它是开源的用来 *** 作Excel电子表格的Java类库,支持读取,修改,写入等 *** 作。这个项目基于GPL发布,而且对中文有很好的支持。对于里面合并行,合并列就需要根据逻辑代码调用具体方法来实现了。
1类库地址下载
jExcelApi 的 JavaDoc:
http://jexcelapisourceforgenet/resources/javadocs/indexhtml
2具体实现
一、需要导入的jar
1commons-collections4-41jar
2poi-317-beta1jar
3poi-ooxml-317-beta1jar
4poi-ooxml-schemas-317-beta1jar
5xmlbeans-260jar
二、主要API
1import orgapachepoissusermodelWorkbook,对应Excel文档;
2import orgapachepoihssfusermodelHSSFWorkbook,对应xls格式的Excel文档;
3import orgapachepoixssfusermodelXSSFWorkbook,对应xlsx格式的Excel文档;
4import orgapachepoissusermodelSheet,对应Excel文档中的一个sheet;
5import orgapachepoissusermodelRow,对应一个sheet中的一行;
6import orgapachepoissusermodelCell,对应一个单元格。
还有合并行,合并列,行背景色,加粗,一般table有的样式,基本都具有。
java中表格的删除是通过事件监控来实现的,示例代码如下:
import javaawteventActionEvent;
import javaawteventActionListener;
import javaawteventMouseAdapter;
import javaawteventMouseEvent;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingJLabel;
import javaxswingJPanel;
import javaxswingJScrollPane;
import javaxswingJTable;
import javaxswingJTextField;
import javaxswingListSelectionModel;
import javaxswingtableDefaultTableModel;
//维护表格
public class JTableDefaultTableModelTest extends JFrame{
private DefaultTableModel tableModel; //表格模型对象
private JTable table;
private JTextField aTextField;
private JTextField bTextField;
public JTableDefaultTableModelTest()
{
super();
setTitle("表格");
setBounds(100,100,500,400);
setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
String[] columnNames = {"A","B"}; //列名
String [][]tableVales={{"A1","B1"},{"A2","B2"},{"A3","B3"},{"A4","B4"},{"A5","B5"}}; //数据
tableModel = new DefaultTableModel(tableVales,columnNames);
table = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(table); //支持滚动
getContentPane()add(scrollPane,BorderLayoutCENTER);
//jdk16
//排序:
//tablesetRowSorter(new TableRowSorter(tableModel));
tablesetSelectionMode(ListSelectionModelSINGLE_SELECTION); //单选
tableaddMouseListener(new MouseAdapter(){ //鼠标事件
public void mouseClicked(MouseEvent e){
int selectedRow = tablegetSelectedRow(); //获得选中行索引
Object oa = tableModelgetValueAt(selectedRow, 0);
Object ob = tableModelgetValueAt(selectedRow, 1);
aTextFieldsetText(oatoString()); //给文本框赋值
bTextFieldsetText(obtoString());
}
});
scrollPanesetViewportView(table);
final JPanel panel = new JPanel();
getContentPane()add(panel,BorderLayoutSOUTH);
paneladd(new JLabel("A: "));
aTextField = new JTextField("A4",10);
paneladd(aTextField);
paneladd(new JLabel("B: "));
bTextField = new JTextField("B4",10);
paneladd(bTextField);
final JButton addButton = new JButton("添加"); //添加按钮
addButtonaddActionListener(new ActionListener(){//添加事件
public void actionPerformed(ActionEvent e){
String []rowValues = {aTextFieldgetText(),bTextFieldgetText()};
tableModeladdRow(rowValues); //添加一行
int rowCount = tablegetRowCount() +1; //行数加上1
aTextFieldsetText("A"+rowCount);
bTextFieldsetText("B"+rowCount);
}
});
paneladd(addButton);
final JButton updateButton = new JButton("修改"); //修改按钮
updateButtonaddActionListener(new ActionListener(){//添加事件
public void actionPerformed(ActionEvent e){
int selectedRow = tablegetSelectedRow();//获得选中行的索引
if(selectedRow!= -1) //是否存在选中行
{
//修改指定的值:
tableModelsetValueAt(aTextFieldgetText(), selectedRow, 0);
tableModelsetValueAt(bTextFieldgetText(), selectedRow, 1);
//tablesetValueAt(arg0, arg1, arg2)
}
}
});
paneladd(updateButton);
final JButton delButton = new JButton("删除");
delButtonaddActionListener(new ActionListener(){//添加事件
public void actionPerformed(ActionEvent e){
int selectedRow = tablegetSelectedRow();//获得选中行的索引
if(selectedRow!=-1) //存在选中行
{
tableModelremoveRow(selectedRow); //删除行
}
}
});
paneladd(delButton);
}
/
@param args
/
public static void main(String[] args) {
// TODO Auto-generated method stub
JTableDefaultTableModelTest jTableDefaultTableModelTest = new JTableDefaultTableModelTest();
jTableDefaultTableModelTestsetVisible(true);
}
}
项目结构:
xls:
\\\
XlsMain java 类
//该类有main方法,主要负责运行程序,同时该类中也包含了用poi读取Excel(2003版)
import javaioFileInputStream;
import javaioIOException;
import javaioInputStream;
import javautilArrayList;
import javautilList;
import orgapachepoihssfusermodelHSSFCell;
import orgapachepoihssfusermodelHSSFRow;
import orgapachepoihssfusermodelHSSFSheet;
import orgapachepoihssfusermodelHSSFWorkbook;
/
@author Hongten</br>
参考地址:http://hao0610iteyecom/blog/1160678
/
public class XlsMain {
public static void main(String[] args) throws IOException {
XlsMain xlsMain = new XlsMain();
XlsDto xls = null;
List<XlsDto> list = xlsMainreadXls();
try {
XlsDto2ExcelxlsDto2Excel(list);
} catch (Exception e) {
eprintStackTrace();
}
for (int i = 0; i < listsize(); i++) {
xls = (XlsDto) listget(i);
Systemoutprintln(xlsgetXh() + " " + xlsgetXm() + " "
+ xlsgetYxsmc() + " " + xlsgetKcm() + " "
+ xlsgetCj());
}
}
/
读取xls文件内容
@return List<XlsDto>对象
@throws IOException
输入/输出(i/o)异常
/
private List<XlsDto> readXls() throws IOException {
InputStream is = new FileInputStream("pldrxkxxmbxls");
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
XlsDto xlsDto = null;
List<XlsDto> list = new ArrayList<XlsDto>();
// 循环工作表Sheet
for (int numSheet = 0; numSheet < hssfWorkbookgetNumberOfSheets(); numSheet++) {
HSSFSheet hssfSheet = hssfWorkbookgetSheetAt(numSheet);
if (hssfSheet == null) {
continue;
}
// 循环行Row
for (int rowNum = 1; rowNum <= hssfSheetgetLastRowNum(); rowNum++) {
HSSFRow hssfRow = hssfSheetgetRow(rowNum);
if (hssfRow == null) {
continue;
}
xlsDto = new XlsDto();
// 循环列Cell
// 0学号 1姓名 2学院 3课程名 4 成绩
// for (int cellNum = 0; cellNum <=4; cellNum++) {
HSSFCell xh = hssfRowgetCell(0);
if (xh == null) {
continue;
}
xlsDtosetXh(getValue(xh));
HSSFCell xm = hssfRowgetCell(1);
if (xm == null) {
continue;
}
xlsDtosetXm(getValue(xm));
HSSFCell yxsmc = hssfRowgetCell(2);
if (yxsmc == null) {
continue;
}
xlsDtosetYxsmc(getValue(yxsmc));
HSSFCell kcm = hssfRowgetCell(3);
if (kcm == null) {
continue;
}
xlsDtosetKcm(getValue(kcm));
HSSFCell cj = hssfRowgetCell(4);
if (cj == null) {
continue;
}
xlsDtosetCj(FloatparseFloat(getValue(cj)));
listadd(xlsDto);
}
}
return list;
}
/
得到Excel表中的值
@param hssfCell
Excel中的每一个格子
@return Excel中每一个格子中的值
/
@SuppressWarnings("static-access")
private String getValue(HSSFCell hssfCell) {
if (hssfCellgetCellType() == hssfCellCELL_TYPE_BOOLEAN) {
// 返回布尔类型的值
return StringvalueOf(hssfCellgetBooleanCellValue());
} else if (hssfCellgetCellType() == hssfCellCELL_TYPE_NUMERIC) {
// 返回数值类型的值
return StringvalueOf(hssfCellgetNumericCellValue());
} else {
// 返回字符串类型的值
return StringvalueOf(hssfCellgetStringCellValue());
}
}
}
XlsDto2Exceljava类
//该类主要负责向Excel(2003版)中插入数据
import javaioFileOutputStream;
import javaioOutputStream;
import javautilList;
import orgapachepoihssfusermodelHSSFCell;
import orgapachepoihssfusermodelHSSFRichTextString;
import orgapachepoihssfusermodelHSSFRow;
import orgapachepoihssfusermodelHSSFSheet;
import orgapachepoihssfusermodelHSSFWorkbook;
public class XlsDto2Excel {
/
@param xls
XlsDto实体类的一个对象
@throws Exception
在导入Excel的过程中抛出异常
/
public static void xlsDto2Excel(List<XlsDto> xls) throws Exception {
// 获取总列数
int CountColumnNum = xlssize();
// 创建Excel文档
HSSFWorkbook hwb = new HSSFWorkbook();
XlsDto xlsDto = null;
// sheet 对应一个工作页
HSSFSheet sheet = hwbcreateSheet("pldrxkxxmb");
HSSFRow firstrow = sheetcreateRow(0); // 下标为0的行开始
HSSFCell[] firstcell = new HSSFCell[CountColumnNum];
String[] names = new String[CountColumnNum];
names[0] = "学号";
names[1] = "姓名";
names[2] = "学院";
names[3] = "课程名";
names[4] = "成绩";
for (int j = 0; j < CountColumnNum; j++) {
firstcell[j] = firstrowcreateCell(j);
firstcell[j]setCellValue(new HSSFRichTextString(names[j]));
}
for (int i = 0; i < xlssize(); i++) {
// 创建一行
HSSFRow row = sheetcreateRow(i + 1);
// 得到要插入的每一条记录
xlsDto = xlsget(i);
for (int colu = 0; colu <= 4; colu++) {
// 在一行内循环
HSSFCell xh = rowcreateCell(0);
xhsetCellValue(xlsDtogetXh());
HSSFCell xm = rowcreateCell(1);
xmsetCellValue(xlsDtogetXm());
HSSFCell yxsmc = rowcreateCell(2);
yxsmcsetCellValue(xlsDtogetYxsmc());
HSSFCell kcm = rowcreateCell(3);
kcmsetCellValue(xlsDtogetKcm());
HSSFCell cj = rowcreateCell(4);
cjsetCellValue(xlsDtogetCj());
(xlsDtogetMessage());
}
}
// 创建文件输出流,准备输出电子表格
OutputStream out = new FileOutputStream("POI2Excel/pldrxkxxmbxls");
hwbwrite(out);
outclose();
Systemoutprintln("数据库导出成功");
}
}
XlsDto java类
//该类是一个实体类
public class XlsDto {
/
选课号
/
private Integer xkh;
/
学号
/
private String xh;
/
姓名
/
private String xm;
/
学院
/
private String yxsmc;
/
课程号
/
private Integer kch;
/
课程名
/
private String kcm;
/
成绩
/
private float cj;
public Integer getXkh() {
return xkh;
}
public void setXkh(Integer xkh) {
thisxkh = xkh;
}
public String getXh() {
return xh;
}
public void setXh(String xh) {
thisxh = xh;
}
public String getXm() {
return xm;
}
public void setXm(String xm) {
thisxm = xm;
}
public String getYxsmc() {
return yxsmc;
}
public void setYxsmc(String yxsmc) {
thisyxsmc = yxsmc;
}
public Integer getKch() {
return kch;
}
public void setKch(Integer kch) {
thiskch = kch;
}
public String getKcm() {
return kcm;
}
public void setKcm(String kcm) {
thiskcm = kcm;
}
public float getCj() {
return cj;
}
public void setCj(float cj) {
thiscj = cj;
}
}
后台输出:
数据库导出成功
10 hongten 信息技术学院 计算机网络应用基础 800
20 王五 信息技术学院 计算机网络应用基础 810
30 李胜基 信息技术学院 计算机网络应用基础 820
40 五班古 信息技术学院 计算机网络应用基础 830
50 蔡诗芸 信息技术学院 计算机网络应用基础 840
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)