最近项目中需要导出excel并在头部添加客户需要的表头,项目是基于peimefaces做的,生成excel直接使用组件生成的,生成代码如下:
<p:commandButton styleClass="editBtn" ajax="false"
rendered="#{request.isUserInRole('ROLE_BIOINFO')}"value="导出SampleSheet" id="export_csv1" update="msgs,runPerSamples">
<p:dataExporter type="xls" target="runPerSamples" fileName="runPerSamples"
postProcessor="#{runPerSampleList.postProcessXLS}"
selectionOnly="true"/>
</p:commandButton>
但是在后台直接增加行的办法试了好几种都没有效果,后来在网上找到一种先复制行然后其他的数据往下移动,然后在根据需求来编辑复制行,下面是代码:
public void postProcessXLS(Object document) throws IOException {
//获取excel
HSSFWorkbook wb = (HSSFWorkbook) document;
HSSFSheet sheet = wb.getSheetAt(0);
//根据获取的excel复制添加行
for(int i=0;i<19;i++){
copyRow(wb,sheet,0,1);
}
//根据需求设计表头
HSSFCell title = sheet.createRow(0).createCell(0);//标题的行,列
title.setCellType(HSSFCell.CELL_TYPE_STRING);//列的样式
title.setCellValue("[Header]");//标题内容
// sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 11));//添加标题合并
//放入流中下载
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
externalContext.