`
defrag_sly
  • 浏览: 125935 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

操作excel--稍后整理

    博客分类:
  • java
阅读更多
OfficeDrawingWithGraphics


import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFChildAnchor;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFShapeGroup;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class Line {

/**
* @param args
*/
public static void main(String[] args) {
FileOutputStream fileOut = null;

try {

HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");

// HSSFRow row = sheet1.createRow(2);
HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0,
(short) 10, 10, (short) 20, 20);

HSSFShapeGroup g = patriarch.createGroup(anchor);
g.setCoordinates(0, 0, 100, 100);
HSSFChildAnchor lAnchor = new HSSFChildAnchor(0, 100, 100, 0);
g.createShape(lAnchor);
int length = 6;
Math.abs(lAnchor.getDy1() - Math.sin(30 * Math.PI / 180) * length);
Math.abs(lAnchor.getDx1() - Math.cos(30 * Math.PI / 180) * length);
Math.abs(lAnchor.getDy1() - Math.sin(60 * Math.PI / 180) * length);
Math.abs(lAnchor.getDx1() - Math.cos(60 * Math.PI / 180) * length);

Math.abs(lAnchor.getDy2() - Math.sin(30 * Math.PI / 180) * length);
Math.abs(lAnchor.getDx2() - Math.cos(30 * Math.PI / 180) * length);
Math.abs(lAnchor.getDy2() - Math.sin(60 * Math.PI / 180) * length);
Math.abs(lAnchor.getDx2() - Math.cos(60 * Math.PI / 180) * length);

// Math.sin(60 * Math.PI / 180)*length;
// Math.cos(60 * Math.PI / 180)*length;
HSSFChildAnchor al = new HSSFChildAnchor(lAnchor.getDx1(), lAnchor
.getDy1(), (int) Math.abs(lAnchor.getDx1()
- Math.cos(30 * Math.PI / 180) * length), (int) Math
.abs(lAnchor.getDy1() - Math.sin(30 * Math.PI / 180)
* length));
HSSFChildAnchor ar = new HSSFChildAnchor(lAnchor.getDx1(), lAnchor
.getDy1(), (int) Math.abs(lAnchor.getDx1()
- Math.cos(60 * Math.PI / 180) * length), (int) Math
.abs(lAnchor.getDy1() - Math.sin(60 * Math.PI / 180)
* length));

HSSFChildAnchor atl = new HSSFChildAnchor(lAnchor.getDx2(), lAnchor
.getDy2(), (int) Math.abs(lAnchor.getDx2()
- Math.cos(30 * Math.PI / 180) * length), (int) Math
.abs(lAnchor.getDy2() - Math.sin(30 * Math.PI / 180)
* length));
HSSFChildAnchor atr = new HSSFChildAnchor(lAnchor.getDx2(), lAnchor
.getDy2(), (int) Math.abs(lAnchor.getDx2()
- Math.cos(60 * Math.PI / 180) * length), (int) Math
.abs(lAnchor.getDy2() - Math.sin(60 * Math.PI / 180)
* length));

g.createShape(al);
g.createShape(ar);
g.createShape(atl);
g.createShape(atr);

fileOut = new FileOutputStream("d:/workbook.xls");

wb.write(fileOut);
fileOut.close();

} catch (IOException io) {
io.printStackTrace();
System.out.println("io erorr :  " + io.getMessage());
} finally {
if (fileOut != null) {

try {
fileOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

}










import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
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.HSSFTextbox;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

;

public class TestPOI {

public static void main(String[] args) {
FileOutputStream fileOut = null;
BufferedImage bufferImg = null;
BufferedImage bufferImg1 = null;
try {

// 先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
ByteArrayOutputStream byteArrayOut1 = new ByteArrayOutputStream();
bufferImg = ImageIO.read(new File("d:/Control_Treeview.jpg"));
bufferImg1 = ImageIO.read(new File("d:/Control_Treeview.jpg"));
ImageIO.write(bufferImg, "jpg", byteArrayOut);
ImageIO.write(bufferImg1, "jpg", byteArrayOut1);

// 创建一个工作薄
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
// HSSFRow row = sheet1.createRow(2);
HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
HSSFClientAnchor anchor3 = new HSSFClientAnchor(0, 0, 512, 255,
(short) 1, 1, (short) 10, 20);
HSSFClientAnchor anchor1 = new HSSFClientAnchor(0, 0, 512, 255,
(short) 2, 30, (short) 10, 60);
anchor1.setAnchorType(2);
// HSSFClientAnchor anchor3 = new
// HSSFClientAnchor(0,0,512,255,(short) 1,1,(short)10,20);
// 插入图片

patriarch.createPicture(anchor3, wb.addPicture(byteArrayOut
.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
patriarch.createPicture(anchor3, wb.addPicture(byteArrayOut1
.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
HSSFTextbox createComment = patriarch.createTextbox(anchor3);
createComment.setString(new HSSFRichTextString("aaaaaaaaa"));
patriarch.createSimpleShape(anchor3);

fileOut = new FileOutputStream("d:/workbook.xls");
// 写入excel文件
wb.write(fileOut);
fileOut.close();

} catch (IOException io) {
io.printStackTrace();
System.out.println("io erorr :  " + io.getMessage());
} finally {
if (fileOut != null) {

try {
fileOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
分享到:
评论
1 楼 xinxinlong 2013-07-18  
lAnchor.getDy1() 是什么意思

相关推荐

Global site tag (gtag.js) - Google Analytics