最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

Java POI導(dǎo)出excel經(jīng)典實現(xiàn)-交叉報表斜表頭

2023-02-14 10:50 作者:bili_54844292307  | 我要投稿

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); ? ? ? ?cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); ? ? ? ?cell.setCellStyle(cellStyle); ? ?}}

單元格文本設(shè)置字體樣式

單元格文本可設(shè)置字體大小、顏色、斜體、粗體、下劃線等。

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開始)、合并單元格最后列四個參數(shù)值

CellRangeAddress region = new CellRangeAddress(0, // first row ? ? ? ?0, // last row ? ? ? ?0, // first column ? ? ? ?2 // last column ); sheet.addMergedRegion(region);

單元格中加入圖片

單元格中不僅是文本、數(shù)值、也可以加入圖片,需要指定圖片占用單元格開始行數(shù)、開始列數(shù)、末尾行數(shù)、末尾列數(shù)。

//加入圖片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.setCol1(0); //圖片開始列數(shù)anchor.setRow1(4); //圖片開始行數(shù)anchor.setCol2(3); //圖片結(jié)束列數(shù)anchor.setRow2(25);//圖片結(jié)束行數(shù)drawing.createPicture(anchor, pictureIdx);

excel中插入浮動層圖片類似html中div

excel中插入圖片,poi導(dǎo)出excel似乎沒有按絕對位置X、Y這樣插入圖片,可以行高和列寬計算X、Y值的大概的位置在哪個單元格中,然后類似插入圖片,只指定圖片開始行數(shù)、開始列數(shù),picture.resize()會使圖片依據(jù)圖片實際大小進行擴展。

//加入圖片int pictureIdx = wb.addPicture(bt, Workbook.PICTURE_TYPE_PNG);CreationHelper helper = wb.getCreationHelper();Drawing drawing = sheet.createDrawingPatriarch();ClientAnchor anchor = helper.createClientAnchor();anchor.setCol1(0); //圖片開始列數(shù)anchor.setRow1(4); //圖片開始行數(shù)Picture picture = drawing.createPicture(anchor, pictureIdx);picture.resize();

單元格中畫斜線

excel單元格中畫斜線另一篇有詳細介紹:poiexcel斜線表頭,長度轉(zhuǎn)換MSExcelUtil,excel中單元格寬度和高度并不是像素值、ppt值,所以需要轉(zhuǎn)換,MSExcelUtil是一個轉(zhuǎn)換工具類,更多例子可參考:交叉報表制作

public class MSExcelUtil { ? ?public static final short EXCEL_COLUMN_WIDTH_FACTOR = 256; ? ?public static final int UNIT_OFFSET_LENGTH = 7; ? ?public static final int[] UNIT_OFFSET_MAP = new int[] { 0, 36, 73, 109, 146, 182, 219 }; ? ?/** ? ? * pixel units to excel width units(units of 1/256th of a character width) ? ? * ? ? * @param pxs ? ? * @return ? ? */ ? ?public static short pixel2WidthUnits(int pxs) { ? ? ? ?short widthUnits = (short) (EXCEL_COLUMN_WIDTH_FACTOR * (pxs / UNIT_OFFSET_LENGTH)); ? ? ? ?widthUnits += UNIT_OFFSET_MAP[(pxs % UNIT_OFFSET_LENGTH)]; ? ? ? ?return widthUnits; ? ?} ? ?/** ? ? * excel width units(units of 1/256th of a character width) to pixel units ? ? * ? ? * @param widthUnits ? ? * @return ? ? */ ? ?public static int widthUnits2Pixel(int widthUnits) { ? ? ? ?int pixels = (widthUnits / EXCEL_COLUMN_WIDTH_FACTOR) * UNIT_OFFSET_LENGTH; ? ? ? ?int offsetWidthUnits = widthUnits % EXCEL_COLUMN_WIDTH_FACTOR; ? ? ? ?pixels += Math.round(offsetWidthUnits ? ? ? ? ? ? ? ?/ ((float) EXCEL_COLUMN_WIDTH_FACTOR / UNIT_OFFSET_LENGTH)); ? ? ? ?return pixels; ? ?}}

完整例子

poi導(dǎo)出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"); ?//創(chuàng)建table工作薄 ? ? ? ?Object[][] datas = {{"區(qū)域產(chǎn)品銷售額","",""},{"區(qū)域", "總銷售額(萬元)", "總利潤(萬元)簡單的表格"}, {"江蘇省" , 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);//創(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(); ? ? ? ? ? ? ? ?if(i == 0 || i == 1) { ? ? ? ? ? ? ? ? ? ? ?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); ? ? ? ? ? ? ? ?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 row0, // last row0, // first column2 // last column); ? ? ? ?sheet.addMergedRegion(region); ? ? ? ? ? ? ? ?//創(chuàng)建表格之后設(shè)置行高與列寬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")); ? ?} ? }


Java POI導(dǎo)出excel經(jīng)典實現(xiàn)-交叉報表斜表頭的評論 (共 條)

分享到微博請遵守國家法律
砀山县| 方正县| 车致| 扶绥县| 曲水县| 石家庄市| 浪卡子县| 庆元县| 和林格尔县| 钟祥市| 加查县| 遵化市| 政和县| 泰安市| 弥渡县| 公主岭市| 水城县| 澳门| 淮北市| 泾阳县| 乃东县| 桑植县| 桂平市| 禄劝| 房山区| 清徐县| 邵阳市| 瑞金市| 龙海市| 新干县| 鱼台县| 青神县| 靖边县| 南昌县| 凭祥市| 望都县| 木里| 通许县| 衡水市| 石家庄市| 青浦区|