jxl在excel中插入形状并在形状上写字 在线等待

a761208 2007-07-17 02:43:19
如题
我想在excel中插入一个菱形
并且要在菱形里面写入文字
网上几乎没有找到任何答案

如果不行只好将文字写在图片上面
然后插入图片了

在线等待
...全文
732 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Blacklab 2010-01-13
  • 打赏
  • 举报
回复
hhhhhhhhhhhhh  out
a761208 2007-07-24
  • 打赏
  • 举报
回复
/**
* 绘制一个菱形 如果参数全部为NULL则绘制一个标准的菱形200*140
*
* @param p1
* @param p2
* @param p3
* @param p4
* @return
*/
public static byte[] drawDiamond(Point p1, Point p2, Point p3, Point p4,
String s) {
int width = 200;
int height = 120;
if (p1 == null && p2 == null && p3 == null && p4 == null) {
p1 = new Point(width / 2, 1);
p2 = new Point(width - 1, height / 2);
p3 = new Point(width / 2, height - 1);
p4 = new Point(1, height / 2);
}
// File file = new File("src.jpg");
// Image src = javax.imageio.ImageIO.read(file);
BufferedImage b = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = b.getGraphics();
// Polygon p = new Polygon();
// g.drawImage(src, 0, 0, null);
g.setColor(new Color(255, 255, 255));
g.fillRect(0, 0, width, height);
g.setColor(new Color(0, 0, 0));
Graphics2D gg = (Graphics2D) g;
gg.setStroke(new BasicStroke(2.5F, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND));
// 2.0F是笔的粗细
// CAP_ROUND是线条端点
// JOIN_ROUND是点划线模式
gg.drawLine(p1.x, p1.y, p2.x, p2.y);
gg.drawLine(p2.x, p2.y, p3.x, p3.y);
gg.drawLine(p3.x, p3.y, p4.x, p4.y);
gg.drawLine(p4.x, p4.y, p1.x, p1.y);
// 设回原来的
gg.setStroke(new BasicStroke());
g.setColor(new Color(0, 0, 0));
String[] ss = s.split(" ");
int x = 0;
int y = 40;
for (int i = 0; i < ss.length; i++) {
String curStr = ss[i];
Font font = new Font(null, Font.PLAIN, 12);
FontRenderContext frc = gg.getFontRenderContext();
double w = font.getStringBounds(curStr, frc).getWidth();
// 自动断行
// 如果本次截取长度小于80,并且增加以后长度小于80
if (i < ss.length - 2) {
if (w < 80) {
curStr += " " + ss[i + 1];
w = font.getStringBounds(curStr, frc).getWidth();
if (w < 80) {
i++;
} else {
curStr = ss[i];
}
}
}
w = font.getStringBounds(curStr, frc).getWidth();
// double h = font.getStringBounds(curStr, frc).getHeight();
x = (int) ((width - w) / 2);
g.drawString(curStr, x, y);
y += 20;
}
PngEncoderB png = new PngEncoderB();
png.setImage(b);
png.setEncodeAlpha(false);
png.setFilter(PngEncoder.FILTER_NONE);
png.setCompressionLevel(0);
byte[] pngbytes = png.pngEncode();
return pngbytes;
/*
* FileOutputStream outfile = new FileOutputStream("test.png"); if
* (pngbytes == null) System.out.println("Null image"); else
* outfile.write(pngbytes); outfile.flush(); outfile.close();
*/
}
a761208 2007-07-24
  • 打赏
  • 举报
回复
似乎JXL确实不能实现这个功能
我现在的做法是生成一个图片
然后在图片上画的图形,并添上文字
难道JXL真的不如POI强大?
稽姬 2007-07-18
  • 打赏
  • 举报
回复
poi 的 绘图 看看能不能帮你
稽姬 2007-07-18
  • 打赏
  • 举报
回复
private static void drawPolygon(HSSFPatriarch patriarch) {
// HSSFClientAnchor a = new HSSFClientAnchor( 0, 0, 1023, 255, (short) 2, 2, (short) 3, 3 );
// HSSFPolygon p = patriarch.createPolygon(a);
// p.setPolygonDrawArea(100,100);
// p.setPoints( new int[]{30, 90, 50}, new int[]{88, 5, 44} );


HSSFClientAnchor a = new HSSFClientAnchor();
a.setAnchor((short) 2, 2, 0, 0, (short) 3, 3, 1023, 255);
HSSFShapeGroup g = patriarch.createGroup(a);
g.setCoordinates(0, 0, 200, 200);
HSSFPolygon p1 = g.createPolygon(new HSSFChildAnchor(0, 0, 200, 200));
p1.setPolygonDrawArea(100, 100);
p1.setPoints(new int[] {0, 90, 50}, new int[] {5, 5, 44});
p1.setFillColor(0, 255, 0);
HSSFPolygon p2 = g.createPolygon(new HSSFChildAnchor(20, 20, 200, 200));
p2.setPolygonDrawArea(200, 200);
p2.setPoints(new int[] {120, 20, 150}, new int[] {105, 30, 195});
p2.setFillColor(255, 0, 0);
}

private static void drawManyLines(HSSFPatriarch patriarch) {
// Draw bunch of lines
int x1 = 100;
int y1 = 100;
int x2 = 800;
int y2 = 200;
int color = 0;
for (int i = 0; i < 10; i++) {
HSSFClientAnchor a2 = new HSSFClientAnchor();
a2.setAnchor((short) 2, 2, x1, y1, (short) 2, 2, x2, y2);
HSSFSimpleShape shape2 = patriarch.createSimpleShape(a2);
shape2.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
shape2.setLineStyleColor(color);
y1 -= 10;
y2 -= 10;
color += 30;
}
}

private static void drawGrid(HSSFPatriarch patriarch) {
// This draws a grid of lines. Since the coordinates space fixed at
// 1024 by 256 we use a ratio to get a reasonably square grids.

double xRatio = 3.22;
double yRatio = 0.6711;

int x1 = 000;
int y1 = 000;
int x2 = 000;
int y2 = 200;
for (int i = 0; i < 20; i++) {
HSSFClientAnchor a2 = new HSSFClientAnchor();
a2.setAnchor((short) 2, 2, (int) (x1 * xRatio), (int) (y1 * yRatio),
(short) 2, 2, (int) (x2 * xRatio), (int) (y2 * yRatio));
HSSFSimpleShape shape2 = patriarch.createSimpleShape(a2);
shape2.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);

x1 += 10;
x2 += 10;
}

x1 = 000;
y1 = 000;
x2 = 200;
y2 = 000;
for (int i = 0; i < 20; i++) {
HSSFClientAnchor a2 = new HSSFClientAnchor();
a2.setAnchor((short) 2, 2, (int) (x1 * xRatio), (int) (y1 * yRatio),
(short) 2, 2, (int) (x2 * xRatio), (int) (y2 * yRatio));
HSSFSimpleShape shape2 = patriarch.createSimpleShape(a2);
shape2.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);

y1 += 10;
y2 += 10;
}
}

private static void drawLinesToCenter(HSSFPatriarch patriarch) {
// Draw some lines from and to the corners
{
HSSFClientAnchor a1 = new HSSFClientAnchor();
a1.setAnchor((short) 2, 2, 0, 0, (short) 2, 2, 512, 128);
HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1);
shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
}
{
HSSFClientAnchor a1 = new HSSFClientAnchor();
a1.setAnchor((short) 2, 2, 512, 128, (short) 2, 2, 1024, 0);
HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1);
shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
}
{
HSSFClientAnchor a1 = new HSSFClientAnchor();
a1.setAnchor((short) 1, 1, 0, 0, (short) 1, 1, 512, 100);
HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1);
shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
}
{
HSSFClientAnchor a1 = new HSSFClientAnchor();
a1.setAnchor((short) 1, 1, 512, 100, (short) 1, 1, 1024, 0);
HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1);
shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
}

}
}
稽姬 2007-07-18
  • 打赏
  • 举报
回复
public class OfficeDrawing {
public static void main(String[] args) throws IOException {
// Create the workbook and sheets.
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
HSSFSheet sheet2 = wb.createSheet("second sheet");
HSSFSheet sheet3 = wb.createSheet("third sheet");
HSSFSheet sheet4 = wb.createSheet("fourth sheet");

// Draw stuff in them
drawSheet1(sheet1);
drawSheet2(sheet2);
drawSheet3(sheet3);
drawSheet4(sheet4, wb);

// Write the file out.
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
}

private static void drawSheet1(HSSFSheet sheet1) {
// Create a row and size one of the cells reasonably large.
HSSFRow row = sheet1.createRow(2);
row.setHeight((short) 2800);
sheet1.setColumnWidth((short) 2, (short) 9000);

// Create the drawing patriarch. This is the top level container for
// all shapes.
HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();

// Draw some lines and an oval.
drawLinesToCenter(patriarch);
drawManyLines(patriarch);
drawOval(patriarch);
drawPolygon(patriarch);

// Draw a rectangle.
HSSFSimpleShape rect = patriarch.createSimpleShape(new HSSFClientAnchor(
100, 100, 900, 200, (short) 0, 0, (short) 0, 0));
rect.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE);
}

private static void drawSheet2(HSSFSheet sheet2) {
// Create a row and size one of the cells reasonably large.
HSSFRow row = sheet2.createRow(2);
row.setHeightInPoints(240);
sheet2.setColumnWidth((short) 2, (short) 9000);

// Create the drawing patriarch. This is the top level container for
// all shapes. This will clear out any existing shapes for that sheet.
HSSFPatriarch patriarch = sheet2.createDrawingPatriarch();

// Draw a grid in one of the cells.
drawGrid(patriarch);
}

private static void drawSheet3(HSSFSheet sheet3) {
// Create a row and size one of the cells reasonably large
HSSFRow row = sheet3.createRow(2);
row.setHeightInPoints(140);
sheet3.setColumnWidth((short) 2, (short) 9000);

// Create the drawing patriarch. This is the top level container for
// all shapes. This will clear out any existing shapes for that sheet.
HSSFPatriarch patriarch = sheet3.createDrawingPatriarch();

// Create a shape group.
HSSFShapeGroup group = patriarch.createGroup(
new HSSFClientAnchor(0, 0, 900, 200, (short) 2, 2, (short) 2, 2));

// Create a couple of lines in the group.
HSSFSimpleShape shape1 = group.createShape(new HSSFChildAnchor(3, 3,
500, 500));
shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
((HSSFChildAnchor) shape1.getAnchor()).setAnchor((short) 3, 3, 500, 500);
HSSFSimpleShape shape2 = group.createShape(new HSSFChildAnchor((short)
1, 200, 400, 600));
shape2.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);

}

private static void drawSheet4(HSSFSheet sheet4, HSSFWorkbook wb) {
// Create the drawing patriarch. This is the top level container for
// all shapes. This will clear out any existing shapes for that sheet.
HSSFPatriarch patriarch = sheet4.createDrawingPatriarch();

// Create a couple of textboxes
HSSFTextbox textbox1 = patriarch.createTextbox(
new HSSFClientAnchor(0, 0, 0, 0, (short) 1, 1, (short) 2, 2));
textbox1.setString(new HSSFRichTextString("This is a test"));
HSSFTextbox textbox2 = patriarch.createTextbox(
new HSSFClientAnchor(0, 0, 900, 100, (short) 3, 3, (short) 3, 4));
textbox2.setString(new HSSFRichTextString("Woo"));
textbox2.setFillColor(200, 0, 0);
textbox2.setLineStyle(HSSFSimpleShape.LINESTYLE_DOTGEL);

// Create third one with some fancy font styling.
HSSFTextbox textbox3 = patriarch.createTextbox(
new HSSFClientAnchor(0, 0, 900, 100, (short) 4, 4, (short) 5,
4 + 1));
HSSFFont font = wb.createFont();
font.setItalic(true);
font.setUnderline(HSSFFont.U_DOUBLE);
HSSFRichTextString string = new HSSFRichTextString("Woo!!!");
string.applyFont(2, 5, font);
textbox3.setString(string);
textbox3.setFillColor(0x08000030);
textbox3.setLineStyle(HSSFSimpleShape.LINESTYLE_NONE); // no line around the textbox.
textbox3.setNoFill(true); // make it transparent
}

private static void drawOval(HSSFPatriarch patriarch) {
// Create an oval and style to taste.
HSSFClientAnchor a = new HSSFClientAnchor();
a.setAnchor((short) 2, 2, 20, 20, (short) 2, 2, 190, 80);
HSSFSimpleShape s = patriarch.createSimpleShape(a);
s.setShapeType(HSSFSimpleShape.OBJECT_TYPE_OVAL);
s.setLineStyleColor(10, 10, 10);
s.setFillColor(90, 10, 200);
s.setLineWidth(HSSFShape.LINEWIDTH_ONE_PT * 3);
s.setLineStyle(HSSFShape.LINESTYLE_DOTSYS);
}

bign_hdl 2007-07-18
  • 打赏
  • 举报
回复
是用iReport可能会实现你的效果
rumlee 2007-07-18
  • 打赏
  • 举报
回复
我感觉好像jxl不支持这些功能吧,我以前用jxl做比较复杂的excel表格时,都是先用excel做一个模版,然后用jxl在这个模版的基础上填入自己要的数据。

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧