点击上方“码农突围”,马上关注 这里是码农充电第一站,回复“666”,获取一份专属大礼包 真爱,请设置“星标”或点个“在看”
来源:https://www.cnblogs.com/deng-cc
1、POI是什么
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
HSSF is the POI Project's pure Java implementation of the Excel '97(-2007) file format.
XSSF is the POI Project's pure Java implementation of the Excel 2007 OOXML (.xlsx) file format.
2、POI核心类
2.1 工作簿 Workbook
HSSFWorkbook : 有读取.xls 格式和写入Microsoft Excel文件的方法。它与微软Office97-2003版本兼容
XSSFWorkbook : 有读写Microsoft Excel和OpenOffice的XML文件的格式.xls或.xlsx的方法。它与MS-Office版本2007或更高版本兼容
//直接创建新的
HSSFWorkbook()
//通过输入流创建
HSSFWorkbook(java.io.InputStream s)
//直接创建新的
XSSFWorkbook()
//通过File类创建
XSSFWorkbook(java.io.File file)
//通过输入流创建
XSSFWorkbook(java.io.InputStream is)
2.2 标签页 Sheet
workbook.createSheet();
workbook.createSheet(String sheetName);
2.3 行 Row
sheet.createRow(int rownum);
2.4 单元格 Cell
row.createCell(int column);
row.createCell(int column, int type);
3、创建和读取
import java.io.*;
import org.apache.poi.xssf.usermodel.*;
public class CreateWorkBook
{
public static void main(String[] args)throws Exception
{
Blank workbook
XSSFWorkbook workbook = new XSSFWorkbook();
file system using specific name
FileOutputStream out = new FileOutputStream(
new File("createworkbook.xlsx"));
operation workbook using file out object
workbook.write(out);
out.close();
System.out.println("
written successfully");
}
}
3.2 打开现有的工作簿
import java.io.*;
import org.apache.poi.xssf.usermodel.*;
public class OpenWorkBook
public static void main(String args[])throws Exception
File file = new File("openworkbook.xlsx");
FileInputStream fIP = new FileInputStream(file);
//Get the workbook instance for XLSX file
XSSFWorkbook workbook = new XSSFWorkbook(fIP);
if(file.isFile() && file.exists())
{
System.out.println(
"openworkbook.xlsx file open successfully.");
}
else
{
System.out.println(
"Error to open openworkbook.xlsx file.");
}
}
}
4、方法示例:任意对象List转至为Excel文档(可用注解定义标签名和列名)
"学生标签页") (name =
public class Student {
"姓名") (name =
private String name;
private boolean male;
"身高") (name =
private int height;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isMale() {
return male;
}
public void setMale(boolean male) {
this.male = male;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
public static void main(String[] args) {
List<Student> list = new ArrayList<Student>();
Student student1 = new Student();
student1.setName("小红");
student1.setMale(false);
student1.setHeight(167);
Student student2 = new Student();
student2.setName("小明");
student2.setMale(true);
student2.setHeight(185);
list.add(student1);
list.add(student2);
File file = new File("C:/Users/Dulk/Desktop/1314.xls");
createExcel(list, file);
}
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
public Excel {
//设置名称
public String name() default "";
}
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
/**
Excel的操作工具类
*/
public class ExcelUtil {
private static Logger log = Logger.getLogger(ExcelUtil.class);
/**
获取某个File文件对应的Workbook工作簿对象
*/
public static Workbook gainWorkbook(File file) throws ExcelException {
if (!isExcel(file)) {
throw new ExcelException("文件不是Excel类型");
}
//如果文件不存在则新建
if (!file.exists()) {
try {
OutputStream os = new FileOutputStream(file);
Workbook workbook = isOlderEdition(file) ? new HSSFWorkbook() : new XSSFWorkbook();
workbook.write(os);
log.debug("文件不存在,新建该Excel文件");
os.close();
catch (FileNotFoundException e) {
e.printStackTrace();
catch (IOException e) {
e.printStackTrace();
}
}
try {
InputStream is = new FileInputStream(file);
return isOlderEdition(file) ? new HSSFWorkbook(is) : new XSSFWorkbook(is);
catch (FileNotFoundException e) {
e.printStackTrace();
catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
判断某个Excel文件是否是2003-2007通用旧版
*/
private static boolean isOlderEdition(File file) {
return file.getName().matches(".+\\.(?i)xls");
}
/**
判断文件是否是一个Excel文件
*/
private static boolean isExcel(File file) {
String fileName = file.getName();
String regXls = ".+\\.(?i)xls";
String regXlsx = ".+\\.(?i)xlsx";
return fileName.matches(regXls) || fileName.matches(regXlsx);
}
/**
将某个对象的List转换为Excel工作簿
*/
public static <E> Workbook createExcel(List<E> list, File file) {
String sheetName = "default";
if (list.size() == 0) {
return null;
}
Workbook workbook = null;
try {
Class clazz = list.get(0).getClass();
fields = clazz.getDeclaredFields();
if (clazz.isAnnotationPresent(Excel.class)) {
Excel excel = (Excel) clazz.getAnnotation(Excel.class);
sheetName = excel.name();
}
workbook = gainWorkbook(file);
Sheet sheet = workbook.createSheet(sheetName);
//创建首行
Row line = sheet.createRow(0);
for (int k = 0; k < fields.length; k++) {
Cell cell = line.createCell(k);
String columnName = fields[k].getName();
if (fields[k].isAnnotationPresent(Excel.class)) {
Excel excel = fields[k].getAnnotation(Excel.class);
columnName = excel.name();
}
cell.setCellValue(columnName);
}
//创建数据
for (int i = 1; i <= list.size(); i++) {
Row row = sheet.createRow(i);
for (int j = 1; j <= fields.length; j++) {
Cell cell = row.createCell(j - 1);
String fieldName = fields[j - 1].getName();
String fieldFirstLetterUpper = fieldName.substring(0, 1).toUpperCase();
String prefix = "get";
if ("boolean".equals(fields[j - 1].getType().getName())) {
prefix = "is";
}
String methodName = prefix + fieldFirstLetterUpper + fieldName.substring(1);
Method method = clazz.getMethod(methodName);
- 1))));
}
}
log.debug("List读入完毕");
OutputStream os = new FileOutputStream(file);
workbook.write(os);
os.close();
catch (ExcelException e) {
e.printStackTrace();
catch (InvocationTargetException e) {
e.printStackTrace();
catch (NoSuchMethodException e) {
e.printStackTrace();
catch (IllegalAccessException e) {
e.printStackTrace();
catch (FileNotFoundException e) {
e.printStackTrace();
catch (IOException e) {
e.printStackTrace();
}
return workbook;
}
}
最近有有不少老铁在后台留言说,想进大厂,但是算法不好。最近我整理了一份刷题实录,这份刷题实录,也让我进了心仪的大厂。现在开放分享给大家。希望对大家有所帮助。 任何的算法题,如同写作文一样,都有一些模板可以套用的。比如面试常考的DP(动态规划),难的是一些关键点是否能想清楚。比如你能写出动态转移方程,这题基本上就可以AC了。
整个刷题实录内容,包括 双子针、动态规划、二分查找、贪心算法、深度优先搜索、字符串、递归、字典树、排序、链表等相关专题内容。图文并茂,附有刷题答案源码。 刷题任务的题目,是根据题目的类型来汇总的,总结了八个类别,每个类别下面也总结了5个左右的题型,帮助大家分门别类的突破,所以刷起来相对会更有重点和针对性。如果从头到尾的刷,每周按顺序刷42题,很容易让自己坚持不下来,也会觉得很枯燥。所以在制定计划的时候可以让这个计划变得更“有趣"和针对性,让它看起来更容易实现一点,才会更容易坚持。 目前上述内容已打包成完整电子书,具体获取方式如下:
扫描关注 程序猿进阶 公众号;
在 程序猿进阶 公众号后台回复关键词「9999」获取下载地址。
扫描关注,回复"9999"即可下载 最近热文
• 全球当下最厉害的14位程序员,说没听过简直离谱~ • Netflix创始人:我不要求996,一样市值1万亿 • 字节小组长无意中得知整个部门的薪资,自己28K,手下却有35K,怎么办 • 网易一员工核酸阳性后,全员居家办公,提前下班引大厂员工集体围观 • 再见!IBM中国研究院
在这里,我为大家准备了一份2020年最新最全的《Java面试题及答案V3.0》,这套电子书涵盖了诸多后端技术栈的面试题和答案,相信可以帮助大家在最短的时间内复习Java后端的大多数面试题,从而拿到自己心仪的offer。 截了张图,大家可以仔细查看左边的菜单栏,覆盖的知识面真的很广,而且质量都很不错。 资料获取方法
扫描下方二维码
后台回复关键词:Java核心整理
明天见(。・ω・。)