npoi 匯出execl 動態合併單元格內容

2022-05-23 13:54:13 字數 2485 閱讀 6760

最近工作遇到了匯出execl畫**,記錄一下,省的下次再忘了,用的第三方的npoi, 其中涉及到動態合併單元格,其中你想要動態合併的那列的資料,最好查詢資料的時候排序一下,不然還得要處理跨行合併的問題,那樣比較麻煩,所以我是先排序了一下,以下是**

1

public

iworkbook getdeptexcelquerys(yearqueryparam param, user user)

2, , , , , , , };

42 irow headrow = sheet.createrow(1

);43

var cellcount = 0;44

foreach (var item in

arrlist)

4551

var result =getdeptexeclquerys(param, user);

5253

var name = string

.empty;

54var type = string

.empty;

55object

vue;

56//

填充表內資料

57for (int i = 0; i < result.count; i++)

5875

else

7679 cell.cellstyle =bodystyle;80}

81}82 j++;83}

8485}86

87//

合併行88 arraylist arrtitles = new arraylist , , , , , , , };

89 datatable dt = listtodt(result, arrtitles, false, ""); //

因為我取的資料來源是集合,於是轉成datatable,合併單元格的時候需要用到

90 mergecells(sheet, dt, 0, 0, 0); //

合併單元格

91 mergecells(sheet, dt, 1, 1, 1

);92 mergecells(sheet, dt, 2, 2, 2

);93

//列寬

94 sheet.setcolumnwidth(0, 20 * 256

);95 sheet.setcolumnwidth(1, 20 * 256

);96 sheet.setcolumnwidth(2, 20 * 256

);97 sheet.setcolumnwidth(3, 20 * 256

);98 sheet.setcolumnwidth(4, 25 * 256

);99 sheet.setcolumnwidth(5, 25 * 256

);100 sheet.setcolumnwidth(6, 25 * 256

);101 sheet.setcolumnwidth(7, 25 * 256

);102

103return

workbook;

104 }

view code

1

///2

///合併單元格

3///

4///

當前sheet

5///

資料來源6

///要合併的第幾列

7///

起始列號

8///

終止列號910

public

void mergecells(isheet sheet, datatable dt, int cellindex, int startindex, int

endindex)

1124

else

if (value.trim() == value1.trim() && j == (dt.rows.count + 1

))2529}

30 sheet.addmergedregion(new

cellrangeaddress(i, end, startindex, endindex));

31 i =end;32}

33 }

view code

效果圖:

第一次寫部落格有點懵!!

NPOI匯出完美合併單元格

後台 using system using system.data using system.configuration using system.data.sqlclient using system.io using npoi.ss.usermodel using system.web usin...

java poi 匯出execl動態可選字段

在網上看了很多poi動態匯出可選字段的文章,覺得都過於複雜。這裡給出乙個簡單的實現,希望能幫助需要的朋友。需求說明 首先在匯出頁面選擇查詢條件和匯出字段傳到後台,後台根據查詢條件查詢資料,再把資料轉換成匯出字段生成 execl。這裡使用的持久化框架是mybatis,資料跟字段之間的轉換就是通過myb...

NPOI匯出excel(居中,合併單元格)

npoi匯出excel 居中,合併單元格 excel表頭作為引數傳入 bll層 using system using system.collections.generic using npoi.ss.usermodel using npoi.xssf.usermodel using npoi.hss...