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);
excel表格斜线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
.getBlue());
return petteIndex;
}
//调⽤地:
//sheet0操作页签,i⾏号,j列号,cell0当前单元格(通过row0获得),row0当前⾏(通过sheet0获得) PoiUtil.drawLine(sheet0, i, j, ColumnIndex()), Height());效果:
发布评论