Java操作Excel(二)将数据库中的数据导出到Excel中
2012-03-07 17:05
656 查看
public void exportToExcel() {
try {
ArrayList list = this.getSelectedCourseStudentsTableData();
System.out.println("=============size:" + list.size());
if (list != null && list.size() > 0) {
Long tp = new Long(System.currentTimeMillis());
String path = getApplicationBean().getFilePath()
+ "printfiles/" + tp.toString() + ".xls";
String name=path.substring(path.lastIndexOf('/')+1,path.length());
OutputStream outf = new FileOutputStream(path);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("StudentCource");
sheet.setColumnWidth((short)0,(short)4000); //设置单元格的长度
sheet.setColumnWidth((short)3,(short)4000);
sheet.setColumnWidth((short)5,(short)6000);
sheet.setColumnWidth((short)6,(short)6000);
sheet.setHorizontallyCenter(true);
HSSFRow row1 = sheet.createRow(0);
HSSFCell cell1 = row1.createCell((short) 0);
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置编码
cell1.setCellValue("学号");
cell1 = row1.createCell((short) 1);
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置编码
cell1.setCellValue("姓名");
cell1 = row1.createCell((short) 2);
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置编码
cell1.setCellValue("性别");
cell1 = row1.createCell((short) 3);
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置编码
cell1.setCellValue("学生类别");
cell1 = row1.createCell((short) 4);
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置编码
cell1.setCellValue("年级");
cell1 = row1.createCell((short) 5);
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置编码
cell1.setCellValue("所属学院");
cell1 = row1.createCell((short) 6);
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置编码
cell1.setCellValue("所学专业");
ArrayList selectedCourseStudentslist = this
.getSelectedCourseStudentsTableData();
for (int i = 0; i < selectedCourseStudentslist.size(); i++) {
HSSFRow row = sheet.createRow(i+1);
PgtStatusInfo pgtStatusInfo = (PgtStatusInfo) selectedCourseStudentslist
.get(i);
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue(pgtStatusInfo.getStudentNo());
cell = row.createCell((short) 1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(pgtStatusInfo.getName());
cell = row.createCell((short) 2);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(pgtStatusInfo.getSexCn());
cell = row.createCell((short) 3);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(pgtStatusInfo.getPgtTypeCn());
cell = row.createCell((short) 4);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(pgtStatusInfo.getGradeCn());
cell = row.createCell((short) 5);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(pgtStatusInfo.getBelongDeptCn());
cell = row.createCell((short) 6);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(pgtStatusInfo.getSpeCn());
}
wb.write(outf);
outf.close();
this.excelLink ="./files/printfiles/" + name;
this.excelFileLinkVisible = true;
this.outputFileLinkVisible = false;
} else {
getApplicationBean().addMessage(FacesMessage.SEVERITY_INFO,
"导出excel内容为空!", null);
}
} catch (Exception e) {
getApplicationBean().addMessage(FacesMessage.SEVERITY_ERROR,
"导出excel操作失败!", e.getMessage());
}
}
try {
ArrayList list = this.getSelectedCourseStudentsTableData();
System.out.println("=============size:" + list.size());
if (list != null && list.size() > 0) {
Long tp = new Long(System.currentTimeMillis());
String path = getApplicationBean().getFilePath()
+ "printfiles/" + tp.toString() + ".xls";
String name=path.substring(path.lastIndexOf('/')+1,path.length());
OutputStream outf = new FileOutputStream(path);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("StudentCource");
sheet.setColumnWidth((short)0,(short)4000); //设置单元格的长度
sheet.setColumnWidth((short)3,(short)4000);
sheet.setColumnWidth((short)5,(short)6000);
sheet.setColumnWidth((short)6,(short)6000);
sheet.setHorizontallyCenter(true);
HSSFRow row1 = sheet.createRow(0);
HSSFCell cell1 = row1.createCell((short) 0);
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置编码
cell1.setCellValue("学号");
cell1 = row1.createCell((short) 1);
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置编码
cell1.setCellValue("姓名");
cell1 = row1.createCell((short) 2);
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置编码
cell1.setCellValue("性别");
cell1 = row1.createCell((short) 3);
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置编码
cell1.setCellValue("学生类别");
cell1 = row1.createCell((short) 4);
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置编码
cell1.setCellValue("年级");
cell1 = row1.createCell((short) 5);
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置编码
cell1.setCellValue("所属学院");
cell1 = row1.createCell((short) 6);
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置编码
cell1.setCellValue("所学专业");
ArrayList selectedCourseStudentslist = this
.getSelectedCourseStudentsTableData();
for (int i = 0; i < selectedCourseStudentslist.size(); i++) {
HSSFRow row = sheet.createRow(i+1);
PgtStatusInfo pgtStatusInfo = (PgtStatusInfo) selectedCourseStudentslist
.get(i);
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue(pgtStatusInfo.getStudentNo());
cell = row.createCell((short) 1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(pgtStatusInfo.getName());
cell = row.createCell((short) 2);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(pgtStatusInfo.getSexCn());
cell = row.createCell((short) 3);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(pgtStatusInfo.getPgtTypeCn());
cell = row.createCell((short) 4);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(pgtStatusInfo.getGradeCn());
cell = row.createCell((short) 5);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(pgtStatusInfo.getBelongDeptCn());
cell = row.createCell((short) 6);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(pgtStatusInfo.getSpeCn());
}
wb.write(outf);
outf.close();
this.excelLink ="./files/printfiles/" + name;
this.excelFileLinkVisible = true;
this.outputFileLinkVisible = false;
} else {
getApplicationBean().addMessage(FacesMessage.SEVERITY_INFO,
"导出excel内容为空!", null);
}
} catch (Exception e) {
getApplicationBean().addMessage(FacesMessage.SEVERITY_ERROR,
"导出excel操作失败!", e.getMessage());
}
}
相关文章推荐
- java 对excel操作 读取、写入、修改数据;导出数据库数据到excel
- Java操作Excel(二)将数据库中的数据导出到Excel中
- java实现数据库数据导出到Excel并下载查看
- java导出数据库中Excel表格数据的方法
- Java程序员从笨鸟到菜鸟之(一百零五)java操作office和pdf文件(三)利用jxl实现数据导出excel报表以及与POI的区别
- java将数据库里的数据导出到excel(转)
- java、Extjs导出数据库里的数据至Excel并下载至本地进行数据备份方法之二——使用Action
- 利用java操作Excel表格(把Excel中的数据导入数据库中)
- PHP将操作数据库得到的数据生成Excel导出
- java操作Excel之POI(4)利用POI实现数据的批量导出
- Java通过POI技术操作Excel(3)----数据导出
- Java程序员从笨鸟到菜鸟之(一百零五)java操作office和pdf文件(三)利用jxl实现数据导出excel报表以及与POI的区别
- java 数据库 对象 导出Excel数据
- java中使用JXL对Excel文件进行数据的写入、导出操作
- JAVA实现数据库数据导入/导出到Excel(POI)
- java读取数据库数据并导出到EXCEL中
- Java一次导出百万条数据生成excel(web操作)
- java 数据库数据导出到EXCEL
- java操作excel之jxls导出excel模板数据
- java poi实现数据库数据导出到excel