概述
PoiTest.java
进行判断的数据不能为空,将从数据库查询出的List转换为二维数组进行操作。
package test;
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.ss.util.CellRangeAddress;
import com.pojo.PoiModel;
public class PoiTest {
public static void main(String[] args) {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("sheet1");
HSSFRow row = sheet.createRow((int) 0);
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
String[] excelHeader = {"第一列标题","第二列标题","第三列标题","第四列标题"};
String[][] goodsArray= {{"11","12","13","14"},{"11","22","23","24"},{"11","22","33","24"}};
for (int i = 0; i < excelHeader.length; i++) {
HSSFCell cell = row.createCell(i);
cell.setCellValue(excelHeader[i]);
cell.setCellStyle(style);
sheet.setColumnWidth(i, 20 * 200);
}
for (int i = 0; i < goodsArray.length; i++) {
row = sheet.createRow(i + 1);
for (int j = 0; j < goodsArray[0].length; j++) {
HSSFCell cell =row.createCell(j);
cell.setCellStyle(style);
}
}
int rowNum=goodsArray.length;//行数
int coloumNum=goodsArray[0].length;//列数
int index=0;
for (int i = 0; i < coloumNum; i++) {
index=0;
for (int j = 0; j < rowNum; j++) {
PoiModel poimodel=new PoiModel();
poimodel.setContent(goodsArray[j][i].toString());
if(j==rowNum-1) {
CellRangeAddress cra=new CellRangeAddress((short)index+1/*重复数据起始行,第0行为标题*/, (short)j+1/*到第几行*/, (short)i/*从某一列开始*/, (short)i/*到第几列*/);
sheet.addMergedRegion(cra);
sheet.getRow(index+1).getCell(i).setCellValue(poimodel.getContent());
}else {
poimodel.setOldContent(goodsArray[j+1][i].toString());
}
// String content=goodsArray[j][i].toString();
// String newContent=goodsArray[j+1][i].toString();
if(!poimodel.getContent().equals(poimodel.getOldContent())) {
CellRangeAddress cra=new CellRangeAddress((short)index+1/*重复数据起始行,第0行为标题*/, (short)j+1/*到第几行*/, (short)i/*从某一列开始*/, (short)i/*到第几列*/);
sheet.addMergedRegion(cra);
sheet.getRow(index+1).getCell(i).setCellValue(poimodel.getContent());
if(j==rowNum-2) {//最后一行与前一行不相同
sheet.getRow(index+2).getCell(i).setCellValue(poimodel.getOldContent());
break;
}
index=j+1;
}
}
}
/*生成临时文件*/
FileOutputStream out = null;
// String localPath = null;
// File tempFile = null;
String fileName = "D:\test.xlsx";
try {
// tempFile = File.createTempFile(fileName, ".xlsx");
// localPath = tempFile.getAbsolutePath();
out = new FileOutputStream(fileName);
wb.write(out);
System.out.println(fileName);
}catch (IOException e){
e.printStackTrace();
}finally {
try {
out.flush();
out.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
PoiModel.java
package com.pojo;
public class PoiModel {
private String content;
private String oldContent;
public String getOldContent() {
return oldContent;
}
public void setOldContent(String oldContent) {
this.oldContent = oldContent;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
最后
以上就是坚强服饰为你收集整理的poi导出数据进行重复行合并(个人练习记录)的全部内容,希望文章能够帮你解决poi导出数据进行重复行合并(个人练习记录)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复