亲宝软件园·资讯

展开

导出带有图片的excel

节日快乐 人气:0
public static void main(String[] args) {

try {
FileOutputStream out = new FileOutputStream("d:\\excel13.xls");
HSSFWorkbook wb = new HSSFWorkbook();

Font headFont = wb.createFont();
headFont.setFontName("宋体");
headFont.setFontHeightInPoints((short) 18);// 字体大小
headFont.setBold(true);// 字体大小

CellStyle headStyle = wb.createCellStyle();
headStyle.setFont(headFont);
headStyle.setAlignment(HorizontalAlignment.CENTER);// 左右居中
headStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 上下居中

//创建sheet页
HSSFSheet sheet = wb.createSheet("农村集体监督资金支付明细");
sheet.setColumnWidth(0,4500);
sheet.setColumnWidth(1,3500);
sheet.setColumnWidth(2,3000);
sheet.setColumnWidth(3,4500);
sheet.setColumnWidth(4,3000);
sheet.setColumnWidth(5,3000);

//创建行
HSSFRow row0 = sheet.createRow(0);
row0.setHeight((short)800);
//创建单元格
HSSFCell cell00 = row0.createCell(0);
//往单元格中设置值
cell00.setCellValue("农村集体监督资金支付明细");
cell00.setCellStyle(headStyle);

//合并单元格
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 5));

Font cellFont = wb.createFont();
cellFont.setFontName("宋体");
cellFont.setFontHeightInPoints((short) 12);// 字体大小
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setFont(cellFont);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 上下居中

CellStyle cellStyle2 = wb.createCellStyle();
cellStyle2.setVerticalAlignment(VerticalAlignment.CENTER);// 上下居中
cellStyle2.setFont(cellFont);

HSSFRow row1 = sheet.createRow(1);
row1.setHeight((short)500);
HSSFCell cell10 = row1.createCell(0);
cell10.setCellValue("申请单位");
cell10.setCellStyle(cellStyle2);

HSSFCell cell11 = row1.createCell(1);
cell11.setCellValue("");
cell11.setCellStyle(cellStyle2);

HSSFCell cell15 = row1.createCell(5);
cell15.setCellValue("");
cell15.setCellStyle(cellStyle2);

sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 4));

HSSFRow row2 = sheet.createRow(2);
row2.setHeight((short)500);

for(int i=0;i<6;i++){
HSSFCell cell = row2.createCell(i);
if(i==0){
cell.setCellValue("款项用途");
}else if(i==2){
cell.setCellValue("yiyi");
}else{
cell.setCellValue("");
}
cell.setCellStyle(cellStyle);
}

sheet.addMergedRegion(new CellRangeAddress(2, 2, 1, 5));

HSSFRow row3 = sheet.createRow(3);
row3.setHeight((short)500);


HSSFCell cell30 = row3.createCell(0);

for(int i=0;i<6;i++){
HSSFCell cell = row3.createCell(i);
if(i==0){
cell.setCellValue("金额");
}else if(i==1){
cell.setCellValue("yiyi");
}else if(i==5){
cell.setCellValue(11);
}else{
cell.setCellValue("");
}
cell.setCellStyle(cellStyle);
}

sheet.addMergedRegion(new CellRangeAddress(3, 3, 1, 4));

HSSFRow row4 = sheet.createRow(4);
row4.setHeight((short)500);

for(int i=0;i<6;i++){
HSSFCell cell = row4.createCell(i);
if(i==0){
cell.setCellValue("付款方式");
}else if(i==1){
cell.setCellValue("单笔转账支付");
}else{
cell.setCellValue("");
}
cell.setCellStyle(cellStyle);
}

sheet.addMergedRegion(new CellRangeAddress(4, 4, 1, 5));

HSSFRow row5 = sheet.createRow(5);
row5.setHeight((short)500);
for(int i=0;i<6;i++){
HSSFCell cell = row5.createCell(i);
if(i==0){
cell.setCellValue("付款人名称");
}else if(i==3){
cell.setCellValue("付款人账号");
}else{
cell.setCellValue("");
}
cell.setCellStyle(cellStyle);
}
sheet.addMergedRegion(new CellRangeAddress(5, 5, 1, 2));
sheet.addMergedRegion(new CellRangeAddress(5, 5, 4, 5));

HSSFRow row6 = sheet.createRow(6);
row6.setHeight((short)500);
for(int i=0;i<6;i++){
HSSFCell cell = row6.createCell(i);
if(i==0){
cell.setCellValue("收款人名称");
}else if(i==3){
cell.setCellValue("收款人账号");
}else{
cell.setCellValue("");
}
cell.setCellStyle(cellStyle);
}
sheet.addMergedRegion(new CellRangeAddress(6, 6, 1, 2));
sheet.addMergedRegion(new CellRangeAddress(6, 6, 4, 5));

CellStyle cellStyle3 = wb.createCellStyle();
cellStyle3.setFont(cellFont);
cellStyle3.setBorderRight(BorderStyle.THIN);
cellStyle3.setBorderBottom(BorderStyle.THIN);
cellStyle3.setBorderLeft(BorderStyle.THIN);
cellStyle3.setBorderTop(BorderStyle.THIN);
cellStyle3.setVerticalAlignment(VerticalAlignment.CENTER);// 上下居中
cellStyle3.setAlignment(HorizontalAlignment.CENTER);// 上下居中

HSSFRow row7 = sheet.createRow(7);
row7.setHeight((short)600);
for(int i=0;i<6;i++){
HSSFCell cell7 = row7.createCell(i);
if(i==0){
cell7.setCellValue("审批情况");
}else{
cell7.setCellValue("");
}
cell7.setCellStyle(cellStyle3);
}
sheet.addMergedRegion(new CellRangeAddress(7, 7, 0, 5));


CellStyle cellStyle4 = wb.createCellStyle();
cellStyle4.setFont(cellFont);
cellStyle4.setBorderRight(BorderStyle.THIN);
cellStyle4.setBorderBottom(BorderStyle.THIN);
cellStyle4.setBorderLeft(BorderStyle.THIN);
cellStyle4.setBorderTop(BorderStyle.THIN);
cellStyle4.setVerticalAlignment(VerticalAlignment.CENTER);// 上下居中
cellStyle4.setAlignment(HorizontalAlignment.CENTER);
HSSFRow row8 = sheet.createRow(8);
row8.setHeight((short)500);
for(int i=0;i<6;i++){
HSSFCell cell = row8.createCell(i);
if(i==0){
cell.setCellValue("职务");
}else if(i==1){
cell.setCellValue("审核审批状态");
}else if(i==2){
cell.setCellValue("审核审批意见");
}else if(i==3){
cell.setCellValue("");
}else if(i==4){
cell.setCellValue("审核审批人签字");
}else if(i==5){
cell.setCellValue("审核日期");
}
cell.setCellStyle(cellStyle4);
}
sheet.addMergedRegion(new CellRangeAddress(8, 8, 2, 3));


HSSFRow row9 = sheet.createRow(9);
row9.setHeight((short)500);
for(int i=0;i<6;i++){
HSSFCell cell = row9.createCell(i);
if(i==0){
cell.setCellValue("职务");
}else if(i==1){
cell.setCellValue("审核审批状态");
}else if(i==2){
cell.setCellValue("审核审批意见");
}else if(i==3){
cell.setCellValue("");
}else if(i==4){
try {
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
BufferedImage bufferImg = null;
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
bufferImg = ImageIO.read(new File("F:\\小憩\\Map.png"));
ImageIO.write(bufferImg, "png", byteArrayOut);
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 1023, 250,(short) 4, 9, (short) 4, 9);
anchor.setAnchorType(ClientAnchor.AnchorType.DONT_MOVE_AND_RESIZE);
patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
} catch (IOException e) {
e.printStackTrace();
}
}else if(i==5){
cell.setCellValue("审核日期");
}
cell.setCellStyle(cellStyle4);
}
sheet.addMergedRegion(new CellRangeAddress(9, 9, 2, 3));

wb.write(out);
out.close();
} catch (IOException e) {
e.printStackTrace();
}

}

加载全部内容

相关教程
猜你喜欢
用户评论