需要导入poi-3.9-20121203.jar
import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
public class Poi5 {
public static void main(String[] args) throws Exception {
//定义一个新的工作簿
Workbook wb=new HSSFWorkbook();
//创建sheet1
Sheet sheet = wb.createSheet("sheet1");
//创建行
Row row = sheet.createRow(5);
//设置行高
row.setHeightInPoints(30);
Cell cell = row.createCell(1);
Cell cell2 = row.createCell(2);
cell.setCellValue(new HSSFRichTextString("合并单元格"));
//单元格样式类
CellStyle cellStyle=wb.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直
cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED); // 上边边框虚线
cellStyle.setTopBorderColor(IndexedColors.GREEN.getIndex()); //颜色
cellStyle.setFillForegroundColor(IndexedColors.RED.getIndex()); // 前景色
cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell.setCellStyle(cellStyle);
CellStyle cellStyle2=wb.createCellStyle();
cellStyle2.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED); // 上边边框虚线
cellStyle2.setTopBorderColor(IndexedColors.GREEN.getIndex()); //颜色
cell2.setCellStyle(cellStyle2);
//合并单元格
sheet.addMergedRegion(new CellRangeAddress(
5, // 起始行
5, // 结束行
1, // 其实列
2 // 结束列
));
//创建一个输入流
FileOutputStream fileOutputStream = new FileOutputStream("d:\\简单的工作簿.xls");
//写入
wb.write(fileOutputStream);
}
}
效果图