数据导出excel简单案例
需要的依赖坐标
<!--POI-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.0.1</version>
</dependency>
Java代码演示 ```java package com.xiaoha.excel.excel_eximport;
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.*;
/**
- 导出excel文件 *
- @author HausenLee
@date 2021/04/09 */ public class ExceExport { public static void main(String[] args) {
//1.获取到对应的Excel文件,工作簿文件
Workbook wb = new XSSFWorkbook();
//2.创建工作表
Sheet sheet = wb.createSheet();
wb.createSheet("这是啥呀");
//3.创建工作表中的行对象
Row row = sheet.createRow(1);
//4.创建工作表中行中的列对象
Cell cell = row.createCell(1);
//5.在列中写数据(设置数据)
cell.setCellValue("测试一下单元格");
//创建一个文件对象,作为excel文件内容的输出文件
File f = new File("test.xlsx");//文件在当前项目下
//输出时通过流的形式对外输出,包装对应的目标文件
OutputStream os = null;
try {
os = new FileOutputStream(f);
wb.write(os);
} catch (Exception e) {
e.printStackTrace();
} finally {
//将内存中的workbook数据写入到流中
try {
wb.close();
if(os!=null){
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} } ```
excel导入数据简单案例
- Java代码演示 ```java package com.xiaoha.excel.excelImport;
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.IOException;
/**
- 导入excel文件 *
- @author HausenLee
@date 2021/04/09 */ public class ExcelImport { public static void main(String[] args) {
Workbook wb = null;
try {
//1.获取要读取的文件工作簿对象
wb = new XSSFWorkbook("test.xlsx");
//2.获取工作表
Sheet s = wb.getSheetAt(0);
//3.获取行
Row row = s.getRow(1);
//4.获取列
Cell cell = row.getCell(1);
//5.根据数据的类型获取数据
String data = cell.getStringCellValue();
//double data = cell.getNumericCellValue(); 获取数值类型
//boolean data = cell.getBooleanCellValue(); 获取布尔类型的
System.out.println(data);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(wb!=null){
wb.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} } ```
数据导出excel复杂案例
- Java代码演示 ```java package com.xiaoha.excel.export_header;
import com.xiaoha.excel.pojo.Question; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.junit.Test;
import java.io.*; import java.util.ArrayList; import java.util.List;
/**
- 导出表头 *
- @author HausenLee
@date 2021/04/09 */ public class ExportHeader { public static void main(String[] args) {
//1.获取到对应的Excel文件,工作簿文件
Workbook wb = new XSSFWorkbook();
//2.创建工作表
Sheet s = wb.createSheet("题目数据文件");
//设置通用配置
//s.setColumnWidth(4,100);
//制作标题 addMergedRegion方法是合并单元格的方法
//参数为指定行列的对象CellRangeAddress
//它的构造方法参数为第一行,最后一行,第一列,最后一列
s.addMergedRegion(new CellRangeAddress(1,1,1,12));
//写入标题
Row row_1 = s.createRow(1);
Cell cell_1_1 = row_1.createCell(1);
cell_1_1.setCellValue("在线试题导出信息");
//创建一个样式
CellStyle cs_title = wb.createCellStyle();
//水平对齐
cs_title.setAlignment(HorizontalAlignment.CENTER);
//垂直对齐
cs_title.setVerticalAlignment(VerticalAlignment.CENTER);
cell_1_1.setCellStyle(cs_title);
//制作表头
String[] fields = {"题目ID","所属公司ID","所属目录ID","题目简介","题干描述","题干配图","题目分析","题目类型","题目难度","是否经典题","题目状态","审核状态"};
Row row_2 = s.createRow(2);
for (int i = 0; i < fields.length; i++) {
Cell cell_2_temp = row_2.createCell(1 + i);
//++
cell_2_temp.setCellValue(fields[i]);
//++
CellStyle cs_field = wb.createCellStyle();
cs_field.setAlignment(HorizontalAlignment.CENTER);
cell_2_temp.setCellStyle(cs_field);
}
//制作数据区
// 创建一个文件对象,作为excel文件内容的输出文件
File f = new File("test.xlsx");
//输出时通过流的形式对外输出,包装对应的目标文件
OutputStream os = null;
try {
os = new FileOutputStream(f);
//将内存中的workbook数据写入到流中
wb.write(os);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
wb.close();
if(os!=null){
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void testProjectPoi() throws IOException {
//1.获取到对应的Excel文件,工作簿文件
Workbook wb = new XSSFWorkbook();
//2.创建工作表
Sheet s = wb.createSheet("题目数据文件");
//制作数据区
List<Question> questionList = new ArrayList<>();
Question qq = new Question();
qq.setId("1");
qq.setPicture("12");
qq.setReviewStatus("13");
qq.setAnalysis("14");
qq.setCatalogId("15");
qq.setCompanyId("16");
qq.setDifficulty("17");
qq.setIsClassic("18");
qq.setRemark("19");
qq.setState("21");
qq.setSubject("31");
qq.setType("41");
questionList.add(qq);
Question qqq = new Question();
qqq.setId("1");
qqq.setPicture("12");
qqq.setReviewStatus("13");
qqq.setAnalysis("14");
qqq.setCatalogId("15");
qqq.setCompanyId("16");
qqq.setDifficulty("17");
qqq.setIsClassic("18");
qqq.setRemark("19");
qqq.setState("21");
qqq.setSubject("31");
qqq.setType("41");
questionList.add(qqq);
//遍历集合
/*for (int i = 0; i < questionList.size(); i++) {
Row row = s.createRow(i + 3);
for (int j = 0; j < 12; j++) {
Cell cell = row.createCell(j + 1);
}
}*/
int rowIndex= 0;
for (Question question : questionList) {
int cellIndex = 0;
Row row = s.createRow( 3+rowIndex++);
Cell cell_1 = row.createCell(1+cellIndex++);
cell_1.setCellValue(question.getId());
Cell cell_2 = row.createCell(2+cellIndex++);
cell_2.setCellValue(question.getPicture());
Cell cell_3 = row.createCell(3+cellIndex++);
cell_3.setCellValue(question.getReviewStatus());
Cell cell_4 = row.createCell(4+cellIndex++);
cell_4.setCellValue(question.getAnalysis());
Cell cell_5 = row.createCell(5+cellIndex++);
cell_5.setCellValue(question.getCatalogId());
Cell cell_6 = row.createCell(6+cellIndex++);
cell_6.setCellValue(question.getCompanyId());
Cell cell_7 = row.createCell(7+cellIndex++);
cell_7.setCellValue(question.getDifficulty());
Cell cell_8 = row.createCell(8+cellIndex++);
cell_8.setCellValue(question.getIsClassic());
Cell cell_9 = row.createCell(9+cellIndex++);
cell_9.setCellValue(question.getRemark());
Cell cell_10 = row.createCell(10+cellIndex++);
cell_10.setCellValue(question.getState());
Cell cell_11 = row.createCell(11+cellIndex++);
cell_11.setCellValue(question.getSubject());
Cell cell_12 = row.createCell(12+cellIndex++);
cell_12.setCellValue(question.getType());
}
//创建一个文件对象,作为excel文件内容的输出文件
File f = new File("test.xlsx");//文件在当前项目下
//输出时通过流的形式对外输出,包装对应的目标文件
OutputStream os = null;
try {
os = new FileOutputStream(f);
wb.write(os);
} catch (Exception e) {
e.printStackTrace();
} finally {
//将内存中的workbook数据写入到流中
try {
wb.close();
if(os!=null){
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
} ```