javaexcel单元格斜线_POI实现excel单元格画斜线//⼯具类
public class PoiUtil {
public static final int PERCENT_WIDTH = 50;
public static final int PERCENT_HEIGHT = 20;
public static final float PXTOPT = 0.75f;
// draw cell line
/**
* @param sheet 页签
* @param i ⾏
* @param j 列
* @param width 单元格宽度
* @param height 单元格⾼度
*/
public static void drawLine(HSSFSheet sheet, int i, int j, int width, int height) {
int cellWidth = (int) (PERCENT_WIDTH * PXTOPT * width);
short cellHeight = (short) (PERCENT_HEIGHT * PXTOPT * height);
HSSFPatriarch patriarch = ateDrawingPatriarch();
HSSFClientAnchor a = new HSSFClientAnchor(0, 0, 1023, 255, (short) j, i, (short) (j), i);
HSSFShapeGroup group = ateGroup(a);
float verticalPointsPerPixel = a.getAnchorHeightInPoints(sheet);
EscherGraphics g = new EscherGraphics(group, Workbook(), Color.black,
verticalPointsPerPixel);
EscherGraphics2d g2d = new EscherGraphics2d(g);
int[] xys = {width, height};
for (int l = 0; l < xys.length; l += 2) {
int x = (int) ((PERCENT_WIDTH * 0.75 * xys[l] / cellWidth) * 1023);
int y = (int) ((PERCENT_HEIGHT * 0.75 * xys[l + 1] / cellHeight) * 255);
//g2d.drawLine(0, 0, x, y); 左上右下
g2d.drawLine(0, y, x, 0);
}
}
public static HSSFCellStyle getCellFormat(HSSFWorkbook wb) {
HSSFCellStyle cellStyle = wb.createCellStyle();
if (BorderBottom() != HSSFCellStyle.BORDER_THIN) {
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
}
if (BorderLeft() != HSSFCellStyle.BORDER_THIN) {
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
}
if (BorderTop() != HSSFCellStyle.BORDER_THIN) {
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
}
if (BorderRight() != HSSFCellStyle.BORDER_THIN) {
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
}
cellStyle.setBottomBorderColor(createPette(wb));
cellStyle.setLeftBorderColor(createPette(wb));
cellStyle.setRightBorderColor(createPette(wb));
cellStyle.setTopBorderColor(createPette(wb));
return cellStyle;
}
public static short createPette(HSSFWorkbook wb) {
short petteIndex = 0;
Color rgb = new Color(0x00, 0x00, 0x00);
HSSFPalette palette = wb.getCustomPalette();
palette.setColorAtIndex(petteIndex, (byte) Red(), (byte) Green(), (byte) rgb
return petteIndex;
}
//调⽤地:
//sheet0操作页签,i⾏号,j列号,cell0当前单元格(通过row0获得),row0当前⾏(通过sheet0获得) PoiUtil.drawLine(sheet0, i, j, ColumnIndex()), Height());效果:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论