POI操作excel修改样式

public static void main(String[] args) throws IOException {
		//读取工作簿模板
		FileInputStream fileInputStream = new FileInputStream(new File("D:/workbook.xlsx"));
		XSSFWorkbook sheets = new XSSFWorkbook(fileInputStream);
		//创建表格
		XSSFSheet sheet = sheets.getSheetAt(0);
		//设置列宽
		sheet.setColumnWidth(0,4000);
		//创建行
		XSSFRow row = sheet.getRow(0);
		//创建列
		XSSFCell cell = row.getCell(0);
		//向列中写入数据
		cell.setCellValue("successful");
		//创建列样式
		XSSFCellStyle cellStyle = sheets.createCellStyle();
		//设置填充模式
//		cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
		//设置背景颜色
//		cellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
		//设置边框
//		cellStyle.setBorderBottom(BorderStyle.DOUBLE);
		//设置对齐方式
		cellStyle.setAlignment(HorizontalAlignment.LEFT);
		//设置自动换行
		cellStyle.setWrapText(true);
		//创建字体样式
		XSSFFont font = sheets.createFont();
		//设置字体
		font.setFontName("宋体");
		//设置字的大小
		font.setFontHeightInPoints((short) 16);
		//设置粗体
		font.setBold(true);
		cellStyle.setFont(font);
		//设置列样式
		cell.setCellStyle(cellStyle);
		//将工作簿中的内容写入文件
		FileOutputStream fileOutputStream = new FileOutputStream(new File("D:/workbook1.xlsx"));
		sheets.write(fileOutputStream);
		fileOutputStream.close();
		sheets.close();
	}

猜你喜欢

转载自blog.csdn.net/weixin_39841589/article/details/83270889