【无标题】

Java 操作Excel表格(POI)(Maven),创建表格,画边框线,合并单元格,设置内容,设置行高和列宽,设置对齐方式和字体

北芳科技

于 2020-12-24 15:20:40 发布

448
收藏 3
分类专栏: Maven 文章标签: java maven 大数据 poi
版权

Maven
专栏收录该内容
9 篇文章2 订阅
订阅专栏
Java 操作Excel表格
一、创建表格
1、POI介绍
Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能。POI为“Poor Obfuscation Implementation”的首字母缩写,意为“可怜的模糊实现”。
用它可以使用Java读取和创建,修改MS Excel文件.而且,还可以使用Java读取和创建MS Word和MSPowerPoint文件。Apache POI 提供Java操作Excel解决方案(适用于Excel97-2008)

结构:
HSSF - 提供读写Microsoft Excel XLS格式档案的功能。
XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能。
HWPF - 提供读写Microsoft Word DOC格式档案的功能。
HSLF - 提供读写Microsoft PowerPoint格式档案的功能。
HDGF - 提供读Microsoft Visio格式档案的功能。
HPBF - 提供读Microsoft Publisher格式档案的功能。
HSMF - 提供读Microsoft Outlook格式档案的功能。

2、入门程序

3、添加依赖

4.0.0 com.itzheng POIDemo 0.0.1-SNAPSHOT org.apache.poi poi 3.15 1 2 3 4 5 6 7 8 9 10 11 12 13 4、创建POIDemo

package com.itzheng.demo.poi;
import java.io.File;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
public class POIDemo {
public static void main(String[] args) {
//创建一个工作簿
HSSFWorkbook wk = new HSSFWorkbook();
//创建工作表
HSSFSheet sheet = wk.createSheet(“测试”);
//创建一行,行的索引是从0开始
HSSFRow row = sheet.createRow(0);
//创建单元格,列的索引是从0 开始
HSSFCell cell = row.createCell(0);
//给单元格赋值
cell.setCellValue(“测试”);
//设置列宽
sheet.setColumnWidth(0, 5000);
//width:每个字符的大小,每个字符的大小*256(跟字体有关)
File file = new File(“D:\poitest.xls”);
try {
//保存文件
wk.write(file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
//关闭流
wk.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
运行,创建成功

二、特定格式的表格
(一)画边框线
边框线是属于样式范畴,而样式的作用范围是整个工作簿(包括所有的工作表),因此我们可以工作簿来创建样式,再给样式设置边框线

1、创建CreateOrdersTemplate类

package com.itzheng.demo.poi;

import java.io.File;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.BorderStyle;

/*

  • 导出的订单模板
    */
    public class CreateOrdersTemplate {

    public static void main(String[] args) {

     // 创建一个工作簿
     HSSFWorkbook wb = new HSSFWorkbook();
     // 创建工作表
     HSSFSheet sheet = wb.createSheet("测试");
     // 创建一行,行的索引是从0开始
     HSSFRow row = sheet.createRow(0);
     // 创建单元格,列的索引是从0 开始
     HSSFCell cell = row.createCell(0);
     //创建单元格样式
     HSSFCellStyle style_content = wb.createCellStyle();
     style_content.setBorderBottom(BorderStyle.THIN);//下边框
     style_content.setBorderTop(BorderStyle.THIN);//上边框
     style_content.setBorderLeft(BorderStyle.THIN);//左边框
     style_content.setBorderRight(BorderStyle.THIN);//右边框
     
     //创建11行,4列
     for(int i = 2;i <=12;i++) {
     	row = sheet.createRow(i);
     	for (int j = 0; j < 4; j++) {
     		//给单元格设置样式
     		row.createCell(j).setCellStyle(style_content);
     	}
     }
     
     File file = new File("D:\\ordersTemplate.xls");
     try {
     	//保存文件
     	wb.write(file);
     } catch (IOException e) {
     	// TODO Auto-generated catch block
     	e.printStackTrace();
     }finally {
     	try {
     		//关闭流
     		wb.close();
     	} catch (IOException e) {
     		// TODO Auto-generated catch block
     		e.printStackTrace();
     	}
     }
    

    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62

(二)合并单元格

//合并单元格
//合并:标题
sheet.addMergedRegion(new CellRangeAddress(0,0,0,3));
//合并第二行
sheet.addMergedRegion(new CellRangeAddress(2,2,1,3));
//合并第7行
sheet.addMergedRegion(new CellRangeAddress(7,7,0,3));
1
2
3
4
5
6
7

(三)设置内容
//必须先有创建的行和单元格,才可以使用
sheet.getRow(0).getCell(0).setCellValue(“采购单”);

	sheet.getRow(2).getCell(0).setCellValue("供应商");
	sheet.getRow(3).getCell(0).setCellValue("下单日期");
	sheet.getRow(4).getCell(0).setCellValue("审核日期");
	sheet.getRow(5).getCell(0).setCellValue("采购日期");
	sheet.getRow(6).getCell(0).setCellValue("入库日期 ");
	sheet.getRow(3).getCell(2).setCellValue("经办人");
	sheet.getRow(4).getCell(2).setCellValue("经办人");
	sheet.getRow(5).getCell(2).setCellValue("经办人");
	sheet.getRow(6).getCell(2).setCellValue("经办人");
	
	sheet.getRow(7).getCell(0).setCellValue("订单明细");

1
2
3
4
5
6
7
8
9
10
11
12
13
14

(四)设置行高和列宽
//设置行高于列宽
//标题行高
sheet.getRow(0).setHeight((short)1000);
//内容体的行高
for (int i = 2; i <= 12; i++) {
sheet.getRow(i).setHeight((short)500);
}
//设置列宽
for(int i = 0; i < 4;i++) {
sheet.setColumnWidth(i, (short)5000);
}
1
2
3
4
5
6
7
8
9
10
11

(五)设置对齐方式和字体

//设置水平对其方式为居中
style_content.setAlignment(HorizontalAlignment.CENTER);
//设置垂直对其方式为居中
style_content.setVerticalAlignment(VerticalAlignment.CENTER);

	//设置标题的样式
	HSSFCellStyle style_title = wb.createCellStyle();
	style_title.setAlignment(HorizontalAlignment.CENTER);
	style_title.setVerticalAlignment(VerticalAlignment.CENTER);
	HSSFFont style_font = wb.createFont();
	style_font.setFontName("黑体");
	style_font.setFontHeightInPoints((short)18);
	//加粗
	style_font.setBold(true);
	style_title.setFont(style_font);
	
	//创建内容样式的字体
	HSSFFont font_content = wb.createFont();
	//设置字体名称,相当于选中了那种字符
	font_content.setFontName("宋体");
	//设置字体的大小
	font_content.setFontHeightInPoints((short)11);
	style_content.setFont(font_content);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

全部代码
package com.itzheng.demo.poi;

import java.io.File;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;

/*

  • 导出的订单模板
    */
    public class CreateOrdersTemplate {

    public static void main(String[] args) {

     // 创建一个工作簿
     HSSFWorkbook wb = new HSSFWorkbook();
     // 创建工作表
     HSSFSheet sheet = wb.createSheet("测试");
     // 创建一行,行的索引是从0开始
     HSSFRow row = sheet.createRow(0);
     // 创建单元格,列的索引是从0 开始
     HSSFCell cell = row.createCell(0);
     //创建单元格样式
     HSSFCellStyle style_content = wb.createCellStyle();
     style_content.setBorderBottom(BorderStyle.THIN);//下边框
     style_content.setBorderTop(BorderStyle.THIN);//上边框
     style_content.setBorderLeft(BorderStyle.THIN);//左边框
     style_content.setBorderRight(BorderStyle.THIN);//右边框
     
     
     //设置水平对其方式为居中
     style_content.setAlignment(HorizontalAlignment.CENTER);
     //设置垂直对其方式为居中
     style_content.setVerticalAlignment(VerticalAlignment.CENTER);
     
     //设置标题的样式
     HSSFCellStyle style_title = wb.createCellStyle();
     style_title.setAlignment(HorizontalAlignment.CENTER);
     style_title.setVerticalAlignment(VerticalAlignment.CENTER);
     HSSFFont style_font = wb.createFont();
     style_font.setFontName("黑体");
     style_font.setFontHeightInPoints((short)18);
     //加粗
     style_font.setBold(true);
     style_title.setFont(style_font);
     
     //创建内容样式的字体
     HSSFFont font_content = wb.createFont();
     //设置字体名称,相当于选中了那种字符
     font_content.setFontName("宋体");
     //设置字体的大小
     font_content.setFontHeightInPoints((short)11);
     style_content.setFont(font_content);
     
     //合并单元格
     //合并:标题
     sheet.addMergedRegion(new CellRangeAddress(0,0,0,3));
     //合并第二行
     sheet.addMergedRegion(new CellRangeAddress(2,2,1,3));
     //合并第7行
     sheet.addMergedRegion(new CellRangeAddress(7,7,0,3));
     //创建11行,4列
     
     //创建矩阵11行,4列
     for(int i = 2;i <=12;i++) {
     	row = sheet.createRow(i);
     	for (int j = 0; j < 4; j++) {
     		//给单元格设置样式
     		row.createCell(j).setCellStyle(style_content);
     	}
     }
     //必须先有创建的行和单元格,才可以使用
     //创建标题单元格
     HSSFCell titleCell = sheet.createRow(0).createCell(0);
     titleCell.setCellValue("采购单");
     //设置标题样式
     titleCell.setCellStyle(style_title);
     
     sheet.getRow(2).getCell(0).setCellValue("供应商");
     sheet.getRow(3).getCell(0).setCellValue("下单日期");
     sheet.getRow(4).getCell(0).setCellValue("审核日期");
     sheet.getRow(5).getCell(0).setCellValue("采购日期");
     sheet.getRow(6).getCell(0).setCellValue("入库日期 ");
     sheet.getRow(3).getCell(2).setCellValue("经办人");
     sheet.getRow(4).getCell(2).setCellValue("经办人");
     sheet.getRow(5).getCell(2).setCellValue("经办人");
     sheet.getRow(6).getCell(2).setCellValue("经办人");
     
     sheet.getRow(7).getCell(0).setCellValue("订单明细");
     
     //设置行高于列宽
     //标题行高
     sheet.getRow(0).setHeight((short)1000);
     //内容体的行高
     for (int i = 2; i <= 12; i++) {
     	sheet.getRow(i).setHeight((short)500);
     }
     //设置列宽
     for(int i = 0; i < 4;i++) {
     	sheet.setColumnWidth(i, (short)5000);
     } 
     
     File file = new File("D:\\ordersTemplate.xls");
     try {
     	//保存文件
     	wb.write(file);
     } catch (IOException e) {
     	// TODO Auto-generated catch block
     	e.printStackTrace();
     }finally {
     	try {
     		//关闭流
     		wb.close();
     	} catch (IOException e) {
     		// TODO Auto-generated catch block
     		e.printStackTrace();
     	}
     }
    

    }
    }

原文链接:https://blog.csdn.net/qq_44757034/article/details/111593842

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值