栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

poi技术实现数据导入导出

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poi技术实现数据导入导出

1.poi介绍

在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 读

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/936770.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号