使用java程序生成excel文档程序
2013-03-26 23:09
423 查看
package com.itheima.utils; import java.beans.PropertyDescriptor; import java.io.IOException; import java.io.OutputStream; import java.text.SimpleDateFormat; import java.util.Collection; import java.util.Date; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFRichTextString; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; public class ExportExcelUtils { /** * @param title:excel文件名称 * @param headers:表头 * @param dataset:要保存到表格的对象集合 * @param attrList:对象要保存的属性 * @param out:输出目的地 */ public static void exportExcel(String title, String[] headers, Collection dataset,String attrList[], OutputStream out) { // 声明一个工作薄 Workbook workbook = new HSSFWorkbook(); // 生成一个表格 Sheet sheet = workbook.createSheet(title); // 设置表格默认列宽度为15 sheet.setDefaultColumnWidth((short) 15); // 产生表头 Row row = sheet.createRow(0); //插入表头数据 for (short i = 0; i < headers.length; i++) { Cell cell = row.createCell(i); //设置样式 cell.setCellStyle(createHeaderStyle(workbook)); HSSFRichTextString text = new HSSFRichTextString(headers[i]); cell.setCellValue(text); } int index = 1; for(Object bean : dataset){ //得到一个bean,则生成表格的一行 row = sheet.createRow(index++); for(int i=0;i<attrList.length;i++){ PropertyDescriptor pd = null; try{ pd = new PropertyDescriptor(attrList[i],bean.getClass()); }catch (Exception e) { throw new RuntimeException("bean中没有属性:" + attrList[i]); } //得到bean的属性值 Object attrValue = null; try { attrValue = pd.getReadMethod().invoke(bean, null); } catch (Exception e) { throw new RuntimeException("无法获取bean的属性值:" + pd.getName()); } //转成字符串 String cellValue = ""; if(attrValue instanceof Date){ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); df.format(attrValue); }else{ if(attrValue!=null){ cellValue = attrValue.toString(); } } Cell cell = row.createCell(i); cell.setCellStyle(createDataStyle(workbook)); cell.setCellValue(cellValue); } } try { workbook.write(out); } catch (IOException e) { throw new RuntimeException(e); } } private static CellStyle createHeaderStyle(Workbook workbook){ // 生成一个样式 CellStyle style = workbook.createCellStyle(); // 设置表头的样式 style.setFillForegroundColor(HSSFColor.BLUE.index); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setBorderTop(HSSFCellStyle.BORDER_THIN); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 生成表头的字体 Font font = workbook.createFont(); font.setColor(HSSFColor.VIOLET.index); font.setFontHeightInPoints((short) 12); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 把字体应用到当前的样式 style.setFont(font); return style; } private static CellStyle createDataStyle(Workbook workbook){ // 生成数据行的样式 CellStyle style = workbook.createCellStyle(); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setBorderTop(HSSFCellStyle.BORDER_THIN); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 生成数据行的字体 Font font = workbook.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); // 把字体应用到当前的样式 style.setFont(font); return style; } }
所需的jar包在,请下载放在类路径下
点击打开链接
相关文章推荐
- 使用开源项目JExcelApi在Java环境中生成MS Excel文档
- 使用JavaDoc生成Java程序的使用文档
- 发现一段特别好的使用 POI生成Excel文档的 java代码
- Java学习笔记--使用Javadoc生成程序开发文档
- 使用doxygen为C/C++程序生成中文文档(上)
- NPOI 2.1.1 系列(2) 使用NPOI读取List或者datatable数据生成 Excel文档 ;Npoi生成 xlsx 2007以上文档
- 使用doxygen为C/C++程序生成中文文档(上)
- ETL工具kettle与JAVA结合使用程序生成转换
- JXL包大解析;Java程序生成excel文件和解析excel文件内容
- 使用Java程序片段动态生成表格
- 使用.net 程序生成 Excel 和Access 文件 .
- java使用iText生成pdf文档的对齐方式
- 使用doxygen为C/C++程序生成中文文档
- 使用java生成word文档
- 使用Spring MVC生成Excel文档 .
- 使用doxygen为C/C++程序生成中文文档(上)
- Java使用poi包读取Excel文档
- java使用POI工具读取word文档并写入生成新的word文档及错误整理
- 如何使用Java程序打开一个Word文档?
- 使用JXL生成Excel时发生java.lang.ArrayIndexOutOfBoundsException错误