用apache poi导出数据到Excel
版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://tangyajun.blog.51cto.com/177086/77392 |
public class ExPortExcel {
private ResultSet rs;
private String strSql; public ExPortExcel() {
}
public ExPortExcel(ResultSet rs, String strSql) {
this.rs=rs; this.strSql=strSql; } /**
* @return */ public ResultSet getRs() { return rs; } /**
* @return */ public String getStrSql() { return strSql; } /**
* @param set */ public void setRs(ResultSet set) { rs = set; } /**
* @param string */ public void setStrSql(String string) { strSql = string; } //导出数据到Excel public void getExcel(HttpServletRequest req, HttpServletResponse res, ExPortExcel excel) { //HSSFRichTextString title = new HSSFRichTextString("用户信息"); DataDAO db = new DataDAO();
ResultSet rs = db.getData(excel.strSql); // 创建Excel 文件 HSSFWorkbook workbook = new HSSFWorkbook(); // 创建工作区 HSSFSheet sheet = workbook.createSheet(); // 创建行对象 HSSFRow row_title = sheet.createRow(0); // 创建标题的字体 // HSSFFont title_font = workbook.createFont(); // // 设置标题字体属性 // title_font.setFontHeightInPoints((short) 8); // // 设置标题字体属性 // title_font.setFontHeight((short) HSSFFont.BOLDWEIGHT_NORMAL); // // 设置标题字体的颜色 // title_font.setColor((short) HSSFFont.BOLDWEIGHT_NORMAL); // // 创建样式 // HSSFCellStyle title_style = workbook.createCellStyle(); // // 设置标题样式属性 // title_style.setFont(title_font); // // 创建单元格对象 // HSSFCell cell_title = row_title.createCell((short) 1); // // 设置单元格样式 // cell_title.setCellStyle(title_style); // // 将标题写到Excel表格中 // cell_title.setCellValue(title); ArrayList list = new ArrayList();
try { // 得到元数据 ResultSetMetaData rsmd = rs.getMetaData(); for (int j = 1; j <= rsmd.getColumnCount(); j++) { // 得到列名 list.add(rsmd.getColumnName(j)); } } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } String titles[] = { "用户ID", "用户名", "联系电话", "地址" };
// 创建行
HSSFRow row = sheet.createRow((short)0); // 创建样式 HSSFCellStyle items_style = workbook.createCellStyle(); // 设置表头样式 items_style.setAlignment((short) HSSFCellStyle.ALIGN_CENTER); // 创建字体 HSSFFont celltbnamefont = workbook.createFont(); // 设置表头字体属性 celltbnamefont.setFontHeightInPoints((short) 8); // 设置表头字体属性 celltbnamefont.setColor((short) HSSFFont.COLOR_RED); // 设置表头字体属性 items_style.setFont(celltbnamefont); items_style.setWrapText(true);
for (int i = 0; i < list.size(); i++) { HSSFCell cell = row.createCell((short) i);
sheet.setColumnWidth((short) i, (short) 5335);
cell.setCellStyle(items_style);
cell.setCellValue(list.get(i).toString());
} HSSFCellStyle datestyle = workbook.createCellStyle();
HSSFDataFormat df = workbook.createDataFormat(); datestyle.setDataFormat(df.getFormat("yyyy-mm-dd")); int i = 0; try { while (rs.next()) { // 创建行 HSSFRow datarow = sheet.createRow((short) i + 1); // 创建样式 HSSFCellStyle data_style = workbook.createCellStyle(); // 创建字体 HSSFFont data_font = workbook.createFont(); // 设置字体大小 data_font.setFontHeightInPoints((short)10); // 设置字体颜色 data_font.setColor((short) HSSFFont.COLOR_NORMAL); // 设置样式居中 data_style.setAlignment((short) HSSFCellStyle.ALIGN_CENTER); data_style.setFont(data_font);
data_style.setWrapText(true); for (int j = 1; j < 5; j++) {
HSSFCell cell = datarow.createCell((short) (j - 1)); //设置单元格中的数据类型为String cell.setCellType(HSSFCell.CELL_TYPE_STRING); //设置单元格的字符编码为utf cell.setEncoding(HSSFCell.ENCODING_UTF_16); String data = rs.getString(j); cell.setCellStyle(data_style); cell.setCellValue(data); } i++; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } FileOutputStream out; OutputStream os; String fileName = "userinfo.xls"; try { // res.setContentType("application/x-msdownload"); res.setHeader("Content-Disposition", "attachment;" + " filename=" + new String(fileName.getBytes(), "ISO-8859-1")); os = res.getOutputStream(); workbook.write(os); os.close(); } catch (FileNotFoundException e) {
// TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 本文出自 “城市猎人” 博客,请务必保留此出处http://tangyajun.blog.51cto.com/177086/77392 本文出自 51CTO.COM技术博客 |


tangyajun168
博客统计信息
热门文章
最新评论
友情链接