1. 判断指定的单元格是否是合并单元格
/** * 功能:判断指定的单元格是否是合并单元格 * 原理:excel中的合并单元格其实就是首单元格,只不过该单元格增加了 rowspan和colspan两个属性。 * 并且把该单元格的高度和宽度变成(例如rowspan=2,colspan=3)2行,3列的的标尺长度,再把被合并的单元格(除了首单元格的其余5个单元格)隐藏。 * @param wsheet * @param row * @param column * @return */ private Integer isMergedRegion(Sheet wsheet, int row, int column) { //getNumMergedRegions:得到所有区域 for(int i = 0;i < wsheet.getNumMergedRegions();i++){ //根据索引获取指定的合并单元区域 CellRangeAddress range = wsheet.getMergedRegion(i); int firstColumn = range.getFirstColumn(); int lasColumn = range.getLastColumn(); int firstRow = range.getFirstRow(); int lastRow = range.getLastRow(); if(row >= firstRow && row <= lastRow){ if(column >= firstColumn && column <= lasColumn){ return i; } } } return -1; }
2. 插入图片
public void addPictrue(String imgPath,HSSFSheet sheet, HSSFWorkbook wb){ BufferedImage bufferImg = null; //先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray try { ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream(); bufferImg = ImageIO.read(new File(imgPath)); //这里要注意,第二个参数将会决定插入图片形式,如果是一个png的图片,背景透明,但是此处设置为jpg格式将会自动添加黑色背景 ImageIO.write(bufferImg, "png", byteArrayOut); //画图的顶级管理器,一个sheet只能获取一个 HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); //anchor主要用于设置图片的属性 //dx1、dy1、dx2、dy2、col1,row1,col2,row2 HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 255, 255,(short) 9, 0, (short) 11, 0); anchor.setAnchorType(2); //插入图片 patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG)); } catch (Exception e) { e.printStackTrace(); }finally{ } }
3. 合并单元格
//Test of Merging Row row = firstSheet.createRow(1); Cell cell = row.createCell(1); cell.setCellValue("test of merging"); //firstRow、lastRow、firstCol、lastCol firstSheet.addMergedRegion(new CellRangeAddress(1,1,1,4));
4.
//设置生成Excel中公式自动计算 wsheet.setForceFormulaRecalculation(true);
5.
//获取所有合并单元格的个数 sheet.getNumMergedRegions();
6.
//获取指定索引的合并区域 region = sheet.getMergedRegion(i);
7.
//拷贝合并区域 CellRangeAddress newRegion = region.copy();
8.
//设置合并区域的首行、首列、最后行、最后列 newRegion.setFirstRow(targetRowFrom); newRegion.setFirstColumn(region.getFirstColumn()); newRegion.setLastRow(targetRowTo); newRegion.setLastColumn(region.getLastColumn());
9.
//向工作簿中添加合并区域 sheet.addMergedRegion(newRegion);
10.
//获取当前行的最后列索引 columnCount = sourceRow.getLastCellNum();
11. 设置合并单元格样式
/** * 功能:设置合并单元格样式 * @param cellStyle * @param rangeAddress * @param wsheet */ private void setRegionStyle(CellStyle cellStyle, CellRangeAddress rangeAddress, Sheet wsheet) { for(int i = rangeAddress.getFirstRow();i <= rangeAddress.getLastRow();i++){ Row row = wsheet.getRow(i); if(null == row){ row = wsheet.createRow(i); } for(int j = rangeAddress.getFirstColumn();j <= rangeAddress.getLastColumn();j++){ Cell cell = row.getCell(j); if(null == cell){ cell = row.createCell(j); cell.setCellValue(""); } cell.setCellStyle(cellStyle); } } }
12. 转换坐标(B3)为行列值(row=4、col=1)
//得到列 字母 String lineStr = ""; String reg = "[A-Z]+"; Pattern p = Pattern.compile(reg); Matcher m = p.matcher(point); while (m.find()) { lineStr = m.group(); } //将列字母转成列号 根据ASCII转换 char[] ch = lineStr.toCharArray(); int column = 0; for (int i = 0; i < ch.length; i++) { char c = ch[i]; int post = ch.length - i - 1; int r = (int) Math.pow(10, post); column = column + r * ((int) c - 65); } tempCell.setColumn(column); //得到行号 reg = "[1-9]\d*"; p = Pattern.compile(reg); m = p.matcher(point); while (m.find()) { tempCell.setRow((Integer.parseInt(m.group()) - 1)); }
13.
啦啦啦