poi生成excel


记忆自己的学习(虽然很简单。。。)
首先,我写的是maven项目

需要的包

(可能有些可以删,但我还没有尝试…)

<dependency>
		<groupId>org.apache.poi</groupId>
		<artifactId>poi</artifactId>
		<version>3.16</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
	<dependency>
		<groupId>org.apache.poi</groupId>
		<artifactId>poi-ooxml</artifactId>
		<version>3.16</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/log4j/log4j -->
	<dependency>
		<groupId>log4j</groupId>
		<artifactId>log4j</artifactId>
		<version>1.2.13</version>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
		<version>2.1.3.RELEASE</version>
	</dependency>
	
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-test</artifactId>
	    <version>5.1.5.RELEASE</version>
	    <scope>test</scope>
	</dependency>
	
	<dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-excelant</artifactId>
        <version>3.16</version>
    </dependency>

每一格的宽度和高度

	public static void main(String[] args) {
		// 第一步创建workbook
		HSSFWorkbook wb = new HSSFWorkbook();
		// 第二步创建sheet
		HSSFSheet sheet = wb.createSheet("测试");
		sheet.setColumnWidth(0, 700);//设置第一格的宽度,下标从0开始
		sheet.setColumnWidth(1, 700);
		sheet.setColumnWidth(2, 700);
		sheet.setColumnWidth(3, 700);
		sheet.setColumnWidth(4, 700);
		sheet.setColumnWidth(5, 700);
		sheet.setColumnWidth(6, 700);
		sheet.setColumnWidth(7, 4030);
		sheet.setColumnWidth(8, 1300);
		sheet.setColumnWidth(9, 1300);
		sheet.setColumnWidth(10, 1300);
		sheet.setColumnWidth(11, 1300);
		sheet.setColumnWidth(12, 1300);
		sheet.setColumnWidth(13, 1300);
		sheet.setColumnWidth(14, 1300);
		sheet.setColumnWidth(15, 1300);
		sheet.setColumnWidth(16, 1300);
		sheet.setColumnWidth(17, 1300);
		sheet.setColumnWidth(18, 800);
		sheet.setColumnWidth(19, 700);
		sheet.setColumnWidth(20, 400);
		sheet.setColumnWidth(21, 4030);

		// 第三步创建行row:添加表头0行
		HSSFRow row_1 = sheet.createRow(0);
		row_1.setHeight((short) 350);// 设置行高
		HSSFRow row_2 = sheet.createRow(1);
		row_2.setHeight((short) 350);// 设置行高
		HSSFRow row_3 = sheet.createRow(2);
		row_3.setHeight((short) 500);// 设置行高
		HSSFRow row_4 = sheet.createRow(3);
		row_4.setHeight((short) 500);// 设置行高
		HSSFRow row_5 = sheet.createRow(4);
		row_5.setHeight((short) 500);// 设置行高
		HSSFRow row_6 = sheet.createRow(5);
		row_6.setHeight((short) 800);// 设置行高
		HSSFRow row_7 = sheet.createRow(6);
		row_7.setHeight((short) 800);// 设置行高
		HSSFCellStyle style = wb.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		// 设置自动换行;
		style.setWrapText(true);//表格里面的文字的换行

		//第四步,创建样式
		// ----------------标题样式---------------------
		HSSFCellStyle titleStyle = wb.createCellStyle(); // 标题样式
		titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		Font ztFont = wb.createFont();
		ztFont.setItalic(false); // 设置字体为斜体字
		ztFont.setColor(Font.COLOR_NORMAL); // 将字体设置为“红色”
		ztFont.setFontHeightInPoints((short) 14); // 将字体大小设置为18px
		ztFont.setFontName("宋体"); // 将“宋体”字体应用到当前单元格上
		ztFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 加粗
		titleStyle.setFont(ztFont);//将字体样式放在标题样式中

		// ----------------单元格样式----------------------------------
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		Font cellFont = wb.createFont();
		cellFont.setItalic(false); // 设置字体为斜体字
		cellFont.setColor(Font.COLOR_NORMAL); // 将字体设置为“红色”
		cellFont.setFontHeightInPoints((short) 10); // 将字体大小设置为18px
		cellFont.setFontName("宋体"); // 字体应用到当前单元格上
		style.setFont(cellFont);

		// 第五步创建单元格
		HSSFCell cell_1 = row_1.createCell(0); // 第一个单元格
		sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 21));
		cell_1.setCellValue("现场验收检查原始记录");
		cell_1.setCellStyle(titleStyle);

		HSSFCell cell_2 = row_2.createCell(16); // 第二行第17格
		cell_2.setCellValue("共");

		HSSFCell cell_3 = row_2.createCell(17); // 第二行第18格
		cell_3.setCellValue("页");

		HSSFCell cell_4 = row_2.createCell(19); // 第二行第20格
		cell_4.setCellValue("第");

		HSSFCell cell_5 = row_2.createCell(20); // 第二行第21格
		cell_5.setCellValue("1");

		HSSFCell cell_6 = row_3.createCell(0); // 第三行第1格
		sheet.addMergedRegion(new CellRangeAddress(2, 2, 0, 4));
		cell_6.setCellValue("单位");
		cell_6.setCellStyle(style);

		HSSFCell cell_7 = row_3.createCell(5); // 第三行第2格
		sheet.addMergedRegion(new CellRangeAddress(2, 2, 5, 21));
		cell_7.setCellValue("B1-2地块工程");
		cell_7.setCellStyle(style);

		HSSFCell cell_8 = row_4.createCell(0); // 第四行第1格
		sheet.addMergedRegion(new CellRangeAddress(3, 3, 0, 4));
		cell_8.setCellValue("名称");
		cell_8.setCellStyle(style);

		HSSFCell cell_9 = row_4.createCell(5); // 第四行第2格
		sheet.addMergedRegion(new CellRangeAddress(3, 3, 5, 7));
		cell_9.setCellValue("质量验收记录");
		cell_9.setCellStyle(style);

		HSSFCell cell_10 = row_4.createCell(8); // 第四行第3格
		sheet.addMergedRegion(new CellRangeAddress(3, 3, 8, 14));
		cell_10.setCellValue("编号");
		cell_10.setCellStyle(style);

		HSSFCell cell_11 = row_4.createCell(15); // 第四行第4格
		sheet.addMergedRegion(new CellRangeAddress(3, 3, 15, 21));
		cell_11.setCellValue("020105018597");
		cell_11.setCellStyle(style);

		HSSFCell cell_12 = row_5.createCell(0); // 第五行第1格
		sheet.addMergedRegion(new CellRangeAddress(4, 4, 0, 7));
		cell_12.setCellValue("项目");
		cell_12.setCellStyle(style);

		HSSFCell cell_13 = row_5.createCell(8); // 第五行第2格
		sheet.addMergedRegion(new CellRangeAddress(4, 4, 8, 17));
		cell_13.setCellValue("情况记录");
		cell_13.setCellStyle(style);

		HSSFCell cell_14 = row_5.createCell(18); // 第五行第3格
		sheet.addMergedRegion(new CellRangeAddress(4, 4, 18, 21));
		cell_14.setCellValue("备注");
		cell_14.setCellStyle(style);

		HSSFCell cell_15 = row_6.createCell(0); // 第六行第1格
		sheet.addMergedRegion(new CellRangeAddress(5, 6, 0, 7));
		cell_15.setCellValue("轴线位移");
		cell_15.setCellStyle(style);

		HSSFCell cell_16 = row_6.createCell(11); //
		sheet.addMergedRegion(new CellRangeAddress(5, 5, 18, 21));
		cell_16.setCellStyle(style);

		HSSFCell cell_17 = row_6.createCell(11); //
		sheet.addMergedRegion(new CellRangeAddress(6, 6, 18, 21));
		cell_17.setCellStyle(style);
		
		try {
			//将生成的excel保存到指定的路径
			FileOutputStream fout = new FileOutputStream("D://data//b.xls");
			wb.write(fout);
			fout.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("Excel文件生成成功...");
}

最后成品

不会插图片,告辞。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值