最近搜索

java 代码 指定位置生成excel 表单,设置单元格,格式

浏览:513
管理员 2020-11-10 10:38

创建空的excel

image.png


添加sheet页

image.png

image.png


创建sheet页的行  和  单元格

image.png



设置单元格颜色


		//生成excel
		Workbook wb = new HSSFWorkbook();
		Sheet sheet = wb.createSheet();
		FileOutputStream fos = new FileOutputStream("c:\\1.xls");
		Row row = sheet.createRow(0);
		Cell cell = row.createCell(0);
		CellStyle style =wb.createCellStyle();
		style.setFillForegroundColor(IndexedColors.RED.getIndex());
		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
		
		
		cell.setCellStyle(style );
		cell.setCellValue("混合过关");		
		wb.write(fos);
		fos.close();		

image.png



设置字体颜色  字体大小

		
		//生成excel
		Workbook wb = new HSSFWorkbook();
		Sheet sheet = wb.createSheet();
		FileOutputStream fos = new FileOutputStream("c:\\1.xls");
		Row row = sheet.createRow(0);
		Cell cell = row.createCell(0);
		CellStyle style =wb.createCellStyle();
		//6.1创建字体
        Font font = wb.createFont();
        font.setFontHeightInPoints((short) 36);
        font.setFontName("华文琥珀");
        font.setColor(Font.COLOR_RED);
        style.setFont(font);
		cell.setCellStyle(style);
		cell.setCellValue("混合过关");
        
		wb.write(fos);
		fos.close();

image.png



设置单元格的宽度

		sheet.setColumnWidth(0, 10000);
		sheet.setColumnWidth(1, 10000);

image.png


image.png





设置excel格式


HSSFWorkbook wb = new HSSFWorkbook();

HSSFSheet sheet = wb.createSheet();

HSSFCellStyle setBorder = wb.createCellStyle();

一、设置背景色:

setBorder.setFillForegroundColor((short) 13);// 设置背景色
setBorder.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

二、设置边框:

setBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
setBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
setBorder.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
setBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框

三、设置居中:

setBorder.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中

四、设置字体:

HSSFFont font = wb.createFont();
font.setFontName("黑体");
font.setFontHeightInPoints((short) 16);//设置字体大小

HSSFFont font2 = wb.createFont();
font2.setFontName("仿宋_GB2312");
font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
font2.setFontHeightInPoints((short) 12);

setBorder.setFont(font);//选择需要用到的字体格式

五、设置列宽:

sheet.setColumnWidth(0, 3766); //第一个参数代表列id(从0开始),第2个参数代表宽度值

六、设置自动换行:

setBorder.setWrapText(true);//设置自动换行

七、合并单元格:

Region region1 = new Region(0, (short) 0, 0, (short) 6);

//参数1:行号 参数2:起始列号 参数3:行号 参数4:终止列号
sheet.addMergedRegion(region1);

或者用

CellRangeAddress region1 = new CellRangeAddress(rowNumber, rowNumber, (short) 0, (short) 11);

但应注意两个构造方法的参数不是一样的,具体使用哪个取决于POI的不同版本。
sheet.addMergedRegion(region1);

目前用过的就这么多,后续有新的会继续添加。

八、加边框

  HSSFCellStyle cellStyle= wookBook.createCellStyle();
  cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
  cellStyle.setBorderBottom(HSSFCellStyle.BorderBORDER_MEDIUM);
  cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
  cellStyle.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
  cellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
  cellStyle.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
  cellStyle.setRightBorderColor(HSSFColor.BLACK.index);
  cellStyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
  cellStyle.setTopBorderColor(HSSFColor.BLACK.index);

九、字体颜色










设置线的粗细

image.png



中号线

image.png



小号线

image.png

//BORDER_MEDIUM中号的线
					//BORDER_THIN小号的线
					CellStyle style = wb.createCellStyle();
			        style.setBorderBottom(CellStyle.BORDER_THIN);
			        style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
			        style.setBorderLeft(CellStyle.BORDER_THIN);
			        style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
			        style.setBorderRight(CellStyle.BORDER_THIN);
			        style.setRightBorderColor(IndexedColors.BLACK.getIndex());
			        style.setBorderTop(CellStyle.BORDER_THIN);
			        style.setTopBorderColor(IndexedColors.BLACK.getIndex());
			       // cell.setCellStyle(style);


联系站长

站长微信:xiaomao0055

站长QQ:14496453