分享一个自己做的poi工具类,写不是很完全,足够我自己当前使用,有兴趣的可以自行扩展
1 import org.apache.commons.lang3.exception.ExceptionUtils; 2 import org.apache.poi.hssf.usermodel.HSSFDataFormat; 3 import org.apache.poi.hssf.usermodel.HSSFSheet; 4 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 5 import org.apache.poi.hssf.util.CellReference; 6 import org.apache.poi.ss.usermodel.*; 7 import org.apache.poi.ss.util.CellRangeAddress; 8 import org.apache.poi.ss.util.RegionUtil; 9 import org.apache.poi.xssf.usermodel.XSSFWorkbook; 10 import java.io.File; 11 import java.io.FileInputStream; 12 import java.io.FileOutputStream; 13 import java.io.IOException; 14 import java.util.List; 15 16 public class ExcelWriterUtil_Poi { 17 18 19 // 用于存放结果表内容的xlsx格式的工作簿 20 private XSSFWorkbook xssfWorkbook = null; 21 // 用于存放结果表内容的xls格式的工作簿 22 private HSSFWorkbook hssfWorkbook = null; 23 // 工作的sheet页 24 private Sheet sheet; 25 // 用于读取用例表内容复制到结果标的文件输出流 26 private FileOutputStream stream = null; 27 // 用于存储结果表的路径的成员变量,便于在保存结果时进行判断 28 private String path = null; 29 // 单元格格式 30 private CellStyle style = null; 31 // 表的总行数 32 public int rows = 0; 33 private String sheetName="Sheet1";//初始化默认给一个sheet名字 34 private FileInputStream in =null; 35 private String resultType=null; 36 37 /** 38 * 获取当前操作sheet的名称 39 * @return 40 */ 41 public String getSheetName() { 42 43 return sheetName; 44 } 45 46 /** 47 * 根据名字切换sheet进行操作 // 指定工作sheet 48 * 49 * @param sheetName 50 */ 51 public void useSheet(String sheetName) { 52 53 this.sheetName = sheetName; 54 try { 55 if (resultType.equals(".xlsx")) { 56 sheet = xssfWorkbook.getSheet(getSheetName()); 57 if (sheet == null) { 58 return; 59 } 60 61 } else if (resultType.equals(".xls")) { 62 sheet = hssfWorkbook.getSheet(getSheetName()); 63 if (sheet == null) { 64 return; 65 } 66 } 67 rows = sheet.getPhysicalNumberOfRows(); 68 69 sheet.setForceFormulaRecalculation(true); //刷新公式 70 71 } catch (Exception e) { 72 System.out.println(ExceptionUtils.getStackTrace(e)); 73 } 74 75 76 } 77 78 /** 79 * 修改当前sheet的名称 80 * @param sheetName 81 */ 82 public void updateSheetName(String sheetName){ 83 try { 84 if (resultType.equals(".xlsx")) { 85 sheet = xssfWorkbook.getSheet(getSheetName()); 86 if(sheet == null) { 87 return; 88 } 89 int indexSheet = xssfWorkbook.getSheetIndex(sheet); 90 xssfWorkbook.setSheetName(indexSheet,sheetName); 91 92 }else if(resultType.equals(".xls")){ 93 sheet = hssfWorkbook.getSheet(getSheetName()); 94 if(sheet == null) { 95 return; 96 } 97 int indexSheet = hssfWorkbook.getSheetIndex(sheet); 98 hssfWorkbook.setSheetName(indexSheet,sheetName); 99 }100 }catch (Exception e){101 System.out.println(ExceptionUtils.getStackTrace(e));102 }103 }104 105 /**106 * 关闭文件输入流107 */108 public void closeStream() {109 try {110 in.close();111 } catch (IOException e) {112 // TODO Auto-generated catch block113 e.printStackTrace();114 }115 }116 /*117 * 根据模板 path1,创建path2,将path1中的内容复制到path2中118 * @param path1模板表路径119 * path2新生表路径120 */121 public ExcelWriterUtil_Poi(String path1, String path2) {122 // 截取模板表后缀名123 String Origintype = path1.substring(path1.lastIndexOf("."));124 // 判断是xls还是xlsx格式,完成在内存中创建模板表的工作簿125 XSSFWorkbook xssfWorkbookRead = null;126 HSSFWorkbook hssfWorkbookRead = null;127 if (Origintype.equals(".xlsx")) {128 try {129 xssfWorkbookRead = new XSSFWorkbook(new File(path1));130 } catch (Exception e) {131 System.out.println(ExceptionUtils.getStackTrace(e));132 }133 }134 if (Origintype.equals(".xls")) {135 try {136 hssfWorkbookRead = new HSSFWorkbook(new FileInputStream(new File(path1)));137 } catch (Exception e) {138 System.out.println(ExceptionUtils.getStackTrace(e));139 }140 }141 // 如果两种格式均不符合,则文件打开失败142 if (xssfWorkbookRead == null && hssfWorkbookRead == null) {143 System.out.println("Excel文件打开失败!");144 return;145 }146 147 // 截取结果表后缀名148 resultType = path2.substring(path2.lastIndexOf("."));149 // 确定结果表格式为excel格式150 if (resultType.equals(".xlsx") || resultType.equals(".xls")) {151 try {152 // 根据新生表的文件名,为该文件在内存中开辟空间153 File file = new File(path2);154 try {155 // 在磁盘上面创建该文件156 file.createNewFile();157 } catch (Exception e1) {158 // 创建失败,提示路径非法,并停止创建159 System.out.println(ExceptionUtils.getStackTrace(e1));160 return;161 }162 // 基于新生表,创建文件输出流stream163 stream = new FileOutputStream(file);164 // 将用例表中的内容写入文件输出流stream165 if (hssfWorkbookRead != null) {166 hssfWorkbookRead.write(stream);167 // 关闭用例表在内存中的副本168 hssfWorkbookRead.close();169 } else {170 xssfWorkbookRead.write(stream);171 xssfWorkbookRead.close();172 }173 // 关闭已经写入了用例表内容的文件流174 stream.close();175 // 基于新生表,创建文件输入流176 in = new FileInputStream(file);177 // 判断结果文件的后缀是03版还是07版excel178 if (resultType.equals(".xlsx")) {179 try {180 //通过文件输入流,在内存中创建结果表的工作簿181 xssfWorkbook = new XSSFWorkbook(in);182 System.out.println(getSheetName());183 sheet = xssfWorkbook.getSheet(getSheetName());184 185 } catch (Exception e) {186 System.out.println(ExceptionUtils.getStackTrace(e));187 }188 }189 if (resultType.equals(".xls")) {190 try {191 hssfWorkbook = new HSSFWorkbook(in);192 sheet = hssfWorkbook.getSheet(getSheetName());193 } catch (Exception e) {194 System.out.println(ExceptionUtils.getStackTrace(e));195 }196 }197 rows = sheet.getPhysicalNumberOfRows();198 //将成员变量结果文件路径赋值为path2,表示结果表已经成功创建。199 path = path2;200 201 } catch (Exception e) {202 System.out.println( ExceptionUtils.getStackTrace(e));203 }204 } else {205 System.out.println("写入的文件格式错误!");206 }207 }208 209 /**210 * 创建指定名称的sheet211 * @param sheetName212 */213 public void createSheet(String sheetName){214 if(xssfWorkbook != null){215 sheet=xssfWorkbook.createSheet(sheetName);216 }else if(hssfWorkbook!=null){217 sheet=hssfWorkbook.createSheet(sheetName);218 }219 rows = sheet.getPhysicalNumberOfRows();220 }221 222 223 224 // 设置样式为Excel中指定单元格的样式225 public void setStyle(int rowNo, int column) {226 Row row = null;227 Cell cell = null;228 try {229 style= xssfWorkbook.createCellStyle();230 style.setVerticalAlignment( VerticalAlignment.CENTER);231 style.setAlignment(HorizontalAlignment.CENTER);232 233 } catch (Exception e) {234 e.printStackTrace();235 }236 }237 238 /*239 * 当用例执行结果失败时,使用该方法,以红色字体写入excel240 * @param r单元格行数241 * l单元格列数242 * value输入值243 * size字体大小244 * con是否加粗245 * fontStyle字体类型246 */247 public void writeFailCell(int rowNo, int column, String value,int size,boolean con,String fontStyle) {248 if(fontStyle==null||"".equals(fontStyle)){249 font;250 }251 Row row = null;252 try {253 // 获取指定行254 row = sheet.getRow(rowNo);255 } catch (Exception e) {256 e.printStackTrace();257 }258 // 行不存在,则创建259 if (row == null) {260 row = sheet.createRow(rowNo);261 }262 // 在该行,新建指定列的单元格263 Cell cell = row.createCell(column);264 // 设置单元格值265 cell.setCellValue(value);266 // 设置单元格样式267 CellStyle failStyle = null;268 // 新建字体样式269 Font font = null;270 // 根据不同的excel版本进行实例化271 if (hssfWorkbook != null) {272 font = hssfWorkbook.createFont();273 failStyle = hssfWorkbook.createCellStyle();274 } else {275 font = xssfWorkbook.createFont();276 failStyle = xssfWorkbook.createCellStyle();277 }278 failStyle.setVerticalAlignment( VerticalAlignment.CENTER);279 failStyle.setAlignment(HorizontalAlignment.CENTER);280 failStyle.setBord.........