62,623
社区成员
发帖
与我相关
我的任务
分享
//设置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), "备注");
}
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文件被占用,请关闭该文件并重试此操作!");
}
}
}