您的位置:首页 > 其它

智能销售系统day7

2019-03-29 19:12 85 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/Aydxz/article/details/88900003

导入与导出

1.java操作Excel

技术:

  1. jxl:只能对Excel进行操作,属于比较老的框架
  2. POI:是apache的项目,可对ms的word,Excel,PPT进行操作

POI的使用:
poi针对offices03(.xls)与07(.xlsx)是单独写了相应的实现api
poi支持的jar包:

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.11</version></dependency><dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.11</version></dependency>

创建Excel:

//在内存中创建一个工作薄
SXSSFWorkbook wb = new SXSSFWorkbook();
//在工作薄上创建一张表格
Sheet sheet = wb.createSheet("99乘法表");
//在表格中创建行(createRow)
for(int i=1;i<=9;i++){
Row row = sheet.createRow(i-1);
//在表格中创建列(格子createCell)
for(int j=1;j<=i;j++){
Cell cell = row.createCell(j-1);
//在格子中写东西(setCellValue)
cell.setCellValue(i+"*"+j+"="+(i*j));
}
}
//使用输出流将内容输出出去,以Excel格式
FileOutputStream out = new FileOutputStream("99.xlsx");
wb.write(out);
out.close();

读取Excel:

//读取Excel文件
InputStream inp = new FileInputStream("emp-poi.xlsx");
//创建工作簿(内存中)
Workbook wb = WorkbookFactory.create(inp);
//获取对应的表(getSheetAt)
Sheet sheet = wb.getSheetAt(0);
//获取表的总行数(getLastRowNum)
int lastRowNum = sheet.getLastRowNum();
for (int i = 1; i <= lastRowNum; i++) {
//拿到每一行(getRow)
Row row = sheet.getRow(i);
//拿到每一行的总列数(getLastCellNum)
short lastCellNum = row.getLastCellNum();
for (int j = 0; j < lastCellNum; j++) {
//拿到这一个格子与它的数据
Cell cell = row.getCell(j);
System.out.print(cell.getStringCellValue()+" ");
}
System.out.println();
}

2.easypoi

概述:相对于jxl,poi更加的简单,功能没有前者的强大,但是够用了

easypoi的支持包:(注意:把之前的POI引入去掉,有冲突)

<!-- easypoi的支持 --><dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.2.0</version></dependency><dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.2.0</version></dependency><dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.2.0</version></dependency>

Easypoi的使用:
在domain上打注解
@Excel(name = “名称”) 告诉easypoi它的名称
@Excel(name=“邮件”,width = 25) 可以设置格子的长度宽度
@Excel(name=“性别”,replace = {“男_true”,“女_false”}) boolean类型时使下划线来告诉easypoi,true和false时显示什么
@Excel(name=“出生日期”,format = “yyyy-MM-dd”) 设置日期格式
@Excel(name = “头像”,type = 2,height = 25) type=2就是告诉easypoi这是一张图片
@ExcelTarget(“emp”) 加在类上,可以根据不同的类打开显示的数据不一样

创建Excel:
//new ExportParams(title,sheetName):导出的属性设置
// title:表头名称
//sheetName:表的名称
//PoiEmployee .class:导出的实体类型
//list:导出的数据
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(“部门名称”,“bbb”),
PoiEmployee.class, list);
//使用输出流将内容输出出去,以Excel格式
FileOutputStream fos = new FileOutputStream(“poidept.xlsx”);
workbook.write(fos);
fos.close();

读取Excel:
//创建导入对象
ImportParams params = new ImportParams();
//设置表头的行数
params.setHeadRows(1);
//设置标题的行数
params.setTitleRows(1);
//指定Excel文件,导入的实体类型,传入导入对象
List list = ExcelImportUtil.importExcel(new File(“poiemp.xlsx”),
PoiEmployee.class, params);
list.forEach(e -> {
System.out.println(e+","+e.getDepartment());
});

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: