一、基本功能之输出表格
poi输出excel最基本是输出table表格,下面是输出区域、总销售额(万元)、总利润(万元)简单的表格,
创建HSSFWorkbook 对象,用于将excel输出到输出流中
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("table"); //创建table工作薄
Object[][] datas = {{"区域", "总销售额(万元)", "总利润(万元)简单的表格"}, {"江苏省" , 9045, 2256}, {"广东省", 3000, 690}};
HSSFRow row;
HSSFCell cell;
for(int i = 0; i < datas.length; i++) {
row = sheet.createRow(i);//创建表格行
for(int j = 0; j < datas[i].length; j++) {
cell = row.createCell(j);//根据表格行创建单元格
cell.setCellValue(String.valueOf(datas[i][j]));
}
}
wb.write(new FileOutputStream("/Users/mike/table.xls"));
二、设置表格行高、列宽
有时表格文本比较多,需要设置表格的列宽度,在设置表格的行高与列宽时一定在创建全部的HSSFRow与HSSFCell之后,
即整个表格创建完成之后去设置,因为在单元格合并的时候,合并之前设置的宽度单元格会比设置的宽度更宽。 sheet.setColumnWidth 设置列宽值需要转换为excel的宽度值,使用工具类:MSExcelUtil,excel宽度并不是像素需要转换。
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("table"); //创建table工作薄
Object[][] datas = {{"区域", "总销售额(万元)", "总利润(万元)简单的表格"}, {"江苏省" , 9045, 2256}, {"广东省", 3000, 690}};
HSSFRow row;
HSSFCell cell;
for(int i = 0; i < datas.length; i++) {
row = sheet.createRow(i);//创建表格行
for(int j = 0; j < datas[i].length; j++) {
cell = row.createCell(j);//根据表格行创建单元格
cell.setCellValue(String.valueOf(datas[i][j]));
}
}
//创建表格之后设置行高与列宽
for(int i = 0; i < datas.length; i++) {
row = sheet.getRow(i);
row.setHeightInPoints(30);//设置行高
}
for(int j = 0; j < datas[0].length; j++) {
sheet.setColumnWidth(j, MSExcelUtil.pixel2WidthUnits(160)); //设置列宽
}
wb.write(new FileOutputStream("/Users/mike/table1.xls"));
三、设置单元格样式
单元格可以设置居左、居中、居右、上下居中、设置边框、设置边框颜色、设置单元格背景颜色等, excel设置单元格有一个HSSFCellStyle类可以设置样式,单元格颜色比较麻烦,而且使用场景不多,详情可以参考:Java POI导出excel经典实现
四、单元格文本设置字体样式
单元格文本可设置字体大小、颜色、斜体、粗体、下划线等。
HSSFCellStyle cellStyle = wb.createCellStyle();
HSSFFont font = wb.createFont();
font.setItalic(true);
font.setUnderline(HSSFFont.U_SINGLE);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setFontHeightInPoints((short)14);
cellStyle.setFont(font);
五、合并单元格
sheet中可以类似html合并单元格,指定开始行(从0开始计算)、合并单元格最后行、开始列(从0开始)、 合并单元格最后列四个参数值
CellRangeAddress region = new CellRangeAddress(0, // first row
0, // last row
0, // first column
2 // last column
);
sheet.addMergedRegion(region);
六、单元格插图片、excel中插入浮动层图片类似html中div、长度转换MSExcelUtil
http://www.anyrt.com/blog/list/poiexcel.html
这些东西感觉一方面很少用的到,另一方面即使有需要应该也会有更强大可以直接用的工具。
七、完整例子
poi导出excel源码
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPalette;
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.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
public final class TestExportExcel {
public static void main(String[] args) throws Exception {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("table"); //创建table工作薄
Object[][] datas = {{"区域产品销售额","",""},{"区域", "总销售额(万元)", "总利润(万元)简单的表格"}, {"江苏省" , 9045, 2256}, {"广东省", 3000, 690}};
HSSFRow row;
HSSFCell cell;
short colorIndex = 10;
HSSFPalette palette = wb.getCustomPalette();
Color rgb = Color.GREEN;
short bgIndex = colorIndex ++;
palette.setColorAtIndex(bgIndex, (byte) rgb.getRed(), (byte) rgb.getGreen(), (byte) rgb.getBlue());
short bdIndex = colorIndex ++;
rgb = Color.BLACK;
palette.setColorAtIndex(bdIndex, (byte) rgb.getRed(), (byte) rgb.getGreen(), (byte) rgb.getBlue());
for(int i = 0; i < datas.length; i++) {
row = sheet.createRow(i);//创建表格行
for(int j = 0; j < datas[i].length; j++) {
cell = row.createCell(j);//根据表格行创建单元格
cell.setCellValue(String.valueOf(datas[i][j]));
HSSFCellStyle cellStyle = wb.createCellStyle();
if(i == 0 || i == 1) {
cellStyle.setFillForegroundColor(bgIndex); //bgIndex 背景颜色下标值
cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
}
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
//bdIndex 边框颜色下标值
cellStyle.setBottomBorderColor(bdIndex);
cellStyle.setLeftBorderColor(bdIndex);
cellStyle.setRightBorderColor(bdIndex);
cellStyle.setTopBorderColor(bdIndex);
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
if(i == datas.length - 1 && j == datas[0].length - 1) {
HSSFFont font = wb.createFont();
font.setItalic(true);
font.setUnderline(HSSFFont.U_SINGLE);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setFontHeightInPoints((short)14);
cellStyle.setFont(font);
}
cell.setCellStyle(cellStyle);
}
}
//加入图片
byte[] bt = FileUtils.readFileToByteArray(new File("/Users/mike/pie.png"));
int pictureIdx = wb.addPicture(bt, Workbook.PICTURE_TYPE_PNG);
CreationHelper helper = wb.getCreationHelper();
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = helper.createClientAnchor();
anchor.setDx1(MSExcelUtil.pixel2WidthUnits(60));
anchor.setDy1(MSExcelUtil.pixel2WidthUnits(60));
anchor.setCol1(0);
anchor.setRow1(4);
anchor.setCol2(3);
anchor.setRow2(25);
drawing.createPicture(anchor, pictureIdx);
//合并单元格
CellRangeAddress region = new CellRangeAddress(0, // first row
0, // last row
0, // first column
2 // last column
);
sheet.addMergedRegion(region);
//创建表格之后设置行高与列宽
for(int i = 0; i < datas.length; i++) {
row = sheet.getRow(i);
row.setHeightInPoints(30);
}
for(int j = 0; j < datas[0].length; j++) {
sheet.setColumnWidth(j, MSExcelUtil.pixel2WidthUnits(160));
}
wb.write(new FileOutputStream("/Users/mike/table6.xls"));
}
}