java - 使用 apache-poi 在 excel 中画一条线

标签 java android excel line apache-poi

谁能给我写几行代码,展示如何使用 apache poi 在 excel 的单元格中写一行?! 在普通 Excel 中,我会转到插入 - 形状 - 线条。 基本上,编写如下代码:

Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet();
Row row=sheet.createRow(0);
Cell cell = row.createCell(0);

现在,缺少的代码会放在这里。 当我在网上搜索时,我应该使用类 HSSFSimpleShape 和 OBJECT_TYPE_LINE。 但我不知道如何在我的代码中实现它:(

我想我应该有我想画线的单元格或一些像素作为坐标或其他东西。

帮助! :)

最佳答案

看看这个例子:

Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet();
HSSFPatriarch patriarch = (HSSFPatriarch) sheet.createDrawingPatriarch();

/* Here is the thing: the line will go from top left in cell (0,0) to down left 
of cell (0,1) */
HSSFClientAnchor anchor = new HSSFClientAnchor(
  0, 0, 0, 255, (short) 0, 0,(short) 1, 0);

HSSFSimpleShape shape = patriarch.createSimpleShape(anchor);
shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
shape.setLineStyleColor(10, 10, 10);
shape.setFillColor(90, 10, 200);
shape.setLineWidth(HSSFShape.LINEWIDTH_ONE_PT);
shape.setLineStyle(HSSFShape.LINESTYLE_SOLID);

// you don't even need the cell, but if you also want to write anything...
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Test"); 

我还建议看一下 HSSFClientAnchor Javadoc

关于java - 使用 apache-poi 在 excel 中画一条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13907788/

相关文章:

java - RFC5545。同时计算 RRULE 和 EXDATE (EXRULE) 的事件发生次数

java - 从另一个类扩展的类可以使用具有不同返回类型的相同方法吗?

android - 查看页面 : set a different padding for first and last page

android - 导入后我的外部 JAR 文件库未显示在 'External Libraries' 中

excel - 清理 VBA 代码以引用代码而不是复制它

perl - 什么是用于 HTML MS Excel 文件的良好 CPAN 解析器?

java 游荡和垃圾收集

java - 使用改造库更新 GPS 位置并将坐标发送到服务器(在后台服务中)

php - 是否可以使用 PhoneGap 或 Appcelerator 创建基于 php 的 iphone/android 应用程序?

python - 从 VBA 调用 Python 脚本 - 不起作用