1.导入 poi 组件
<!-- excel word 处理组件 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.15</version>
</dependency>
<!-- 工具 -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
2.word 文档生成,使用word模板,将数据填充入模板
/**
* poi 导出 word
* @author 阿飞
* @version 1.0 ,2018年5月28日上午10:41:01
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class WordBuild {
/**
* word 模板文件导出
* @param tmpFile
* @param contentMap
* @param exportFile
* @throws Exception
*/
private void build(File tmpFile, Map<String, String> contentMap, String exportFile) throws Exception {
FileInputStream tempFileInputStream = new FileInputStream(tmpFile); // 获取文件流
HWPFDocument document = new HWPFDocument(tempFileInputStream); // 创建文档对象
// 读取文本内容
Range bodyRange = document.getRange();
// 替换内容
for (Map.Entry<String, String> entry : contentMap.entrySet()) {
// 替换模板中的字符为新的值
bodyRange.replaceText("${" + entry.getKey() + "}", entry.getValue());
}
// 导出到文件
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
document.write(byteArrayOutputStream);
OutputStream outputStream = new FileOutputStream(exportFile);
outputStream.write(byteArrayOutputStream.toByteArray());
outputStream.close();
}
/**
* word 模板文件保存的具体路径
* @throws Exception
*/
@Test
public void testExportWord() throws Exception {
String tmpFile = "D:/template.doc";
String expFile = "D:/result.doc";
Map<String, String> datas = new HashMap<String, String>();
datas.put("title", "标题部份"); // key 与标题对应
datas.put("content", "这里是内容,测试使用POI导出到Word的内容!");
datas.put("author", "阿飞");
datas.put("url", "www.baidu.com");
build(new File(tmpFile), datas, expFile);
}
/**
* 在项目路径下,ResourceUtils工具类的getFile方法即可读取classpath中的文件
* @throws Exception
*/
// @Test
public void testExportWord2() throws Exception {
String tmpFile = "classpath:template.doc";
String expFile = "D:/result.doc";
Map<String, String> datas = new HashMap<String, String>();
datas.put("title", "标题部份");
datas.put("content", "这里是内容,测试使用POI导出到Word的内容!");
datas.put("author", "阿飞");
datas.put("url", "www.baidu.com");
build(ResourceUtils.getFile(tmpFile), datas, expFile);
}
}
3.excel 表格处理,获取excel数据以及通过excel 模板动态生成 excel
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.ResourceUtils;
import com.afei.excel2word.excel.pojo.WebDto;
import com.afei.excel2word.excel.utils.ExcelUtil;
/**
* excel 处理
*
* @author 阿飞
* @version 1.0 ,2018年5月28日下午2:18:53
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class ReadExcelTest {
/**
* 测试获取excel 内容
*
* @throws Exception
*/
// @Test
public void testRead() throws Exception {
// 读取 excel 文件,获得excel 文档对象
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(ResourceUtils.getFile("classpath:web-info.xls")));
// 获取到第一个表格
HSSFSheet sheet = wb.getSheetAt(0);
// 获取表格的第一行
HSSFRow titleRow = sheet.getRow(0);
// 获取表格第一行的第一列
HSSFCell titleCell = titleRow.getCell(0);
// 获取表格中内容,字符串获取方法 getStringCellValue
// 获取表格中内容,数字获取方法 getNumericCellValue()
String title = titleCell.getStringCellValue();
System.out.println("标题是:" + title);
}
// 读取到列表
@Test
public void testReadList() throws Exception {
List<WebDto> list = new ArrayList<WebDto>();
// 读取 excel 文件,获得excel 文档对象
HSSFWorkbook book = new HSSFWorkbook(new FileInputStream(ResourceUtils.getFile("classpath:web-info.xls")));
// 获取到第一个表格
HSSFSheet sheet = book.getSheetAt(0);
/**
* 获取表格数据 getLastRowNum 方法,获取表单最后一行编号
**/
for (int i = 2; i < sheet.getLastRowNum() + 1; i++) {
// 获取表格的第i行
HSSFRow row = sheet.getRow(i);
// 获取表单第一列数据
String one = row.getCell(0).getStringCellValue(); // 第一列
String two = row.getCell(1).getStringCellValue(); // 第二列
String three = row.getCell(2).getStringCellValue();
String four = row.getCell(3).getStringCellValue();
Integer five = (int) row.getCell(4).getNumericCellValue(); // 第五列,数字类型需要强转
list.add(new WebDto(one, two, three, four, five,"ceshi"));
}
System.out.println("共有 " + list.size() + " 条数据:");
for (WebDto wd : list) {
System.out.println(wd);
}
}
/**
* 测试生成excel
* @throws Exception
*/
// @Test
public void test() throws Exception {
List<WebDto> list = new ArrayList<WebDto>();
list.add(new WebDto("知识林", "http://www.zslin.com", "admin", "111111", 555,"ceshi"));
list.add(new WebDto("权限系统", "http://basic.zslin.com", "admin", "111111", 111,"ceshi"));
list.add(new WebDto("校园网", "http://school.zslin.com", "admin", "222222", 333,"ceshi"));
list.add(new WebDto("校园网", "http://school.zslin.com", "admin", "222222", 333,"ceshi"));
Map<String, String> map = new HashMap<String, String>();
map.put("title", "网站信息表");
map.put("total", list.size() + " 条");
map.put("date", getDate());
map.put("test", "woshishi");
// 需要模板文件,map 封装信息, # 加参数名。模板设置.添加列参照 WebDto
// 模板存放位置
// 生成的文件存放位置
// list 表格数据
// list 集合泛型的类型
ExcelUtil.getInstance().exportObj2ExcelByTemplate(map, "classpath:web-info-template.xls",
new FileOutputStream("D:/out22.xls"), list, WebDto.class, true);
}
private String getDate() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
return sdf.format(new Date());
}
}
下载源码