excel
一.poi
1. 引入依赖
<!-- 03版-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<!-- excel 07版-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<!-- 日期格式化工具-->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.6</version>
</dependency>
<!-- test-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
2.excel写入
package com.hw;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;
import org.junit.Test;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
public class ExcelWriteTest {
String path = "E:\\IO\\";
@Test
public void excel07TestBigData(){
long begin = System.currentTimeMillis();
Workbook workbook = new SXSSFWorkbook();
Sheet sheet = workbook.createSheet("黄巍的测试表");
for (int rowNum = 0; rowNum < 100000; rowNum++) {
Row row = sheet.createRow(rowNum);
for (int cellNum = 0; cellNum < 10; cellNum++) {
Cell cell = row.createCell(cellNum);
cell.setCellValue(cellNum);
}
}
System.out.println("over");
BufferedOutputStream bufferedOutputStream = null;
try {
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File(path + "myexcel07BigData.xlsx")));
workbook.write(bufferedOutputStream);
((SXSSFWorkbook)workbook).dispose();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != bufferedOutputStream){
try {
bufferedOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Long end = System.currentTimeMillis();
System.out.println((double)(end - begin) / 1000);
}
@Test
public void excel07Test(){
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("黄巍的测试表");
Row row = sheet.createRow(0);
Cell cell11 = row.createCell(0);
cell11.setCellValue("姓名");
Cell cell12 = row.createCell(1);
cell12.setCellValue("age");
Cell cell13 = row.createCell(2);
cell13.setCellValue("date");
Row row1 = sheet.createRow(1);
Cell cell21 = row1.createCell(0);
cell21.setCellValue("黄巍");
Cell cell22 = row1.createCell(1);
cell22.setCellValue("28");
Cell cell23 = row1.createCell(2);
String time = new DateTime().toString("yyyy_MM_dd HH_mm_ss");
cell23.setCellValue(time);
BufferedOutputStream bufferedOutputStream = null;
try {
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File(path + "myexcel07.xlsx")));
workbook.write(bufferedOutputStream);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != bufferedOutputStream){
try {
bufferedOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void excel03Test(){
Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("黄巍的测试表");
Row row = sheet.createRow(0);
Cell cell11 = row.createCell(0);
cell11.setCellValue("姓名");
Cell cell12 = row.createCell(1);
cell12.setCellValue("age");
Cell cell13 = row.createCell(2);
cell13.setCellValue("date");
Row row1 = sheet.createRow(1);
Cell cell21 = row1.createCell(0);
cell21.setCellValue("黄巍");
Cell cell22 = row1.createCell(1);
cell22.setCellValue("28");
Cell cell23 = row1.createCell(2);
String time = new DateTime().toString("yyyy_MM_dd HH_mm_ss");
cell23.setCellValue(time);
BufferedOutputStream bufferedOutputStream = null;
try {
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File(path + "myexcel.xls")));
workbook.write(bufferedOutputStream);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != bufferedOutputStream){
try {
bufferedOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
3.excel读取
package com.hw;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;
import org.junit.Test;
import java.io.*;
import java.text.SimpleDateFormat;
public class ExcelReadTest {
String path = "E:\\IO\\";
protected String getValue(Cell cell) {
Object obj = "";
if (cell != null) {
switch (cell.getCellTypeEnum()) {
case BOOLEAN:
obj = cell.getBooleanCellValue();
break;
case ERROR:
obj = cell.getErrorCellValue();
break;
case NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
obj = sdf.format(cell.getDateCellValue());
} else {
cell.setCellType(CellType.STRING);
obj = cell.getStringCellValue();
}
break;
case STRING:
obj = cell.getStringCellValue();
break;
case FORMULA:
try {
obj = String.valueOf(cell.getStringCellValue());
} catch (IllegalStateException e) {
obj = String.valueOf(cell.getNumericCellValue());
}
break;
}
}
return obj.toString().trim().replace(',', ',')
.replace('(', '(').replace(")", ")");
}
@Test
public void excel07ReadTest() throws IOException {
BufferedInputStream bis = null;
bis = new BufferedInputStream(new FileInputStream(new File(path + "myexcel07.xlsx")));
Workbook workbook = new XSSFWorkbook(bis);
Sheet sheet = workbook.getSheetAt(0);
int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();
for (int rowNum = 0; rowNum < physicalNumberOfRows; rowNum++) {
Row row = sheet.getRow(rowNum);
int physicalNumberOfCells = row.getPhysicalNumberOfCells();
for (int cellNum = 0; cellNum < physicalNumberOfCells; cellNum++) {
Cell cell = row.getCell(cellNum);
System.out.println(getValue(cell));
}
}
if (null != bis){
bis.close();
}
}
@Test
public void excel03ReadTest() throws IOException {
BufferedInputStream bis = null;
bis = new BufferedInputStream(new FileInputStream(new File(path + "myexcel03.xls")));
Workbook workbook = new HSSFWorkbook(bis);
Sheet sheet = workbook.getSheetAt(0);
int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();
for (int rowNum = 0; rowNum < physicalNumberOfRows; rowNum++) {
Row row = sheet.getRow(rowNum);
int physicalNumberOfCells = row.getPhysicalNumberOfCells();
for (int cellNum = 0; cellNum < physicalNumberOfCells; cellNum++) {
Cell cell = row.getCell(cellNum);
System.out.println(getValue(cell));
}
}
if (null != bis){
bis.close();
}
}
}
二.Easyexcel
1.依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.0.1</version>
</dependency>
参考
https://alibaba-easyexcel.github.io/index.html