一、pom依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.0.1</version>
</dependency>
二、代码如下
import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
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.HSSFColor;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
public class POICellMergeDemo3 {
public static void main(String[] args) {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("表1");
CellStyle style = wb.createCellStyle();
style.setBorderBottom(BorderStyle.THIN);
style.setBorderBottom(BorderStyle.THIN);
style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderLeft(BorderStyle.THIN);
style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderTop(BorderStyle.THIN);
style.setTopBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderRight(BorderStyle.THIN);
style.setRightBorderColor(IndexedColors.BLACK.getIndex());
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
sheet.setColumnWidth(0, 2800);
sheet.setColumnWidth(1, 2800);
sheet.setColumnWidth(2, 2800);
sheet.setColumnWidth(3, 2800);
sheet.setColumnWidth(4, 4500);
sheet.setColumnWidth(5, 2800);
sheet.setColumnWidth(6, 2800);
sheet.setColumnWidth(7, 2800);
sheet.setColumnWidth(8, 2800);
try {
HSSFRow row = null;
HSSFCell cell = null;
for (int i = 0; i < 6; i++) {
row = sheet.createRow(i);
for (int j = 0; j <= 8; j++) {
cell = row.createCell(j);
cell.setCellStyle(style);
}
}
cell = sheet.getRow(0).getCell(0);
cell.setCellValue(new HSSFRichTextString("转出人员查询展示"));
cell = sheet.getRow(1).getCell(0);
cell.setCellValue(new HSSFRichTextString("基本信息"));
cell = sheet.getRow(1).getCell(6);
cell.setCellValue(new HSSFRichTextString("状态"));
cell = sheet.getRow(2).getCell(0);
cell.setCellValue(new HSSFRichTextString("序号"));
cell = sheet.getRow(2).getCell(1);
cell.setCellValue(new HSSFRichTextString("姓名"));
cell = sheet.getRow(2).getCell(2);
cell.setCellValue(new HSSFRichTextString("年龄"));
cell = sheet.getRow(2).getCell(3);
cell.setCellValue(new HSSFRichTextString("地址"));
cell = sheet.getRow(2).getCell(4);
cell.setCellValue(new HSSFRichTextString("手机号"));
cell = sheet.getRow(2).getCell(5);
cell.setCellValue(new HSSFRichTextString("图画"));
cell = sheet.getRow(2).getCell(6);
cell.setCellValue(new HSSFRichTextString("审批"));
cell = sheet.getRow(2).getCell(7);
cell.setCellValue(new HSSFRichTextString("通过"));
cell = sheet.getRow(2).getCell(8);
cell.setCellValue(new HSSFRichTextString("驳回"));
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 8));
sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 5));
sheet.addMergedRegion(new CellRangeAddress(1, 1, 6, 8));
List<UserTest> list=new LinkedList();
list.add(new UserTest(1, "lili", 17, "新东方", new BigDecimal(1328742894)));
list.add(new UserTest(2, "mola", 34, "赤道几内亚", new BigDecimal(77689372)));
list.add(new UserTest(3, "kobe", 24, "拉不拉卡", new BigDecimal(1897789439)));
list.add(new UserTest(4, "季芳新", 11, "东方帕米尔", new BigDecimal(1991839482)));
int rowIndex=3;
Row nRow = null;
Cell nCell = null;
for(UserTest u:list){
nRow = sheet.createRow(rowIndex++);
int cellIndex = 0;
nCell = nRow.createCell(cellIndex++);
nCell.setCellStyle(style);
nCell.setCellValue(u.getId());
nCell = nRow.createCell(cellIndex++);
nCell.setCellStyle(style);
nCell.setCellValue(u.getName());
nCell = nRow.createCell(cellIndex++);
nCell.setCellStyle(style);
nCell.setCellValue(u.getAge());
nCell = nRow.createCell(cellIndex++);
nCell.setCellStyle(style);
nCell.setCellValue(u.getAddr());
nCell = nRow.createCell(cellIndex++,CellType.NUMERIC);
nCell.setCellStyle(style);
nCell.setCellValue(u.getPhone().doubleValue());
}
FileOutputStream fileOut = new FileOutputStream("d:\\转出查询报表1.xls");
wb.write(fileOut);
fileOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}