写在最后
还有一份JAVA核心知识点整理(PDF):JVM,JAVA集合,JAVA多线程并发,JAVA基础,Spring原理,微服务,Netty与RPC,网络,日志,Zookeeper,Kafka,RabbitMQ,Hbase,MongoDB,Cassandra,设计模式,负载均衡,数据库,一致性哈希,JAVA算法,数据结构,加密算法,分布式缓存,Hadoop,Spark,Storm,YARN,机器学习,云计算…
//通过输入流创建HSSFWorkbook(java.io.InputStream s)
XSSFWorkbook
//直接创建新的XSSFWorkbook()
//通过File类创建XSSFWorkbook(java.io.File file)
//通过输入流创建XSSFWorkbook(java.io.InputStream is)
2.2 标签页 Sheet
HSSFSheet 和 XSSFSheet 都是Sheet接口的实现类,Sheet可以使用Workbook的两个方法获得:
workbook.createSheet();workbook.createSheet(String sheetName);
2.3 行 Row
同理,Row是 HSSFRow 和 XSSFRow 的接口,通过Sheet获取:
sheet.createRow(int rownum);
2.4 单元格 Cell
同理,Cell是 HSSFCell 和 XSSFCell 的接口,通过Row获取:
row.createCell(int column);row.createCell(int column, int type);
3、创建和读取
其实如果能理解面向对象,就很简单了,另外包括字体,公式,超链接等,都有对应的封装类,此处只提出了核心的几个,需要了解更多的需要自行展开。
例子的话,直接从别人教程里摘出来吧,另,读取的workbook,可以debug瞅瞅内容。
3.1 创建空白工作簿
import java.io.;import org.apache.poi.xssf.usermodel.;public class CreateWorkBook{ public static void main(String[] args)throws Exception { //Create Blank workbook XSSFWorkbook workbook = new XSSFWorkbook(); //Create file system using specific name FileOutputStream out = new FileOutputStream( new File(“createworkbook.xlsx”)); //write operation workbook using file out object workbook.write(out); out.close(); System.out.println(" createworkbook.xlsx 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文档(可用注解定义标签名和列名)
写了个方法,可以将某个类的List转换为对应的Excel文档,列名如果在不使用注解的情况下默认为属性名:
类:
@Excel(name = “学生标签页”)public class Student {
@Excel(name = “姓名”) private String name;
private boolean male;
@Excel(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 list = new ArrayList(); 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;
@Retention(RetentionPolicy.RUNTIME)public @interface 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”); }
最后
手绘了下图所示的kafka知识大纲流程图(xmind文件不能上传,导出图片展现),但都可提供源文件给每位爱学习的朋友
大纲流程图(xmind文件不能上传,导出图片展现),但都可提供源文件给每位爱学习的朋友
[外链图片转存中…(img-wrn5ionW-1714966320937)]