poi的簡單使用

2021-10-22 23:15:17 字數 2468 閱讀 2030

日常開發中,需要匯出excel**時,對於一些簡單的excel,hutool封裝的excel工具足矣,對於一些稍複雜的excel,例如涉及合併單元格、複雜樣式,可使用原生的poi生成**,以下記錄一些常用方法。

//建立**物件

hssfworkbook workbook =

newhssfworkbook()

;//建立工作表

hssfsheet sheet = workbook.

createsheet

("測試sheet");

//建立**樣式

hssfcellstyle cellstyle = workbook.

createcellstyle()

;//設定背景色

cellstyle.

setfillforegroundcolor

(indexedcolors.grey_25_percent.

getindex()

);//設定填充模式,必須設定,不然設定背景色不生效

cellstyle.

setfillpattern

(fillpatterntype.solid_foreground)

;//設定邊框

cellstyle.

setborderbottom

(borderstyle.thin)

;//下邊框

cellstyle.

setborderleft

(borderstyle.thin)

;//左邊框

cellstyle.

setbordertop

(borderstyle.thin)

;//上邊框

cellstyle.

setborderright

(borderstyle.thin)

;//右邊框

//建立行

int rowindex=0;

hssfrow row = sheet.

createrow

(rowindex)

;//設定行樣式

row.

setrowstyle

(cellstyle)

;//建立單元格

int cellindex=0;

hssfcell cell = row.

createcell

(cellindex)

;//也可以單獨設定單元格格式

cell.

setcellstyle

(cellstyle)

;//設定單元格值

cell.

setcellvalue

("測試");

//建立合併單元格,引數分別為起始行、結束行、起始列、結束列

cellrangeaddress callrangeaddress =

newcellrangeaddress(1

,1,0

,1);

//將該合併格加入sheet中

sheet.

addmergedregion

(callrangeaddress)

;//為合併格設值,只需要給合併格的其中乙個格設值即可

hssfrow row2 = sheet.

createrow

(rowindex+1)

; hssfcell cell2 = row2.

createcell

(cellindex)

; cell2.

setcellvalue

("合併格測試值");

//合併單元格設定邊框的方式

regionutil.

setborderbottom

(borderstyle.thin, callrangeaddress, sheet)

;//下邊框

regionutil.

setborderleft

(borderstyle.thin, callrangeaddress, sheet)

;//左邊框

regionutil.

setborderright

(borderstyle.thin, callrangeaddress, sheet)

;//右邊框

regionutil.

setbordertop

(borderstyle.thin, callrangeaddress, sheet)

;//上邊框

//寫入輸出流,匯出

workbook.

write

(response.

getoutputstream()

);

利用以上方法,基本可以滿足大部分業務需求,最後附上專案中生成的**效果圖

簡單的poi匯出

org.apache.poi poi ooxml 3.17 org.apache.poi poi3.17 package com.bw.entity public class user public void setuid integer uid public string getuname pub...

jsp簡單匯出word,不使用poi

組織處理決定表 填寫人 匯出 function datatype json success function data jquery var wordexport new jquery.export responsebody public integer exportdisposetable str...

POI的基本使用

poi的基本使用 首先,理解一下乙個excel的檔案的組織形式,乙個excel檔案對應於乙個workbook hssfworkbook 乙個workbook可以有多個sheet hssfsheet 組成,乙個sheet是由多個row hssfrow 組成,乙個row是由多個cell hssfcell...