使用poi匯出excel根據指定列自動合併行

2021-08-20 20:19:20 字數 3916 閱讀 5204

一、效果圖

二、js**

function assemble_dg_columns() );

arr.push();

arr.push();

arr.push();

arr.push();

arr.push();

arr.push();

arr.push();

arr.push();

arr.push();

arr.push();

arr.push();

arr.push();

return arr;

}$("body").on("click", "#btn_print", function () );

function assessevaluationsummaryprint()

}window.location.href= "jee/vassessevaluationsummaryc/exportexecl?key_columns="+key_columns+"&value_columns="+value_columns;

}

三、後台**

(1)controller

@controller

public class vassessevaluationsummarycontroller extends baseentitycontroller

@resource

private vassessevaluationsummaryservice vassessevaluationsummaryservice;

@responsebody

@systemcontrollerlog(description="考核評價彙總--匯出功能")

注:excel檔案中文名亂碼使用"+new string("考核評價".getbytes("gb2312"),"iso8859-1")+".xlsx"解決

(2)service

@service("vassessevaluationsummaryservice")

public class vassessevaluationsummaryserviceimpl implements vassessevaluationsummaryservice

listls_param = new arraylist();

string hql = "select u from vassessevaluation u where (del_flag is null or del_flag != '1')";

string hqlcount = "select count(u) from vassessevaluation u where (del_flag is null or del_flag != '1')";

if(stringutils.isnotblank(key))

if(stringutils.isnotblank(start_time))

if(stringutils.isnotblank(end_time))

if(stringutils.isnotblank(question_type))

if(stringutils.isnotblank(id_postduty))

if(stringutils.isnotblank(sort)&&stringutils.isnotblank(order))

list = baseinfoservice.listquerybyhql(hql,ls_param,rows,page);

object total = baseinfoservice.getobjectbyhql(hqlcount,ls_param);

pageresult result = new pageresult();

if (list != null) else

} return result; }

/***

* @description 考核評價彙總--匯出功能

* @param key_columns

* @param value_columns

* @return hssfworkbook

* @throws exception

* */

@override

public hssfworkbook exportexecl(string key_columns, string value_columns) throws exception else

}else

}list1.add(list2);

} int arr = ; //要合併的列,從0開始

int judge = 10; //判斷字段,沒有則為-1

return exportutil.exportautocell("考核評價彙總",key_columns,value_columns,list1,arr,judge); }

}

(3)匯出工具類

public class exportutil 

//把資料新增到excel

for (int i = 0; i < list.size(); i++)

}//自動合併

for (int k = 0; k < field_arr.length; k++)

if(list.get(i).get(field).equals(list.get(i-1).get(field)))

}int rowspan = 2;

d = rowspan;

for (int j = 2; i-j >= 0; j++) else

}rowspan = j+1;

d = rowspan;}}

sheet.addmergedregion(new cellrangeaddress(i-rowspan+2, i+1, field, field));

}else }}

return workbook;

}/**

* * @author zhangshizhe

* @date 2023年6月15日 下午1:53:52

* @description 利用反射獲取get方法

* @fcunction getfiled

* @param object

* @param field

* @return string**/

public static string getfiled(object object, string field)

return invoke.tostring();

}} catch (exception e)

return "";}}

注:合併導org.apache.poi.ss.util.cellrangeaddress包,org.apache.poi.hssf.util.cellrangeaddress包已廢棄;

import org.apache.poi.ss.util.cellrangeaddress;
合併addmergedregion方法四個屬性分別是(開始行,結束行,開始列,結束列)

sheet.addmergedregion(new cellrangeaddress(0, 1, 0, 1));

使用poi匯出excel

使用poi匯出excel,其中涉及到居中 字型加粗 合併單元格 namespace org examinee results inputname inputstream buffersize 4096 public class orgexamineeexportaction extends nati...

使用POI匯出Excel示例

使用poi匯出excel,需要的jar包 poi bin 3.9 20121203.zip 示例 如下 import org.apache.poi.hssf.usermodel.hssfcell import org.apache.poi.hssf.usermodel.hssfcellstyle i...

使用POI匯出excel檔案

fileoutputstream fileout new fileoutputstream workbook wb new hssfworkbook 建議使用介面來宣告變數 面向介面思想,可以方便使用介面的不同實現 wb.write fileout 寫入檔案 fileout.close sheet ...