Java POI導(dǎo)出excel經(jīng)典實現(xiàn)-交叉報表斜表頭
Java使用poi組件導(dǎo)出excel報表,能導(dǎo)出excel報表的還可以使用jxl組件,但jxl想對于poi功能有限,jxl應(yīng)該不能載excel插入浮動層圖片,poi能很好的實現(xiàn)輸出excel各種功能,介紹poi導(dǎo)出excel功能實現(xiàn)案例,算比較常用的功能實現(xiàn)以及導(dǎo)出excel需要注意的地方,采用的是poi-3.8-20120326.jar,poi-ooxml-3.8-20120326.jar,poi-scratchpad-3.8-20120326.jar
輸出表格
poi輸出excel最基本是輸出table表格,下面是輸出區(qū)域、總銷售額(萬元)、總利潤(萬元)簡單的表格,創(chuàng)建HSSFWorkbook對象,用于將excel輸出到輸出流中
HSSFWorkbook wb = new HSSFWorkbook();HSSFSheet sheet = wb.createSheet("table"); ?//創(chuàng)建table工作薄Object[][] datas = {{"區(qū)域", "總銷售額(萬元)", "總利潤(萬元)簡單的表格"}, {"江蘇省" , 9045, ?2256}, {"廣東省", 3000, 690}};HSSFRow row;HSSFCell cell;for(int i = 0; i < datas.length; i++) { ? ?row = sheet.createRow(i);//創(chuàng)建表格行 ? ?for(int j = 0; j < datas[i].length; j++) { ? ? ? ?cell = row.createCell(j);//根據(jù)表格行創(chuàng)建單元格 ? ? ? ?cell.setCellValue(String.valueOf(datas[i][j])); ? ?}}
設(shè)置表格行高、列寬
有時表格文本比較多,需要設(shè)置表格的列寬度,在設(shè)置表格的行高與列寬時一定在創(chuàng)建全部的HSSFRow與HSSFCell之后,
即整個表格創(chuàng)建完成之后去設(shè)置,因為在單元格合并的時候,合并之前設(shè)置的寬度單元格會比設(shè)置的寬度更寬。sheet.setColumnWidth設(shè)置列寬值需要轉(zhuǎn)換為excel的寬度值,使用工具類:MSExcelUtil,excel寬度并不是像素需要轉(zhuǎn)換
HSSFWorkbook wb = new HSSFWorkbook();HSSFSheet sheet = wb.createSheet("table"); ?//創(chuàng)建table工作薄Object[][] datas = {{"區(qū)域", "總銷售額(萬元)", "總利潤(萬元)簡單的表格"}, {"江蘇省" , 9045, ?2256}, {"廣東省", 3000, 690}};HSSFRow row;HSSFCell cell;for(int i = 0; i < datas.length; i++) { ? ?row = sheet.createRow(i);//創(chuàng)建表格行
? ?for(int j = 0; j < datas[i].length; j++) { ? ? ? ?cell = row.createCell(j);//根據(jù)表格行創(chuàng)建單元格
? ? ? ?cell.setCellValue(String.valueOf(datas[i][j])); ? ?}}
//創(chuàng)建表格之后設(shè)置行高與列寬for(int i = 0; i < datas.length; i++) { ? ?row = sheet.getRow(i); ? ?row.setHeightInPoints(30);//設(shè)置行高}for(int j = 0; j < datas[0].length; j++) { ? ?sheet.setColumnWidth(j, MSExcelUtil.pixel2WidthUnits(160)); //設(shè)置列寬}wb.write(new FileOutputStream("/Users/mike/table1.xls"));
設(shè)置excel單元格樣式
單元格可以設(shè)置居左、居中、居右、上下居中、設(shè)置邊框、設(shè)置邊框顏色、設(shè)置單元格背景顏色等,excel設(shè)置單元格有一個HSSFCellStyle類可以設(shè)置樣式,單元格顏色比較麻煩,excel顏色對應(yīng)一個下標(biāo)值,我們可以使用自定義顏色,但下標(biāo)值從11開始,前1-10被poi已經(jīng)使用,通過palette.setColorAtIndex方法將顏色與下標(biāo)值對應(yīng),下面cellStyle.setFillForegroundColor(bgIndex)設(shè)置背景顏色時set下標(biāo)值并不是顏色Color,一個下標(biāo)值如11不能被重復(fù)設(shè)置顏色,否則excel單元格顯示的都是黑色,如下背景顏色使用下標(biāo)值bgIndex=11,邊框顏色使用下標(biāo)值
short colorIndex = 10;HSSFPalette palette = wb.getCustomPalette();//自定義顏色Color rgb = Color.GREEN;short bgIndex = colorIndex ++; //背景顏色下標(biāo)值palette.setColorAtIndex(bgIndex, (byte) rgb.getRed(), (byte) rgb.getGreen(), (byte) rgb.getBlue());short bdIndex = colorIndex ++; //邊框顏色下標(biāo)值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);//創(chuàng)建表格行
? ?for(int j = 0; j < datas[i].length; j++) { ? ? ? ?cell = row.createCell(j);//根據(jù)表格行創(chuàng)建單元格
? ? ? ?cell.setCellValue(String.valueOf(datas[i][j])); ? ? ? ?HSSFCellStyle cellStyle = wb.createCellStyle(); ? ? ? ?cellStyle.setFillForegroundColor(bgIndex); //bgIndex 背景顏色下標(biāo)值
? ? ? ?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 邊框顏色下標(biāo)值
? ? ? ?cellStyle.setBottomBorderColor(bdIndex); ? ? ? ?cellStyle.setLeftBorderColor(bdIndex); ? ? ? ?cellStyle.setRightBorderColor(bdIndex); ? ? ? ?cellStyle.setTopBorderColor(bdIndex);
? ? ? ?cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER