请教:JAVA中JTable中的数据导出到EXCEL中

ufm2009 2008-03-05 12:55:04
我是个JAVA初学者,现正自己试着开发一个图书管理系统小程序,

想用java实现将JTable中的数据导出到EXCEL中,想了好几天都没找到合适简便的方法。

希望各位高手能给我指教下,最好能给出具体的代码和注释。不胜感激!!!
...全文
1179 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ufm2009 2008-03-07
  • 打赏
  • 举报
回复
//设置Excel表头
private void setInfantCellTitle(HSSFSheet sheet) {
HSSFRow row = sheet.createRow(0);
setCellGBKValue(row.createCell((short) 0), "编号");
setCellGBKValue(row.createCell((short) 1), "幼儿园名称");
setCellGBKValue(row.createCell((short) 2), "园长");
setCellGBKValue(row.createCell((short) 3), "地市");
setCellGBKValue(row.createCell((short) 4), "县区");
setCellGBKValue(row.createCell((short) 5), "幼儿园地址");
setCellGBKValue(row.createCell((short) 6), "类别");
setCellGBKValue(row.createCell((short) 7), "办公电话");
setCellGBKValue(row.createCell((short) 8), "移动电话");
setCellGBKValue(row.createCell((short) 9), "Email");
setCellGBKValue(row.createCell((short) 10), "QQ");
setCellGBKValue(row.createCell((short) 11), "邮编");
setCellGBKValue(row.createCell((short) 12), "活动总人数");
setCellGBKValue(row.createCell((short) 13), "备注");
}

//----------------------------------------------------------------
这段代码中的setCellGBKValue(row.createCell((short) 0), "编号");

也是你自定义的一个方法吧?
我在调试的时候报错,但我后来看明白你这个方法的意思,就按自己的想法修改后调试成功了。
真的非常感谢你了,结贴了,呵呵!
ufm2009 2008-03-06
  • 打赏
  • 举报
回复
setInfantCellTitle(sheet);
我在调试的时候,这个语句的存在错误。
上面这条语句是什么意思?
后来我把它注释了,能够成功导出,但数据库中表中的字段名称无法在EXCEL中显示(显示空行)。
有POI的使用帮助文档吗?中文的就更好。
在此先谢了。
yiyi_wx 2008-03-06
  • 打赏
  • 举报
回复
对了,我明明登录了
可是加你好友时总提示我没登录
郁闷 等我这恢复正常了再加lz啊
^_^
yiyi_wx 2008-03-06
  • 打赏
  • 举报
回复
哦,那个是我的一个方法,因为我是截取我项目中的一部分帖上来的
所以这个方法没放上来~
setInfantCellTitle(sheet)是这样的:

//设置Excel表头
private void setInfantCellTitle(HSSFSheet sheet) {
HSSFRow row = sheet.createRow(0);
setCellGBKValue(row.createCell((short) 0), "编号");
setCellGBKValue(row.createCell((short) 1), "幼儿园名称");
setCellGBKValue(row.createCell((short) 2), "园长");
setCellGBKValue(row.createCell((short) 3), "地市");
setCellGBKValue(row.createCell((short) 4), "县区");
setCellGBKValue(row.createCell((short) 5), "幼儿园地址");
setCellGBKValue(row.createCell((short) 6), "类别");
setCellGBKValue(row.createCell((short) 7), "办公电话");
setCellGBKValue(row.createCell((short) 8), "移动电话");
setCellGBKValue(row.createCell((short) 9), "Email");
setCellGBKValue(row.createCell((short) 10), "QQ");
setCellGBKValue(row.createCell((short) 11), "邮编");
setCellGBKValue(row.createCell((short) 12), "活动总人数");
setCellGBKValue(row.createCell((short) 13), "备注");
}

其实这个就是设置表头
poi的帮助文档我也没有 不过下这个组件的时候 它自带的有一些简单的说明之类的
不过都是英文的
我之前也没接触过poi
一开始是一直在网上搜例子~lz搜搜看
不行的话再找我 共同讨论 共同进步~
yiyi_wx 2008-03-05
  • 打赏
  • 举报
回复
推荐poi组件
下面是我的一段代码:

private void createRSExcel() throws Exception {
if(!filePath.equals("nullnull")){
HSSFWorkbook workBook = new HSSFWorkbook();
HSSFSheet sheet = null;
try {
//写入各条记录,每条记录对应Excel中的一行
String tb = "";
int k = 0;
while (rs.next()) {
if (!tb.equals("幼儿园信息")) {
sheet = workBook.createSheet("幼儿园信息"); //设置表名
setInfantCellTitle(sheet);
k = 1;
} else {
k++;
}
tb = "幼儿园信息";
HSSFRow row = sheet.createRow(k);
row.createCell((short) 0).setCellValue(rs.getInt("infantId"));
row.createCell((short) 1).setCellValue(rs.getString(
"infantName"));
row.createCell((short) 2).setCellValue(rs.getString("master"));
row.createCell((short) 3).setCellValue(rs.getString("city"));
row.createCell((short) 4).setCellValue(rs.getString("country"));
row.createCell((short) 5).setCellValue(rs.getString("address"));
row.createCell((short) 6).setCellValue(rs.getInt("type"));
row.createCell((short) 7).setCellValue(rs.getString(
"publicPhone"));
row.createCell((short) 8).setCellValue(rs.getString(
"selfPhone"));
row.createCell((short) 9).setCellValue(rs.getString("Email"));
row.createCell((short) 10).setCellValue(rs.getString("QQ"));
row.createCell((short) 11).setCellValue(rs.getString("postId"));
row.createCell((short) 12).setCellValue(rs.getString(
"teacherCount"));
row.createCell((short) 13).setCellValue(rs.getString("memo"));
}
FileOutputStream fOut = new FileOutputStream(filePath);
workBook.write(fOut);
fOut.flush();
fOut.close();
JOptionPane.showMessageDialog(null, "导出成功!");
} catch (java.io.FileNotFoundException ex) {
JOptionPane.showMessageDialog(null,
"所要导出的Excel文件被占用,请关闭该文件并重试此操作!");
}
}
}

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧