poi导出excel合并单元格,poi根据模板导出excel

  • Excel合并
  • 2024-04-16

poi导出excel合并单元格?HSSFCell cell = row.createCell((short) (celln + 1));cell.setCellValue("merging" + i); // 跨单元格显示的数据 cell.setCellStyle(style); // 样式 // 不跨单元格显示的数据,如:分两行,那么,poi导出excel合并单元格?一起来了解一下吧。

easypoi导出excel模板

/**

*

*@paramcontext

*@paramdictionary

*@paramrows数据行

*@paramfileName文件名

*@paramfields列名

*@paramfieldsName字段名

*/

privatevoidoutExcelFile(HttpContextcontext,

IContextDictionarydictionary,DataRowCollectionsrows,

StringfileName,Listfields,ListfieldsName){

intcellSize=fields.size();

if(cellSize==0){

LogManager.debug("没有指定列头信息,无法导出Excel文件!");

return;

}

//============创建样式start

HSSFWorkbookworkbook=newHSSFWorkbook();

HSSFSheetsheet=workbook.createSheet();

//列头字体样式

HSSFFontheaderFont=workbook.createFont();

headerFont.setFontName("宋体");

headerFont.setFontHeightInPoints((short)12);

headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

//列头样式

HSSFCellStyleheaderStyle=workbook.createCellStyle();

headerStyle.setFont(headerFont);

headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中

headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中

headerStyle.setLocked(true);

headerStyle.setWrapText(true);

//标题样式

HSSFFonttitleFont=workbook.createFont();

titleFont.setFontName("宋体");

titleFont.setFontHeightInPoints((short)15);

titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

HSSFCellStyletitleStyle=workbook.createCellStyle();

titleStyle.setFont(titleFont);

titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);

titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

titleStyle.setLocked(true);

titleStyle.setWrapText(true);

//普通单元格字体样式

HSSFFontcellFont=workbook.createFont();

cellFont.setFontName("宋体");

cellFont.setFontHeightInPoints((short)12);

//普通单元格样式

HSSFCellStylecellStyle=workbook.createCellStyle();

cellStyle.setFont(cellFont);

cellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);//左右居中

cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中

cellStyle.setLocked(true);

cellStyle.setWrapText(true);

//============创建样式end

//设置序号列、列宽和标题行start

HSSFRowtitleRow=sheet.createRow(0);

titleRow.setHeightInPoints(50);

HSSFCelltitleCell=titleRow.createCell(0);

titleCell.setCellValue(newHSSFRichTextString(fileName));

titleCell.setCellStyle(titleStyle);

//sheet.addMergedRegion(newRegion(0,(short)0,0,(short)cellSize));//合并单元格--方法过时

//CellRangeAddress起始行结束行起始列结束列

sheet.addMergedRegion(newCellRangeAddress(0,0,(short)0,(short)cellSize));//合并单元格

//显示总的数据个数start

HSSFRowcountRow=sheet.createRow(1);

countRow.setHeightInPoints(40);

HSSFCellcountCell=countRow.createCell(0);

countCell.setCellValue(newHSSFRichTextString("共计专项检查("+rows.size()+")项"));

countCell.setCellStyle(headerStyle);

sheet.addMergedRegion(newCellRangeAddress(1,1,(short)0,(short)cellSize));//合并单元格

//显示总的数据个数end

HSSFRowheaderRow=sheet.createRow(2);

headerRow.setHeightInPoints(35);

HSSFCellheaderCell=null;

//序号

intstartIndex=0;

headerCell=headerRow.createCell(0);

sheet.setColumnWidth(0,2000);

headerCell.setCellValue(newHSSFRichTextString("序号"));

headerCell.setCellStyle(headerStyle);

startIndex++;

//列头

for(inti=0;i

sheet.setColumnWidth(startIndex+i,7000);

headerCell=headerRow.createCell(startIndex+i);

headerCell.setCellValue(newHSSFRichTextString(fields.get(i)));

headerCell.setCellStyle(headerStyle);

}

//设置序号列、列宽和标题行end

//文件正文start

introwNum=1;

introwIndex=0;

HSSFRowrow=null;

ListcellRangeLst=newArrayList(0);

Integer[]arr=null;

intl=0;

StringorgName="";

for(intj=2;j

DataRowdataRow=rows.get(rowIndex);//对应数据库字段

HSSFCellcell=null;

row=sheet.createRow(j+1);

row.setHeightInPoints(55);

//序号

cell=row.createCell(0);

cell.setCellValue(rowNum++);

cell.setCellStyle(cellStyle);

if(StringHelper.isNullOrEmpty(orgName)){

arr=newInteger[2];

arr[0]=j+1;

l=j+1;

orgName=dataRow.getString("ORGNAME");

}else{

if(!orgName.equals(dataRow.getString("ORGNAME"))){

arr[1]=j;

cellRangeLst.add(arr);

sheet.addMergedRegion(newCellRangeAddress(l,j,1,1));

arr=newInteger[2];

l=j+1;

orgName=dataRow.getString("ORGNAME");

}

if(rowIndex==rows.size()-1){

arr[1]=j+1;

cellRangeLst.add(arr);

sheet.addMergedRegion(newCellRangeAddress(l,j+1,1,1));

}

}

for(intk=0;k

cell=row.createCell(k+startIndex);

Stringcolumn=fieldsName.get(k);//对应数据库字段

Stringvalue="";

if("APSJ".equals(column)){

value=getAPSJValue(dataRow.getString(column));

}else{

value=dataRow.getString(column);

}

cell.setCellValue(newHSSFRichTextString(value));

cell.setCellStyle(cellStyle);

}

rowIndex++;

}

//文件正文end

//for(Integer[]te:cellRangeLst){

//sheet.addMergedRegion(newCellRangeAddress(te[0],te[1],1,1));//合并处室单元格

//}

//下载

HttpServletResponseresponse=context.getResponse();

response.setContentType("application/x-download;charset=UTF-8");

Stringtitle="export";

try{

title=java.net.URLEncoder.encode(fileName,"UTF-8");

}catch(UnsupportedEncodingExceptione){

e.printStackTrace();

}

response.addHeader("Content-Disposition","attachment;filename="+title+".xls");

try{

OutputStreamout=response.getOutputStream();

workbook.write(out);

out.flush();

out.close();

}catch(IOExceptione){

e.printStackTrace();

}

}

//参考一下吧

poi导出excel内存溢出

你是想用JAVA生成这样一个文档么?是的话留下邮箱,我写了一个工具类,发给你。这个不是一两句代码就搞定的。

poi导出excel的流程

/**

* 合并单元格处理,获取合并行

* @param sheet

* @return List

*/

public List getCombineCell(Sheet sheet)

{

List list = new ArrayList();

//获得一个 sheet 中合并单元格的数量

int sheetmergerCount = sheet.getNumMergedRegions();

//遍历合并单元格

for(int i = 0; i

{

//获得合并单元格加入list中

CellRangeAddress ca = sheet.getMergedRegion(i);

list.add(ca);

}

return list;

}

/**

* 判断单元格是否为合并单元格,是的话则将单元格的值返回

* @param listCombineCell 存放合并单元格的list

* @param cell 需要判断的单元格

* @param sheet sheet

* @return

*/

easypoi导出excel实例

单元格合并为一个单元设置方法如下:

1、首先打开excel文档,在其excel软件的菜单栏找到开始选项,然后打开。

2、按住鼠标左键拖动选中将要合并的几个单元格。

3、在选中的单元格内单击鼠标右键打开菜单,在里面找到合并选项,也可直接在菜单栏点击合并选项可以选择合并居中或者其他,选择合并居中后文字就会自动变到合并后单元格最中间。

4、这样就可以把多个单元格合并成一个了。

单元格含义:

单元格是表格中行与列的交叉部分,它是组成表格的最小单位,可拆分或者合并。单个数据的输入和修改都是在单元格中进行的。单元格属于Microsoft Excel /WPS表格中使用的术语。

单元格类型定义了在单元格中呈现的信息的类型,以及这种信息如何显示,用户如何与其进行交互。用户可以 使用两种不同的单元格类型对表单中的单元格进行设置:一种是可以简单地关联于单元格的文本格式,另一种就是显示控件或者图形化信息。

Excel中内置有通用、货币、数字、百分比、文本类型。而第三方表格控件往往会有更丰富的单元格类型,比如Spread会支持18种单元格类型(Mask型、按钮型、复选框型、组合框型、图形型等)。

poi根据模板导出excel

POI生成excel表格,如何合并单元格

Java代码

import java.io.FileOutputStream;

import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.hssf.util.Region;

public class ExcelTest {

/**

* @param args

*/

public static void main(String[] args) throws IOException {

try {

HSSFWorkbook wb = new HSSFWorkbook();

HSSFSheet sheet = wb.createSheet("new sheet");

HSSFCellStyle style = wb.createCellStyle(); // 样式对象

style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直

style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平

HSSFRow row = sheet.createRow((short) 0);

HSSFRow row2 = sheet.createRow((short) 1);

sheet.addMergedRegion(new Region(0, (short) 0, 1, (short) 0));

HSSFCell ce = row.createCell((short) 0);

ce.setEncoding(HSSFCell.ENCODING_UTF_16);// 中文处理

ce.setCellValue("项目\\日期"); // 表格的第一行第一列显示的数据

ce.setCellStyle(style); // 样式,居中

int num = 0;

for (int i = 0; i < 9; i++) { // 循环9次,每一次都要跨单元格显示

// 计算从那个单元格跨到那一格

int celln = 0;

int celle = 0;

if (i == 0) {

celln = 0;

celle = 1;

} else {

celln = (i * 2);

celle = (i * 2 + 1);

}

// 单元格合并

// 四个参数分别是:起始行,起始列,结束行,结束列

sheet.addMergedRegion(new Region(0, (short) (celln + 1), 0,

(short) (celle + 1)));

HSSFCell cell = row.createCell((short) (celln + 1));

cell.setCellValue("merging" + i); // 跨单元格显示的数据

cell.setCellStyle(style); // 样式

// 不跨单元格显示的数据,如:分两行,上一行分别两格为一格,下一行就为两格,“数量”,“金额”

HSSFCell cell1 = row2.createCell((short) celle);

HSSFCell cell2 = row2.createCell((short) (celle + 1));

cell1.setEncoding(HSSFCell.ENCODING_UTF_16);

cell1.setCellValue("数量");

cell1.setCellStyle(style);

cell2.setEncoding(HSSFCell.ENCODING_UTF_16);

cell2.setCellValue("金额");

cell2.setCellStyle(style);

num++;

}

// 在后面加上合计百分比

// 合计 在最后加上,还要跨一个单元格

sheet.addMergedRegion(new Region(0, (short) (2 * num + 1), 0,

(short) (2 * num + 2)));

HSSFCell cell = row.createCell((short) (2 * num + 1));

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

cell.setCellValue("合计");

cell.setCellStyle(style);

HSSFCell cell1 = row2.createCell((short) (2 * num + 1));

HSSFCell cell2 = row2.createCell((short) (2 * num + 2));

cell1.setEncoding(HSSFCell.ENCODING_UTF_16);

cell1.setCellValue("数量");

cell1.setCellStyle(style);

cell2.setEncoding(HSSFCell.ENCODING_UTF_16);

cell2.setCellValue("金额");

cell2.setCellStyle(style);

// 百分比 同上

sheet.addMergedRegion(new Region(0, (short) (2 * num + 3), 0,

(short) (2 * num + 4)));

HSSFCell cellb = row.createCell((short) (2 * num + 3));

cellb.setEncoding(HSSFCell.ENCODING_UTF_16);

cellb.setCellValue("百分比");

cellb.setCellStyle(style);

HSSFCell cellb1 = row2.createCell((short) (2 * num + 3));

HSSFCell cellb2 = row2.createCell((short) (2 * num + 4));

cellb1.setEncoding(HSSFCell.ENCODING_UTF_16);

cellb1.setCellValue("数量");

cellb1.setCellStyle(style);

cellb2.setEncoding(HSSFCell.ENCODING_UTF_16);

cellb2.setCellValue("金额");

cellb2.setCellStyle(style);

FileOutputStream fileOut = new FileOutputStream("workbook.xls");

wb.write(fileOut);

fileOut.close();

System.out.print("OK");

} catch (Exception ex) {

ex.printStackTrace();

}

}

}

以上就是poi导出excel合并单元格的全部内容,//遍历合并单元格 for(int i = 0; i

猜你喜欢