import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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.util.CellRangeAddress;
import org.junit.Test; public class POIDemo1 {
// 导入
// @Test
public void test2() throws Exception {
// 创建HSSFWorkbook对象(excel的文档对象)
HSSFWorkbook wb = new HSSFWorkbook();
// 建立新的sheet对象(excel的表单)
HSSFSheet sheet = wb.createSheet("成绩表"); sheet.setDefaultRowHeightInPoints(10);// 设置缺省列高
sheet.setDefaultColumnWidth(20);// 设置缺省列宽 // 在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个
HSSFRow row1 = sheet.createRow(0);
// 创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个
HSSFCell cell = row1.createCell(0);
// 设置单元格内容
cell.setCellValue("学员考试成绩一览表");
// 合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 3));
// 在sheet里创建第二行
HSSFRow row2 = sheet.createRow(1);
// 创建单元格并设置单元格内容
row2.createCell(0).setCellValue("姓名");
row2.createCell(1).setCellValue("班级");
row2.createCell(2).setCellValue("笔试成绩");
row2.createCell(3).setCellValue("机试成绩");
// 在sheet里创建第三行
HSSFRow row3 = sheet.createRow(2);
row3.createCell(0).setCellValue("李明");
row3.createCell(1).setCellValue("As178");
row3.createCell(2).setCellValue(87);
row3.createCell(3).setCellValue(78);
// .....省略部分代码 // 输出Excel文件
// 输出Excel文件
FileOutputStream output = new FileOutputStream("d:\\workbook.xls");
// OutputStream output=response.getOutputStream();
// response.reset();
// response.setHeader("Content-disposition",
// "attachment; filename=details.xls");
// response.setContentType("application/msexcel");
wb.write(output);
output.flush();
output.close();
} // 导出
@Test
public void loadScoreInfo() throws IOException {
List temp = new ArrayList(); FileInputStream fileIn = new FileInputStream("d:\\workbook.xls");
// 根据指定的文件输入流导入Excel从而产生Workbook对象
HSSFWorkbook wb0 = new HSSFWorkbook(fileIn);
// 获取Excel文档中的第一个表单
Sheet sht0 = wb0.getSheetAt(0);
// 对Sheet中的每一行进行迭代
for (Row r : sht0) {
// 如果当前行的行号(从0开始)未达到2(第三行)则从新循环
if (r.getRowNum() < 2) {
continue;
}
// String result1 = getResult(r.getCell(0));
// String result2 = getResult(r.getCell(1));
// String result3 = getResult(r.getCell(2));
// String result4 = getResult(r.getCell(3));
// System.out.println("="+result1);
// System.out.println("="+result2);
// System.out.println("="+result3);
// System.out.println("="+result4); // 创建实体类
ScoreInfo info = new ScoreInfo();
// 取出当前行第1个单元格数据,并封装在info实体stuName属性上
info.setStuName(r.getCell(0).getStringCellValue());
info.setClassName(r.getCell(1).getStringCellValue());
info.setWrittenScores(r.getCell(2).getNumericCellValue());
info.setMachineScores(r.getCell(3).getNumericCellValue());
temp.add(info);
}
System.out.println(temp);
fileIn.close();
} public static String getResult(Cell cell) {
if (cell.getCellType() == cell.CELL_TYPE_BOOLEAN) {
return String.valueOf(cell.getBooleanCellValue());
} else if (cell.getCellType() == cell.CELL_TYPE_NUMERIC) {
return String.valueOf(cell.getNumericCellValue());
} else if (cell.getCellType() == cell.CELL_TYPE_STRING) {
return String.valueOf(cell.getStringCellValue());
} else {
return String.valueOf(cell.getStringCellValue());
}
} }

使用Poi对EXCLE的导入导出的更多相关文章

  1. poi实现excel的导入导出功能

    Java使用poi实现excel的导入导出功能: 工具类ExcelUtil,用于解析和初始化excel的数据:代码如下 package com.raycloud.kmmp.item.service.u ...

  2. 一个基于POI的通用excel导入导出工具类的简单实现及使用方法

    前言: 最近PM来了一个需求,简单来说就是在录入数据时一条一条插入到系统显得非常麻烦,让我实现一个直接通过excel导入的方法一次性录入所有数据.网上关于excel导入导出的例子很多,但大多相互借鉴. ...

  3. SSM中使用POI实现excel的导入导出

    环境:导入POI对应的包 环境: Spring+SpringMVC+Mybatis POI对应的包 <dependency> <groupId>org.apache.poi&l ...

  4. Java集成POI进行Excele的导入导出,以及报错: java.lang.AbstractMethodError..........

    报错信息如下 java.lang.AbstractMethodError: org.apache.poi.xssf.usermodel.XSSFCell.setCellType(Lorg/apache ...

  5. 用poi框架进行批量导入导出实例

    Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能.我们这里使用poi对数据库中的数据进行批量导出,以及 ...

  6. POI实现excel的导入导出

    引入依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</arti ...

  7. poi管道流的导入导出

    /** * 导入学生信息 * * @param classid * @param uploadFilePath * @return */ public boolean uploadStudentFil ...

  8. 使用JXL对EXCLE的导入导出

    import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.Da ...

  9. Springboot Excle导入导出

    Springboot Excle导入导出 导入操作:Excle批量导入 导出操作:下载模版 开发笔记 pom.xml <!-- Excle相关jar --> <dependency& ...

随机推荐

  1. Ubuntu新装系统要装软件

    1. 在虚拟机中新安装系统的时候,通常因为时间过了很长,软件有更新之后,安装vim的时候会出错,因此,装完系统先要做的: cd /var/lib/dpkg/updates/ ls sudo apt-g ...

  2. 【SaltStack】一些常用模块举例

    一.用户和用户组模块 说明:该sls文件用来在Minion端创建nginx用户和nginx用户组,当创建nginx用户时,对nginx用户组是否已存在做判断! (1)  /srv/salt/creat ...

  3. VBS脚本获取安全标识符SID(Security Identifiers)的方法

    一.SID简介       SID也就是安全标识符(Security Identifiers),是标识用户.组和计算机帐户的唯一的号码.在第一次创建该帐户时,将给网络上的每一个帐户发布一个唯一的 SI ...

  4. Etree方式解析xml知识积累

    movies.xml: <collection shelf="New Arrivals"> <movie title="Enemy Behind&quo ...

  5. STM32F407 SPI 个人笔记

    概述 SPI ,Serial Peripheral interface,串行外围设备接口 全双工,同步的通信总线,四根线 主要应用在 EEPROM,FLASH,实时时钟,AD转换器,还有数字信号处理器 ...

  6. 【leetcode 字符串】466. Count The Repetitions

    https://leetcode.com/problems/count-the-repetitions/description/ 找循环节 https://www.cnblogs.com/grandy ...

  7. php 学习随笔

    ---恢复内容开始--- round进行格式化数值(进位规则遵守“四舍六入五双”,即前一位是奇数,则进一,前一位是偶数则舍入,因此,rount(1.5)=2,round(2.5)=2,round(0. ...

  8. log4j详细配置解析

    出自:http://www.blogjava.net/zJun/archive/2006/06/28/55511.html Log4J的配置文件(Configuration File)就是用来设置记录 ...

  9. 【SPOJ687&POJ3693】Maximum repetition substring(后缀数组)

    题意: n<=1e5 思路: From http://hzwer.com/6152.html 往后匹配多远 r 用ST表求lcp即可...往前 l 就把串反过来再做一下.. 但是有可能求出来的最 ...

  10. laravel 数据库配置

    数据库配置文件为项目根目录下的config/database.php //默认数据库为mysql 'default' => env('DB_CONNECTION', 'mysql'), 'mys ...