如何⽤javaPOI在excel中画线_JAVA利⽤POI在excel中绘图package cn.chn.chen.tacticmap.action;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;htmlborder
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFSimpleShape;
import org.apache.poi.hssf.usermodel.HSSFTextbox;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import cn.chn.chen.base.action.BaseAction;
/**
*
* PoiiAction类主要⽤于-java利⽤Poi在excel中绘图.
*
*
* 创建时间 2010-6-21 - 下午08:27:29
*
*
* copyright cd**** 2010-2011, all rights reserved.
*
*
* @author IT⼭⼈
* @since 1.0
* @version 1.0
*/
public class PoiiAction extends BaseAction {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
*
* excel⽅法-Poi在excel中绘图.
*
*
* 创建⼈ IT⼭⼈ 创建时间 2010-6-21 - 下午08:26:17
*
* @return
* @throws IOException
*/
public String excel() throws IOException {
String savePath = "**.xls";
OutputStream os = new FileOutputStream(savePath);
//声明⼀个⼯作薄
HSSFWorkbook workbook = new HSSFWorkbook();
//⽣成⼀个表格
HSSFSheet sheet = ateSheet("java在excel中绘图"); //⽣成⼀个样式
HSSFCellStyle style = ateCellStyle();
//设置这些样式
style.setFillForegroundColor(HSSFColor.SKY_BLUE.index); style.setFillPattern(HSSFCellStyle.SOLI
D_FOREGROUND); style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//⽣成⼀个字体
HSSFFont ateFont();
font.setColor(HSSFColor.VIOLET.index);
font.setFontHeightInPoints((short)16);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//把字体应⽤到当前的样式
style.setFont(font);
//声明⼀个画图的顶级管理器
HSSFPatriarch patriarch = ateDrawingPatriarch();
/**
* dx1:起始单元格的x偏移量,如例⼦中的255表⽰直线起始位置距A1单元格左侧的距离;
* dy1:起始单元格的y偏移量,如例⼦中的125表⽰直线起始位置距A1单元格上侧的距离;
* dx2:终⽌单元格的x偏移量,如例⼦中的1023表⽰直线起始位置距C3单元格左侧的距离;
* dy2:终⽌单元格的y偏移量,如例⼦中的150表⽰直线起始位置距C3单元格上侧的距离;
* colFrom:起始单元格列序号,从0开始计算;
* rowFrom:起始单元格⾏序号,从0开始计算,如例⼦中col1=0,row1=0就表⽰起始单元格为A1;
* colTo:终⽌单元格列序号,从0开始计算;
* rowTo:终⽌单元格⾏序号,从0开始计算,如例⼦中col2=2,row2=2就表⽰起始单元格为C3;
*/
// default
int dx1 = 0, dy1 = 0, dx2 = 1023, dy2 = 255;
int colFrom = 0, rowFrom = 0, colTo = 5, rowTo = 5;
HSSFClientAnchor bigValueAnchorShape = new HSSFClientAnchor(dx1, dy1, dx2, dy2, (short)(colFrom), rowFrom, (short) (colTo), rowTo);
HSSFSimpleShape bigValueShape = ateSimpleShape(bigValueAnchorShape);
bigValueShape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_OVAL);// 画椭圆 HSSFSimpleShape.OBJECT_TYPE_LINE画线
bigValueShape.setFillColor(180, 200, 160);
HSSFClientAnchor bigValueAnchorTextBox = new HSSFClientAnchor(dx1, dy1, dx2, dy2, (short)(col
From+1), rowFrom+1, (short)(colTo-1), rowTo-1);
HSSFTextbox bigValueTextbox = ateTextbox(bigValueAnchorTextBox);
bigValueTextbox.setString(new HSSFRichTextString("相当于⽂本框中的⽂字"));
bigValueTextbox.setLineStyle(HSSFSimpleShape.LINESTYLE_NONE);
bigValueTextbox.setFillColor(180, 205, 160);
workbook.write(os);
os.close();
return "excel";
}
⾃定义的⽅法,插⼊某个图⽚到指定索引的位置
// private static void insertImage(HSSFWorkbook wb,HSSFPatriarch pa,byte[] data,int row,int column,int index){
// int x1=index*250;
// int y1=0;
// int x2=x1+255;
// int y2=255;
// HSSFClientAnchor anchor = new HSSFClientAnchor(x1,y1,x2,y2,(short)column,row,(short)column,row); // anchor.setAnchorType(2);
// pa.createPicture(anchor , wb.addPicture(data,HSSFWorkbook.PICTURE_TYPE_JPEG));
// }
// //从图⽚⾥⾯得到字节数组
// private static byte[] getImageData(BufferedImage bi){
// try{
// ByteArrayOutputStream bout=new ByteArrayOutputStream();
// ImageIO.write(bi,"PNG",bout);
// ByteArray();
// }catch(Exception exe){
// exe.printStackTrace();
// return null;
// }
// }
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论