博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POI之excel导入导出
阅读量:6435 次
发布时间:2019-06-23

本文共 4979 字,大约阅读时间需要 16 分钟。

下载地址http://poi.apache.org/

org.apache.poi
poi
3.12
org.apache.poi
poi-ooxml
3.12

 

package com.zns.bean;public class Person {    private int id;    private String name;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Person(int id, String name) {        super();        this.id = id;        this.name = name;    }    public Person() {        super();        // TODO Auto-generated constructor stub    }    @Override    public String toString() {        return "Person [id=" + id + ", name=" + name + "]";    }}

 

package com.zns.test;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.poifs.filesystem.POIFSFileSystem;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 com.zns.bean.Person;public class Test2 {    public static void main(String[] args) throws Exception {        // func1();        // func2();        // func3();    }    //导出到excel    private static void func1() {        Date currentTime = new Date();        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");        String dateStr = formatter.format(currentTime);        // 导出xls用HSSFWorkbook,导出xlsx用XSSFWorkbook        // String outFileName = dateStr + ".xls";        // Workbook wb = new HSSFWorkbook();        String outFileName = dateStr + ".xlsx";        Workbook wb = new XSSFWorkbook();        Sheet sheet1 = (Sheet) wb.createSheet("sheet001");        Row row = (Row) sheet1.createRow(0);        String[] headers = { "编号", "姓名" };        for (int i = 0; i < headers.length; i++) {            Cell cell = row.createCell(i);            cell.setCellValue(headers[i]);        }        List
list = new ArrayList
(); list.add(new Person(1, "张三")); list.add(new Person(2, "李四")); for (int i = 0; i < list.size(); i++) { row = sheet1.createRow((int) i + 1); Person obj = (Person) list.get(i); row.createCell(0).setCellValue(obj.getId()); row.createCell(1).setCellValue(obj.getName()); } try { FileOutputStream fout = new FileOutputStream("D:/" + outFileName); wb.write(fout); fout.close(); System.out.println("导出成功..."); } catch (Exception e) { e.printStackTrace(); } } //从excel导入 private static void func2() throws FileNotFoundException, IOException { // InputStream is = new FileInputStream("D://test.xls"); // Workbook wb = new HSSFWorkbook(is); InputStream is = new FileInputStream("D://test.xlsx"); Workbook wb = new XSSFWorkbook(is); List
list = new ArrayList
(); Sheet sheet1 = (Sheet) wb.getSheetAt(0); for (int rowNum = 1; rowNum <= sheet1.getLastRowNum(); rowNum++) { Row row = (Row) sheet1.getRow(rowNum); if (row != null) { Person obj = new Person(); Cell cell0 = row.getCell(0); cell0.setCellType(Cell.CELL_TYPE_STRING); Cell cell1 = row.getCell(1); obj.setId(Integer.parseInt(cell0.getStringCellValue())); obj.setName(cell1.getStringCellValue()); list.add(obj); } } System.out.println(list); } //从excel模板获取内容 再导出 private static void func3() throws IOException, FileNotFoundException { // excel模板路径 File fi = new File("D:\\test.xls"); POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fi)); // 读取excel模板 HSSFWorkbook wb = new HSSFWorkbook(fs); // 读取了模板内sheet0 HSSFSheet sheet = wb.getSheetAt(0); // 在相应的单元格进行赋值 HSSFCell cell = sheet.getRow(1).getCell(3); cell.setCellValue("aa"); HSSFCell cell2 = sheet.getRow(3).getCell(3); cell2.setCellValue("bb"); HSSFCell cell3 = sheet.getRow(0).getCell(0); cell3.setCellValue("cc"); // 修改模板内容导出新文件 FileOutputStream out = new FileOutputStream("D:/export.xls"); wb.write(out); out.close(); }}

 

转载于:https://www.cnblogs.com/zengnansheng/p/10389255.html

你可能感兴趣的文章
客户故事:4家银行如何打造新一代移动金融中心
查看>>
【新书推荐】“十三五”国家重点出版规划项目《网络安全技术及应用》第3版出版发行...
查看>>
《神探tcpdump终结招》-linux命令五分钟系列之四十三
查看>>
博客即日起不再更新,已转移至https://dacat.cc
查看>>
Tomcat的Server.xml虚拟主机和虚拟目录的配置
查看>>
utf8编码
查看>>
sublime插件与使用技巧
查看>>
微信公众平台开发
查看>>
mysql线程状态
查看>>
6.Dockerfile
查看>>
《猜猜看》游戏开发
查看>>
安卓倒计时实例
查看>>
spring+springmvc+mybatis+logback 框架搭建
查看>>
Android 判断手机是否连接网络
查看>>
小米手机最新线刷包 fastboot包,和最新卡刷包!MIUI V4
查看>>
解决资源抢夺问题(买车票)
查看>>
4.6 元组
查看>>
“十年磨一剑”酷客多智慧连锁版小程序重磅来袭
查看>>
同步与异步到底是什么???
查看>>
django更改数据
查看>>