使用poi將資料進行匯出

2021-08-15 06:50:49 字數 2167 閱讀 5453

1.jar的依賴

org.apache.poi

poi-ooxml

3.15

2.方法

//建立行

xssfrow row1= sheet.createrow(3);

//設定單元格內容水平居中

//建立單元格樣式

xssfcellstyle style1 = workbook.createcellstyle();

style1.setalignment(horizontalalignment.center);

//設定單元格內容垂直居中

style1.setverticalalignment(verticalalignment.center);

xssffont font1 = workbook.createfont();

font1.setfontname(xssffont.default_font_name);//設定字型樣式為預設

font1.setbold(true);//加粗

font1.setfontheightinpoints((short) 12);//設定字型大小

style1.setfont(font1);

//設定單元格中的值

for(int i=0;ixssfcell cell1 =row1.createcell(i);

cell1.setcellvalue(info[i]);

cell1.setcellstyle(style1);

}//將資料插入到**中

for(int i=0;i//******dateformat deteformat =  new ******dateformat("yyyy年mm月dd日 hh時mm分ss秒");

//date date = new date();

decimalformat    df = new decimalformat("0");//使用者格式化十進位制數字

//建立行 

xssfrow row11 = sheet.createrow(i+4);

//建立單元格 

xssfcell cell11 = row11.createcell(0);

cell11.setcellvalue(pojo.get(i).getnick_name());

xssfcell cell12 = row11.createcell(1);

cell12.setcellvalue(pojo.get(i).getphone());

xssfcell cell13 = row11.createcell(2);

cell13.setcellvalue(pojo.get(i).getcustomer_id());

xssfcell cell14 = row11.createcell(3);

xssfcellstyle style2 =workbook.createcellstyle();

xssfdataformat format =workbook.createdataformat();

style2.setdataformat(format.getformat("@"));//設定為文字形式

cell14.setcellstyle(style2);

cell14.setcellvalue(df.format(pojo.get(i).getcreated_time()));//將數字轉化為字串

cell14.setcelltype(xssfcell.cell_type_string);

}sheet.autosizecolumn((short)0,true);//設定列的自適應寬度

sheet.autosizecolumn((short)1,true);

sheet.autosizecolumn((short)2,true);

sheet.autosizecolumn((short)3,true);

workbook.write(out);

out.close();

workbook.close();

}3.效果

簡單使用POI技術,將大量資料匯出Excel表

首先匯入框架所需要的三個jar包 使用fileoutputstream流建立 xlsx 的本地資料夾 然後使用的是org.apache.poi.xssf.streaming.sxssfworkbook這個物件進行對excel表的操控,這個物件可以操作大資料量的匯出,之前使用的是hssfworkboo...

POI匯出資料

很簡單的乙個資料匯出啊,因為我的需求只有兩個字段,使用者名稱及手機號所以簡單的一批,記錄一下,方便能對大家有幫助,這個poi也非常的簡單。上 poi 的 jar依賴 org.apache.poi poi3.6 上一下excel工具類 新建 excelutil 類 package com.tianru...

POI匯出資料

workbook wb new hssfworkbook 新建乙個wb工作簿 sheet sheet wb.createsheet 學生的sheet頁 row row sheet.createrow 0 反射獲取屬性名作為表頭 這裡也可以讓別人傳 field fields clz.getdeclar...