项目使用excel模板写入图片总结

导出excel的问题,导出多个sheet使用模板方式还是api方式,不知道什么方式更好点
     讨论结果:
     1)使用模板方式,对于多余的sheel进行删除。
     2)建议在导出的过程中,将导出按钮灰掉,不让其操作。
     3)介绍使用jconsole监控导出性能,防止后期出现性能问题。
     4)jxl只支持png格式的图片,需要通过ImageIO对图片进行转换
     遇到问题:
     1)poi在向模板中插入图片的时候,如果模板存在批注,生产的文件会提示数据丢失,并且不能编译。
     2)如何将多张图片放在同一个单元格中显示。
样例代码:
package com.test;


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


import javax.imageio.ImageIO;


import org.apache.poi.hssf.usermodel.HSSFCell;
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.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;


public class ExcelPoi {


@SuppressWarnings("deprecation")
public void exportExcel() {
String realpath = "关注本地人员导出模板.xls";
try {
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(realpath));
File target = new File("test1.xls");
HSSFSheet sheet = workbook.getSheet("fang");
HSSFCellStyle style = this.getStyle(workbook);


HSSFRow row = sheet.getRow(1);
HSSFCell cell = row.getCell((short) 2);
cell.setCellStyle(style);
cell.setCellValue("xx");
/**/

// 行高
//row = sheet.getRow(59);
//row.setHeight((short)5000);

BufferedImage bufferImg = null;
BufferedImage bufferImg1 = null;
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
ByteArrayOutputStream byteArrayOut1 = new ByteArrayOutputStream();
bufferImg = ImageIO.read(new File("1.jpg"));
bufferImg1 = ImageIO.read(new File("2.jpg"));
ImageIO.write(bufferImg, "jpg", byteArrayOut);
ImageIO.write(bufferImg1, "jpg", byteArrayOut1);

HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 174, 190, (short) 2, 59, (short) 2, 59);
//HSSFClientAnchor anchor1 = new HSSFClientAnchor(174, 0, 350, 160, (short) 2, 59, (short) 2, 59);
//anchor1.setAnchorType(2);
patriarch.createPicture(anchor, workbook.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
//patriarch.createPicture(anchor1, workbook.addPicture(byteArrayOut1.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));


//byteArrayOut.close();
//byteArrayOut1.close();

//workbook.removeSheetAt(2);
FileOutputStream fos = new FileOutputStream(target);
workbook.write(fos);
fos.flush();
fos.close();



} catch (Exception e) {
e.printStackTrace();
}


System.out.println("成功导出Excel!!!!");
}


public HSSFCellStyle getStyle(HSSFWorkbook workbook) {
//      设置字体; 
HSSFFont font = workbook.createFont();
//设置字体大小; 
font.setFontHeightInPoints((short) 10);
//设置字体名字; 
font.setFontName("仿宋_GB2312");
//font.setItalic(true); 
//font.setStrikeout(true); 
//      设置样式; 
HSSFCellStyle style = workbook.createCellStyle();
//设置底边框; 
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//设置底边框颜色; 
style.setBottomBorderColor(HSSFColor.BLACK.index);
//设置左边框; 
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//设置左边框颜色; 
style.setLeftBorderColor(HSSFColor.BLACK.index);
//设置右边框; 
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//设置右边框颜色; 
style.setRightBorderColor(HSSFColor.BLACK.index);
//设置顶边框; 
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//设置顶边框颜色; 
style.setTopBorderColor(HSSFColor.BLACK.index);
//在样式用应用设置的字体; 
style.setFont(font);
//设置自动换行; 
style.setWrapText(false);
//设置水平对齐的样式为居中对齐; 
style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
//设置垂直对齐的样式为居中对齐; 
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
return style;
}


public static void main(String[] args) {
ExcelPoi excelpoi = new ExcelPoi();
excelpoi.exportExcel();
}
}

你可能感兴趣的:(exception,api,String,File,Excel,Class)