在Excel的导入导出,Excel导入导出常用的工具就是POI和easyExcel。
基本功能
HSSF-提供读写MicrosoftExcel格式档案的功能。
XSSF-提供读写MicrosoftExcel OOXML格式档案的功能。
HWPF-提供读写MicrosoftWord格式档案的功能。
HSLF-提供读写Microsof PowerPoint格式档案的功能。
HDGF-提供读写MicrosoftVisio格式档案的功能。
2.poi中的对象介绍- 工作簿
使用WorkBook对象进行操作,实现类主要有HSSFWorkBook、XSSFWorkBook、SXSSFWorkBook
- 工作表
使用Sheet对象操作工作表,一个excel中有多个sheet
- 行
使用Row对象操作工作表中的一行
- 单元格
使用Cell对象操作一行中的某一列
3.用到的jar包代码段
导入代码
package com.zking.main;
import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.zking.dao.StudentsDao;
import com.zking.entity.Students;
public class Import {
public static void main(String[] args) throws Exception {
//拿到对应exel文件
File file = new File("E://import//学生.xls");
//转换成文件流
FileInputStream fis = new FileInputStream(file);
//根据路径获取这个操作excel的实例
HSSFWorkbook wb = new HSSFWorkbook(fis);
//根据页面index 获取sheet页
HSSFSheet sheet = wb.getSheetAt(0);
//实体类集合
//List stus = new ArrayList<>();
//声明行数据,对应exel的行
HSSFRow row = null;
StudentsDao sd = new StudentsDao();
//循环sesheet页中数据从第二行开始,第一行是标题
for(int i = 1;i
导出代码
package com.zking.main;
import java.io.FileOutputStream;
import java.util.ArrayList;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import com.zking.dao.StudentsDao;
import com.zking.entity.Students;
public class Export {
public static void main(String[] args) {
StudentsDao std = new StudentsDao();
ArrayList slist = std.getall();
String[] title = {"编号","姓名","性别","年龄","爱好","地址","简介"};
SXSSFWorkbook wb = new SXSSFWorkbook(10);
Sheet sheet = wb.createSheet();
Row row = sheet.createRow(0);
//给单元格设置样式
CellStyle cellStyle = wb.createCellStyle();
Font font = wb.createFont();
//设置字体大小
font.setFontHeightInPoints((short) 12);
//设置字体加粗
font.setBold(true);
//给字体设置样式
cellStyle.setFont(font);
//设置单元格背景颜色
cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
//设置单元格填充样式(使用纯色背景颜色填充)
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//填充表头数据
for(int i = 0;i
.xls 读



