?? testwriteexcel.java
字號:
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.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class TestWriteExcel {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
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("e:/pic1.jpg"));
bufferImg1 = ImageIO.read(new File("e:/pic2.jpg"));
ImageIO.write(bufferImg,"jpg",byteArrayOut);
ImageIO.write(bufferImg1,"jpg",byteArrayOut1);
//創建一個工作薄
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("圖文表");
for(int i=0;i<10;i++){
HSSFRow row = sheet1.createRow(i);
HSSFCell cell = row.createCell((short)0);
cell.setCellValue("測試數據A"+i);
cell = row.createCell((short)1);
cell.setCellValue("測試數據B"+i);
}
//HSSFRow row = sheet1.createRow(2);
HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
// HSSFClientAnchor構造方法(0,0,512,255,(short)起始水平單元格,超始垂直單元格,(short)終點水平單元格,終點垂直單元格)
HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,512,255,(short) 1,11,(short)10,30);
HSSFClientAnchor anchor1 = new HSSFClientAnchor(0,0,512,255,(short) 1,31,(short)10,50);
anchor1.setAnchorType(2);
//插入圖片
patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
patriarch.createPicture(anchor1 , wb.addPicture(byteArrayOut1.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
fileOut = new FileOutputStream("e:/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();
}
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -