java 实现excel样式设置(居中、字体、大小、换行、合并行,列宽、指定特定字符串样式等) | 您所在的位置:网站首页 › excel怎么合并后换行 › java 实现excel样式设置(居中、字体、大小、换行、合并行,列宽、指定特定字符串样式等) |
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
HSSFWorkbook workbook = new HSSFWorkbook(); Sheet sheet = workbook.createSheet(); sheet.setColumnWidth(0, 30*256);//设置当前sheet页第一列宽度 sheet.setColumnWidth(1, 70*256);//第二列宽度 CellStyle style = workbook.createCellStyle(); Font font = workbook.createFont(); font.setFontHeightInPoints((short) 13); font.setFontName("仿宋_GB2312"); style.setFont(font5);//增加字体样式 style.setAlignment(CellStyle.ALIGN_CENTER);//增加水平居中样式-old style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);//增加垂直居中样式-old style.setAlignment(HorizontalAlignment.CENTER);//增加水平居中样式 style.setVerticalAlignment(org.apache.poi.ss.usermodel.VerticalAlignment.CENTER);//增加垂直居中样式//excle边框样式添加 style.setBorderBottom(HSSFCellStyle.BORDER_THIN); style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setBorderTop(HSSFCellStyle.BORDER_THIN); style.setBorderRight(HSSFCellStyle.BORDER_THIN); //自动换行样式增加 style.setWrapText(true); Row row=sheet.getRow(0);//获取第一行 if(row==null){sheet.createRow(0);} Cell cell=row.getCell(0);//获取第一个单元格 if(cell==null){row.createCell(0);} cell.setCellStyle(style);//开始设置单元格样式//设置单元格合并 int startrow=1; int endrow=1; int startcol=0; int endcol=0; CellRangeAddress region = new CellRangeAddress (startrow, endrow, startcol, endcol); sheet.addMergedRegion(region); ---------------------------------指定部分字符串样式 String content="测试数据"; Font font = workbook.createFont(); font.setFontHeightInPoints((short) 13); font.setFontName("仿宋_GB2312"); Font font2 = workbook.createFont(); font2.setUnderline((byte)1); HSSFRichTextString hts = new HSSFRichTextString(content); int n = content.indexOf("测试"); hts.applyFont(n, n + 2, font); //从第n位开始,n+2位字符设置字体样式 hts.applyFont(1,4,font2);//第2位到第4位字符串下添加下划线 cell.setCellValue(hts); ------------------------------ |
CopyRight 2018-2019 实验室设备网 版权所有 |