POI
<!-- POI Excel -->
<!-- xls(03) 版 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<!-- xlsx(07) 版 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<!-- 日期时间转化 -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.5</version>
</dependency>
看注释
使用0307版不一样用的接口也不一样!!!
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
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.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Date;
/**
* @author: 云
* @date: 2020/10/22 14:48
* @version: 1.0
*/
public class ExcelOneWrite {
public static void main(String[] args) throws Exception {
// 写入
// x1();
//读取
x1();
}
//预留创建位置
static String PATH = "C:\\Users\\zhangyunhao\\Desktop\\";
/**
* 两个版本的写入
* xls 快 大小有限制
* xlsx 慢 大小没限制
* sxlsx xlsx加速升级版
*/
public static void x1() throws Exception {
//1.创建一个工作薄
/**
* 03 版xls 结尾 new HSSFWorkbook();
* 07 版xlsx 结尾 new XSSFWorkbook();
* 用那个 后面的打印 类型一定要对上
*
* 07 版xlsx 升级版 new SXSSFWorkbook();
*/
Workbook workbook = new XSSFWorkbook();
//2.创建一个工作表
Sheet sheet = workbook.createSheet("第一张表");
//3.创建一个 行
Row row1 = sheet.createRow(0);
//4.创建一个单元格
Cell cell1 = row1.createCell(0); //这里单元格的位置相当于 0,0
cell1.setCellValue("今日新增");
Cell cell2 = row1.createCell(1);//这里单元格的位置相当于 0,1
cell2.setCellValue("日期");
//第二行
Row row2 = sheet.createRow(1);
//4.创建一个单元格
Cell cell3 = row2.createCell(0); //这里单元格的位置相当于 1,0
cell3.setCellValue("10");
Cell cell4 = row2.createCell(1);//这里单元格的位置相当于 1,1
String time = new DateTime().toString("yyyy-MM-dd HH:mm");
cell4.setCellValue(time);
//上面写完 开始创建文件
// IO 流 03版的结尾是xls 07 版xlsx 结尾
FileOutputStream fileOutputStream = new FileOutputStream(PATH + "1.xlsx");
//写入
workbook.write(fileOutputStream);
//关闭 流
fileOutputStream.close();
/**
* 如果用的是 SXSSFWorkbook
* 在添加东西的时候会建一个缓存文件,等创建完毕 删除
* 清除临时文件
*/
//((SXSSFWorkbook)workbook).dispose();
System.out.println("excel文件生成成功!!");
}
//------------------- 写入结束 ------------------------
//------------------- 读取开始 ------------------------
public static void x2() throws Exception {
InputStream is = new FileInputStream(PATH + "1.xlsx");
Workbook workbook = new XSSFWorkbook(is);
Sheet sheet = workbook.getSheetAt(0);
// 读取标题所有内容
Row rowTitle = sheet.getRow(0);
if (rowTitle != null) {// 行不为空
// 读取cell
int cellCount = rowTitle.getPhysicalNumberOfCells();
for (int cellNum = 0; cellNum < cellCount; cellNum++) {
Cell cell = rowTitle.getCell(cellNum);
if (cell != null) {
int cellType = cell.getCellType();
String cellValue = cell.getStringCellValue();
System.out.print(cellValue + "|");
}
}
System.out.println();
}
// 读取商品列表数据
int rowCount = sheet.getPhysicalNumberOfRows();
for (int rowNum = 1; rowNum < rowCount; rowNum++) {
Row rowData = sheet.getRow(rowNum);
if (rowData != null) {// 行不为空
// 读取cell
int cellCount = rowTitle.getPhysicalNumberOfCells();
for (int cellNum = 0; cellNum < cellCount; cellNum++) {
System.out.print("【" + (rowNum + 1) + "-" + (cellNum + 1) + "】");
Cell cell = rowData.getCell(cellNum);
if (cell != null) {
int cellType = cell.getCellType();
//判断单元格数据类型
String cellValue = "";
switch (cellType) {
case HSSFCell.CELL_TYPE_STRING://字符串
System.out.print("【STRING】");
cellValue = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BOOLEAN://布尔
System.out.print("【BOOLEAN】");
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_BLANK://空
System.out.print("【BLANK】");
break;
case HSSFCell.CELL_TYPE_NUMERIC:
System.out.print("【NUMERIC】");
//cellValue = String.valueOf(cell.getNumericCellValue());
if (HSSFDateUtil.isCellDateFormatted(cell)) {//日期
System.out.print("【日期】");
Date date = cell.getDateCellValue();
cellValue = new DateTime(date).toString("yyyy-MM-dd");
} else {
// 不是日期格式,则防止当数字过长时以科学计数法显示
System.out.print("【转换成字符串】");
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cellValue = cell.toString();
}
break;
case Cell.CELL_TYPE_ERROR:
System.out.print("【数据类型错误】");
break;
}
System.out.println(cellValue);
}
}
}
}
is.close();
}
}
EasyExcel
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
需要先创建一个对象
import lombok.Data;
import java.util.Date;
@Data
public class DemoData {
private String string;
private Date date;
private Double doubleData;
}
简单读写
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class EasyExcel {
String path = "C:\\Users\\zhangyunhao\\Desktop\\";
private List<DemoData> data() {
List<DemoData> list = new ArrayList<DemoData>();
for (int i = 0; i < 10; i++) {
DemoData data = new DemoData();
data.setString("字符串" + i);
data.setDate(new Date());
data.setDoubleData(0.56);
list.add(data);
}
return list;
}
// 最简单的写
public void simpleWrite() {
// 写法1
String fileName = path+"EasyExcel.xlsx";
// 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
// 如果这里想使用03 则 传入excelType参数即可
EasyExcel.write(fileName, DemoData.class).sheet("模板").doWrite(data());
}
// 最简单的读
public void simpleRead() {
String fileName = path+"EasyExcel.xlsx";
// 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭
EasyExcel.read(fileName, DemoData.class, new DemoDataListener()).sheet().doRead();
}
}