Java使用POi导出Excel(包含图片)

    <div id="article_content" class="article_content clearfix">
    <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-b5506197d8.css">
            <div id="content_views" class="htmledit_views">
                <p>Java使用poi组件导出excel报表,能导出excel报表的还可以使用jxl组件,但jxl想对于poi功能有限,jxl应该不能载excel插入浮动层图片,poi能很好的实现输出excel各种功能,介绍poi导出excel功能实现案例,算比较常用的功能实现以及导出excel需要注意的地方,采用的是poi-3.8-20120326.jar,poi-ooxml-3.8-20120326.jar,poi-scratchpad-3.8-20120326.jar</p> 


输出表格


poi输出excel最基本是输出table表格,下面是输出区域、总销售额(万元)、总利润(万元)简单的表格,
创建HSSFWorkbook 对象,用于将excel输出到输出流中


 
 
  1. HSSFWorkbook wb = new HSSFWorkbook();
  2. HSSFSheet sheet = wb.createSheet( "table");   //创建table工作薄
  3. Object[][] datas = {{ "区域", "总销售额(万元)", "总利润(万元)简单的表格"}, { "江苏省" , 9045,   2256}, { "广东省", 3000, 690}};
  4. HSSFRow row;
  5. HSSFCell cell;
  6. for( int i = 0; i < datas.length; i++) {
  7.     row = sheet.createRow(i); //创建表格行
  8.      for( int j = 0; j < datas[i].length; j++) {
  9.         cell = row.createCell(j); //根据表格行创建单元格
  10.         cell.setCellValue(String.valueOf(datas[i][j]));
  11.     }
  12. }
  13. wb.write( new FileOutputStream( "/Users/mike/table.xls"));


设置表格行高、列宽


有时表格文本比较多,需要设置表格的列宽度,在设置表格的行高与列宽时一定在创建全部的HSSFRow与HSSFCell之后,
即整个表格创建完成之后去设置,因为在单元格合并的时候,合并之前设置的宽度单元格会比设置的宽度更宽。 sheet.setColumnWidth 设置列宽值需要转换为excel的宽度值,使用工具类:MSExcelUtil,excel宽度并不是像素需要转换


 
 
  1. HSSFWorkbook wb = new HSSFWorkbook();
  2. HSSFSheet sheet = wb.createSheet( "table");   //创建table工作薄
  3. Object[][] datas = {{ "区域", "总销售额(万元)", "总利润(万元)简单的表格"}, { "江苏省" , 9045,   2256}, { "广东省", 3000, 690}};
  4. HSSFRow row;
  5. HSSFCell cell;
  6. for( int i = 0; i < datas.length; i++) {
  7.     row = sheet.createRow(i); //创建表格行
  8.      for( int j = 0; j < datas[i].length; j++) {
  9.         cell = row.createCell(j); //根据表格行创建单元格
  10.         cell.setCellValue(String.valueOf(datas[i][j]));
  11.     }
  12. }
  13. //创建表格之后设置行高与列宽
  14. for( int i = 0; i < datas.length; i++) {
  15.     row = sheet.getRow(i);
  16.     row.setHeightInPoints( 30); //设置行高
  17. }
  18. for( int j = 0; j < datas[ 0].length; j++) {
  19.     sheet.setColumnWidth(j, MSExcelUtil.pixel2WidthUnits( 160)); //设置列宽
  20. }
  21. wb.write( new FileOutputStream( "/Users/mike/table1.xls"));


设置excel单元格样式


单元格可以设置居左、居中、居右、上下居中、设置边框、设置边框颜色、设置单元格背景颜色等, excel设置单元格有一个HSSFCellStyle类可以设置样式,单元格颜色比较麻烦,excel颜色对应一个下标值,我们可以使用自定义颜色,但下标值从11开始,前1-10被poi已经使用,通过palette.setColorAtIndex方法将颜色与下标值对应,下面cellStyle.setFillForegroundColor(bgIndex)设置背景颜色时set 下标值并不是颜色Color,一个下标值如11不能被重复设置颜色,否则excel单元格显示的都是黑色,如下 背景颜色使用下标值bgIndex=11,边框颜色使用下标值bdIndex=12


 
 
  1. short colorIndex = 10;
  2. HSSFPalette palette = wb.getCustomPalette(); //自定义颜色
  3. Color rgb = Color.GREEN;
  4. short bgIndex = colorIndex ++; //背景颜色下标值
  5. palette.setColorAtIndex(bgIndex, ( byte) rgb.getRed(), ( byte) rgb.getGreen(), ( byte) rgb.getBlue());
  6. short bdIndex = colorIndex ++; //边框颜色下标值
  7. rgb = Color.BLACK;
  8. palette.setColorAtIndex(bdIndex, ( byte) rgb.getRed(), ( byte) rgb.getGreen(), ( byte) rgb.getBlue());
  9. for( int i = 0; i < datas.length; i++) {
  10.     row = sheet.createRow(i); //创建表格行
  11.      for( int j = 0; j < datas[i].length; j++) {
  12.         cell = row.createCell(j); //根据表格行创建单元格
  13.         cell.setCellValue(String.valueOf(datas[i][j]));
  14.         HSSFCellStyle cellStyle = wb.createCellStyle();
  15.         cellStyle.setFillForegroundColor(bgIndex); //bgIndex 背景颜色下标值
  16.         cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
  17.         cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
  18.         cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
  19.         cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
  20.         cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
  21.          //bdIndex 边框颜色下标值
  22.         cellStyle.setBottomBorderColor(bdIndex);
  23.         cellStyle.setLeftBorderColor(bdIndex);
  24.         cellStyle.setRightBorderColor(bdIndex);
  25.         cellStyle.setTopBorderColor(bdIndex);
  26.         cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
  27.         cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
  28.         cell.setCellStyle(cellStyle);
  29.     }
  30. }


单元格文本设置字体样式


单元格文本可设置字体大小、颜色、斜体、粗体、下划线等。


 
 
  1. HSSFCellStyle cellStyle = wb.createCellStyle();
  2. HSSFFont font = wb.createFont();
  3. font.setItalic( true);
  4. font.setUnderline(HSSFFont.U_SINGLE);
  5. font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  6. font.setFontHeightInPoints(( short) 14);
  7. cellStyle.setFont(font);


合并单元格


sheet中可以类似html合并单元格,指定开始行(从0开始计算)、合并单元格最后行、开始列(从0开始)、 合并单元格最后列四个参数值


 
 
  1. CellRangeAddress region = new CellRangeAddress( 0, // first row
  2.          0, // last row
  3.          0, // first column
  4.          2 // last column
  5. );
  6. sheet.addMergedRegion(region);


单元格中加入图片


单元格中不仅是文本、数值、也可以加入图片,需要指定图片占用单元格开始行数、开始列数、末尾行数、末尾列数。 
支持png、jpeg、emf等


 
 
  1. //加入图片
  2. byte[] bt = FileUtils.readFileToByteArray( new File( "/Users/mike/pie.png"));
  3. int pictureIdx = wb.addPicture(bt, Workbook.PICTURE_TYPE_PNG);
  4. CreationHelper helper = wb.getCreationHelper();
  5. Drawing drawing = sheet.createDrawingPatriarch();
  6. ClientAnchor anchor = helper.createClientAnchor();
  7. anchor.setCol1( 0); //图片开始列数
  8. anchor.setRow1( 4); //图片开始行数
  9. anchor.setCol2( 3); //图片结束列数
  10. anchor.setRow2( 25); //图片结束行数
  11. drawing.createPicture(anchor, pictureIdx);


excel中插入浮动层图片类似html中div


excel中插入图片, poi导出excel似乎没有按绝对位置X、Y这样插入图片,可以行高和列宽计算X、Y值的大概的位置在哪个单元格中,
然后类似(6)中插入图片,只指定图片开始行数、开始列数,picture.resize()会使图片依据图片实际大小进行扩展。


 
 
  1. //加入图片
  2. int pictureIdx = wb.addPicture(bt, Workbook.PICTURE_TYPE_PNG);
  3. CreationHelper helper = wb.getCreationHelper();
  4. Drawing drawing = sheet.createDrawingPatriarch();
  5. ClientAnchor anchor = helper.createClientAnchor();
  6. anchor.setCol1( 0); //图片开始列数
  7. anchor.setRow1( 4); //图片开始行数
  8. Picture picture = drawing.createPicture(anchor, pictureIdx);
  9. picture.resize();

 


长度转换MSExcelUtil


excel中单元格宽度和高度并不是像素值、ppt值,所以需要转换,MSExcelUtil是一个转换工具类


 
 
  1. public class MSExcelUtil {
  2.      public static final short EXCEL_COLUMN_WIDTH_FACTOR = 256;
  3.      public static final int UNIT_OFFSET_LENGTH = 7;
  4.      public static final int[] UNIT_OFFSET_MAP = new int[] { 0, 36, 73, 109, 146, 182, 219 };
  5.      /**
  6.      * pixel units to excel width units(units of 1/256th of a character width)
  7.      *
  8.      * @param pxs
  9.      * @return
  10.      */
  11.      public static short pixel2WidthUnits(int pxs) {
  12.          short widthUnits = ( short) (EXCEL_COLUMN_WIDTH_FACTOR * (pxs / UNIT_OFFSET_LENGTH));
  13.         widthUnits += UNIT_OFFSET_MAP[(pxs % UNIT_OFFSET_LENGTH)];
  14.          return widthUnits;
  15.     }
  16.      /**
  17.      * excel width units(units of 1/256th of a character width) to pixel units
  18.      *
  19.      * @param widthUnits
  20.      * @return
  21.      */
  22.      public static int widthUnits2Pixel(int widthUnits) {
  23.          int pixels = (widthUnits / EXCEL_COLUMN_WIDTH_FACTOR) * UNIT_OFFSET_LENGTH;
  24.          int offsetWidthUnits = widthUnits % EXCEL_COLUMN_WIDTH_FACTOR;
  25.         pixels += Math.round(offsetWidthUnits
  26.                 / (( float) EXCEL_COLUMN_WIDTH_FACTOR / UNIT_OFFSET_LENGTH));
  27.          return pixels;
  28.     }
  29. }


完整例子


poi导出excel源码


 
 
  1. import java.awt.Color;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import org.apache.commons.io.FileUtils;
  5. import org.apache.poi.hssf.usermodel.HSSFCell;
  6. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  7. import org.apache.poi.hssf.usermodel.HSSFFont;
  8. import org.apache.poi.hssf.usermodel.HSSFPalette;
  9. import org.apache.poi.hssf.usermodel.HSSFRow;
  10. import org.apache.poi.hssf.usermodel.HSSFSheet;
  11. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  12. import org.apache.poi.ss.usermodel.CellStyle;
  13. import org.apache.poi.ss.usermodel.ClientAnchor;
  14. import org.apache.poi.ss.usermodel.CreationHelper;
  15. import org.apache.poi.ss.usermodel.Drawing;
  16. import org.apache.poi.ss.usermodel.Workbook;
  17. import org.apache.poi.ss.util.CellRangeAddress;
  18. public final class TestExportExcel {
  19.      public static void main(String[] args) throws Exception  {
  20.         HSSFWorkbook wb = new HSSFWorkbook();
  21.         HSSFSheet sheet = wb.createSheet( "table");   //创建table工作薄
  22.         Object[][] datas = {{ "区域产品销售额", "", ""},{ "区域", "总销售额(万元)", "总利润(万元)简单的表格"}, { "江苏省" , 9045,   2256}, { "广东省", 3000, 690}};
  23.         HSSFRow row;
  24.         HSSFCell cell;
  25.          short colorIndex = 10;
  26.         HSSFPalette palette = wb.getCustomPalette();
  27.         Color rgb = Color.GREEN;
  28.          short bgIndex = colorIndex ++;
  29.         palette.setColorAtIndex(bgIndex, ( byte) rgb.getRed(), ( byte) rgb.getGreen(), ( byte) rgb.getBlue());
  30.          short bdIndex = colorIndex ++;
  31.         rgb = Color.BLACK;
  32.         palette.setColorAtIndex(bdIndex, ( byte) rgb.getRed(), ( byte) rgb.getGreen(), ( byte) rgb.getBlue());
  33.          for( int i = 0; i < datas.length; i++) {
  34.             row = sheet.createRow(i); //创建表格行
  35.              for( int j = 0; j < datas[i].length; j++) {
  36.                 cell = row.createCell(j); //根据表格行创建单元格
  37.                 cell.setCellValue(String.valueOf(datas[i][j]));
  38.                 HSSFCellStyle cellStyle = wb.createCellStyle();
  39.                  if(i == 0 || i == 1) {
  40.                       cellStyle.setFillForegroundColor(bgIndex); //bgIndex 背景颜色下标值
  41.                       cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
  42.                 }
  43.                 cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
  44.                 cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
  45.                 cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
  46.                 cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
  47.                  //bdIndex 边框颜色下标值
  48.                 cellStyle.setBottomBorderColor(bdIndex);
  49.                 cellStyle.setLeftBorderColor(bdIndex);
  50.                 cellStyle.setRightBorderColor(bdIndex);
  51.                 cellStyle.setTopBorderColor(bdIndex);
  52.                 cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
  53.                 cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);           
  54.                  if(i == datas.length - 1 && j == datas[ 0].length - 1) {
  55.                     HSSFFont font = wb.createFont();
  56.                     font.setItalic( true);
  57.                     font.setUnderline(HSSFFont.U_SINGLE);
  58.                     font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  59.                     font.setFontHeightInPoints(( short) 14);
  60.                     cellStyle.setFont(font);
  61.                 }
  62.                 cell.setCellStyle(cellStyle);
  63.             }
  64.         }
  65.          //加入图片
  66.          byte[] bt = FileUtils.readFileToByteArray( new File( "/Users/mike/pie.png"));
  67.          int pictureIdx = wb.addPicture(bt, Workbook.PICTURE_TYPE_PNG);
  68.         CreationHelper helper = wb.getCreationHelper();
  69.         Drawing drawing = sheet.createDrawingPatriarch();
  70.         ClientAnchor anchor = helper.createClientAnchor();
  71.         anchor.setDx1(MSExcelUtil.pixel2WidthUnits( 60));
  72.         anchor.setDy1(MSExcelUtil.pixel2WidthUnits( 60));
  73.         anchor.setCol1( 0);
  74.         anchor.setRow1( 4);
  75.         anchor.setCol2( 3);
  76.         anchor.setRow2( 25);
  77.         drawing.createPicture(anchor, pictureIdx);
  78.          //合并单元格
  79.         CellRangeAddress region = new CellRangeAddress( 0, // first row
  80.                  0, // last row
  81.                  0, // first column
  82.                  2 // last column
  83.         );
  84.         sheet.addMergedRegion(region);
  85.          //创建表格之后设置行高与列宽
  86.          for( int i = 0; i < datas.length; i++) {
  87.             row = sheet.getRow(i);
  88.             row.setHeightInPoints( 30);
  89.         }
  90.          for( int j = 0; j < datas[ 0].length; j++) {
  91.             sheet.setColumnWidth(j, MSExcelUtil.pixel2WidthUnits( 160));
  92.         }
  93.         wb.write( new FileOutputStream( "/Users/mike/table6.xls"));
  94.     }   
  95. }

 

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值