使用java程序生成excel文档程序
来源:互联网 发布:太极拳实战 知乎 编辑:程序博客网 时间:2024/06/11 17:29
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);// 设置表格默认列宽度为15sheet.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包在,请下载放在类路径下
点击打开链接
- 使用java程序生成excel文档程序
- Java生成Excel文件通用程序
- Java生成Excel文件通用程序
- web程序生成excel
- Web程序导出Excel文档
- 用java程序导出数据到Excel文档
- Java生成EXCEL文档说明
- 程序开发文档生成利器
- 使用doxygen为C/C++程序生成中文文档
- 使用doxyen为C程序生成中文文档
- 使用doxygen为C/C++程序生成中文文档
- 使用doxygen为C/C++程序生成中文文档
- 使用doxygen为C/C++程序生成中文文档
- 使用doxygen为C/C++程序生成中文文档
- [转]使用.net 程序生成 Excel 和Access 文件
- [转]使用.net 程序生成 Excel 和Access 文件
- [转]使用.net 程序生成 Excel 和Access 文件
- 使用.net 程序生成 Excel 和Access 文件 .
- 简简单单,自己动手管理系统启动项
- 每日总结:DataTable的Select()方法、DataView的RowsFilter属性
- Unix 高级环境编程 关于原子操作
- QT界面切换 绿色背景
- 以ontouch为例说明android事件发送机制
- 使用java程序生成excel文档程序
- RPM的使用
- makefile详解
- 还亲力亲为的蜡笔小新
- xml
- Uva 11995 - I Can Guess the Data Structure!
- 匪警请拨110,即使手机欠费也可拨通!为了保障社会秩序,保护人民群众生命财产安全,警察叔叔需要与罪犯斗智斗勇,
- DM3730 DSPLink在Linux下的环境搭建
- android中动态实现全屏和动态退出全屏方法