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、添加依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itzheng</groupId>
<artifactId>POIDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
</dependencies>
</project>
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、创建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();
}
}
}
}
(二)合并单元格
//合并单元格
//合并:标题
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));
扫描二维码关注公众号,回复:
12426899 查看本文章

(三)设置内容
//必须先有创建的行和单元格,才可以使用
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("订单明细");
(四)设置行高和列宽
//设置行高于列宽
//标题行高
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);
}
(五)设置对齐方式和字体
//设置水平对其方式为居中
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);
全部代码
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();
}
}
}
}