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;
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;

/**
 * <p>
 * PoiiAction类主要用于-java利用Poi在excel中绘图.
 * </p>
 * <p>
 * 创建时间 2010-6-21 - 下午08:27:29
 * </p>
 * <p>
 * copyright cd**** 2010-2011, all rights reserved.
 * </p>
 * 
 * @author IT山人
 * @since 1.0
 * @version 1.0
 */
public class PoiiAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;	

	/**
	 * <p>
	 * excel方法-Poi在excel中绘图.
	 * </p>
	 * <p>
	 * 创建人 IT山人 创建时间 2010-6-21 - 下午08:26:17
	 * </p>
	 * @return
	 * @throws IOException
	 */
	public String excel() throws IOException {
		String savePath = "**.xls";
		OutputStream os = new FileOutputStream(savePath);
		//声明一个工作薄
        HSSFWorkbook workbook = new HSSFWorkbook();
        //生成一个表格
        HSSFSheet sheet = workbook.createSheet("java在excel中绘图");
        //生成一个样式
        HSSFCellStyle style = workbook.createCellStyle();
        //设置这些样式
        style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
        style.setFillPattern(HSSFCellStyle.SOLID_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 font=workbook.createFont();
        font.setColor(HSSFColor.VIOLET.index);
        font.setFontHeightInPoints((short)16);
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        //把字体应用到当前的样式
        style.setFont(font);
        //声明一个画图的顶级管理器
        HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
		/**
		 * 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 = patriarch.createSimpleShape(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)(colFrom+1), rowFrom+1, (short)(colTo-1), rowTo-1);
		HSSFTextbox bigValueTextbox = patriarch.createTextbox(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);
//            return bout.toByteArray();
//        }catch(Exception exe){
//            exe.printStackTrace();
//            return null;
//        }
//    }
}

你可能感兴趣的:(java,poi,Excel,绘图)