Aapache POI 电子表格操作工具类
package www.dazhi360.com;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.usermodel.HSSFDataFormat;import org.apache.poi.hssf.usermodel.HSSFFont;import org.apache.poi.hssf.usermodel.HSSFHyperlink;import org.apache.poi.hssf.usermodel.HSSFRichTextString;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.hssf.util.CellRangeAddress;import org.apache.poi.hssf.util.HSSFColor;/** * 为此类用于将统计数据以电子表格的格式导出 * @author Frank * {@link <a href="http://www.dazhi360.com/">大智360企业培训网</a>} */public class ExcelUtil {/** * 将Workbook对象写入到一个文件中 * @param wb Workbook对象实例 * @param filepath 文件的绝对路径 */public static void exportWorkbook(HSSFWorkbook wb,String filepath){FileOutputStream fos = null;try { fos = new FileOutputStream(filepath);wb.write(fos);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally{try {if(fos!=null){ fos.close();}} catch (IOException e) {e.printStackTrace();}}}/** * 设置某个单元格的值 * @param cell 单元格对象 * @param value 需要设置的值 */public static void setCellStringValue(HSSFCell cell,String value){cell.setCellValue(new HSSFRichTextString(value));}/** * 合并单元格 * @param sheet sheet对象 * @param fristRow 起始行 * @param lastRow 结束行 * @param firstCol 起始列 * @param lastCol 结束列 */public static void mergeRegion(HSSFSheet sheet,int fristRow,int lastRow,int firstCol,int lastCol){sheet.addMergedRegion(new CellRangeAddress(fristRow,lastRow,firstCol,lastCol));}/** * 获取简单的横向对齐格式样式 * @param align 横向对齐方式 * @return 样式对象 */public static HSSFCellStyle getStyle(HSSFWorkbook wb,int align){HSSFCellStyle style = wb.createCellStyle();style.setAlignment((short)align);return style;}/** * 获取简单包含横向和纵向对齐的样式对象 * @param wb workbook对象 * @param align 横向对齐方式 * @param verticalAlign 纵向对齐方式 * @return 样式对象 */public static HSSFCellStyle getStyle(HSSFWorkbook wb,int align,int verticalAlign){HSSFCellStyle style = getStyle(wb, align);style.setVerticalAlignment((short)verticalAlign);return style;}/** * 创建一个单元格样式 * @param wb 表格文本对象 * @param align 横向对齐方式 * @param verticalAlign 纵向对齐方式 * @param fontName 字体名称 * @param height 高度 * @param fontWeight 字体粗细值 * @return 字体对象 */public static HSSFCellStyle getStyle(HSSFWorkbook wb,int align,int verticalAlign,String fontName,int height,int fontWeight){HSSFCellStyle style = getStyle(wb, align, verticalAlign);//创建字体HSSFFont font = wb.createFont();font.setFontName(fontName);font.setFontHeight((short)fontWeight);font.setBoldweight((short)fontWeight);//设置字体style.setFont(font);return style;}/** * 获取单元格样式 * @param wb 工作表格 * @param align 横向对齐方式 * @param warpText 是否自动换行 * @param hidden 内容超出是否隐藏 * @return 单元格样式对象 */public static HSSFCellStyle getStyle(HSSFWorkbook wb,short align,boolean warpText,boolean hidden){HSSFCellStyle style = wb.createCellStyle();style.setAlignment(align);style.setWrapText(warpText);style.setHidden(hidden);return style;}public static HSSFCellStyle getStyle(HSSFWorkbook wb,short align,String timeFormat){HSSFCellStyle style = wb.createCellStyle();style.setAlignment(align);//创建数据格式对象HSSFDataFormat format = wb.createDataFormat();//设置时间格式style.setDataFormat(format.getFormat(timeFormat));return style;}/** * 获取一个链接样式 * @param book * @param align * @return */public static HSSFCellStyle getLinkStyle(HSSFWorkbook book,short align){HSSFCellStyle style = book.createCellStyle();style.setAlignment(align);//创建下划线风格和颜色字体HSSFFont font = book.createFont();font.setUnderline(HSSFFont.U_NONE);font.setColor(HSSFColor.BLUE.index);//设置字体style.setFont(font);return style;}/** * 创建一个文档链接 * @return */public static HSSFHyperlink getDocumentHyperLink(String targetSheetName){HSSFHyperlink link = new HSSFHyperlink(HSSFHyperlink.LINK_DOCUMENT);link.setAddress(targetSheetName+"!A1");return link;}/** * 获取电子表格内部普通url链接对象 * @param url 链接目标地址 * @return 链接对象实例 */public static HSSFHyperlink getUrlLink(String url){HSSFHyperlink link = new HSSFHyperlink(HSSFHyperlink.LINK_URL);link.setAddress(url);return link;}}?