POI与Excel文件+集合
1.大纲
- 表示文件
- 建立考生库
- POI
- 集合类
- 读取**Excel**文件
- 写入**Excel**文件
考生库
成绩库实验15 学习SQL 进行简单的语句,方法调用,进行操作、解析格式(相当于大型的插件!)
使用 Microsoft Excel 在 data 文件夹中建立考生库 Examinee.xls**,其中有一个考生信息表 Examinee**,按照下面的表结构录入考生的信息 数字想在excel中 呈字符形式就用 单引号 ‘
org.apache.poi.hssf**.* 实现 xls 的! http://poi.apache.org/
HSSF**(Horrible Spread Sheet Format) (可怕的电子表格格式**) 注意读取时行号 都从零开始;但是第一行又是考号!!!5—〉4
2.HSSF(Horrible Spread Sheet Format)
(可怕的电子表格格式)
包
类**/**接口
功能支持
org.apache.poi.hssf**.*
HSSFWorkbook
工作簿
HSSFSheet
工作表必须建立 sheet1位置的名称
HSSFRow
行
HSSFCell
单元格
3.使用**POI**
读取具体:请查看文档:
·1读取**Excel**文件:
String**类型的文件名name或File类型的文件对象**file
FileInputStream in = new FileInputStream**(name); //或file**
HSSFWorkbook workbook = new HSSFWorkbook**(in); //**工作簿
HSSFSheet sheet = workbook.getSheet**(工作表名); //**工作表 HSSFRow row = sheet.getRow**(行号); //行,从0**开始计 HSSFCell cell = row.getCell**(列号); //单元格,从0**开始计 Xxx xxx = cell.getXxxCellValue**(); //按类型获取数据,如:String,Numeric**
in.close**();**
·2使用**POI写入Excel**文件
1
String**类型的文件名name或File类型的文件对象**file
2
FileOutputStream out = new FileOutputStream**(name); //或file**
3
HSSFWorkbook workbook = new HSSFWorkbook**(); //**工作簿
4
HSSFSheet sheet = workbook.createSheet**(工作表名); //**工作表 HSSFRow row = sheet.createRow**(行号); //行,从0**开始计 HSSFCell cell = row.createCell**(列号); //单元格,从0**开始计 cell.setCellType**(类型); //如CellType.STRING,CellType.NUMERIC cell.setCellValue(值**); //**按类型设置数据,如:String、**double workbook.write**(out); //**写入
5
out.close**();**
4.ArrayList**的基本使用方法*
**List<Integer>** **list** **= new** **ArrayList****<>();** **int**** n = 10; for (****int** **i****=0;** **i****<n;** **i****++) {** **list.add****(i+1); }** l**int**** sum = 0; for (****int****x : list) {** **// for (****int** **i****=0;** **i****<****list.size****();** **i****++)** ** sum += x; }** l**list.clear****();** l**泛****型** l**菱形****推断** 
- java.util.List 注意拥有:
- 泛型:限制内部类型的范围! 1.一种类型<>直接放2. super 上限 extends下限
boolean
add**(Ee) Appends the specified element to the end of this list (optional operation).**
void
add**(int index, E element**) Inserts the specified element at the specified position in this list (optional operation).
void
clear**() Removes all of the elements from this list (optional operation).**
boolean
contains**(Object o**) Returns** true if this list contains the specified element.**
E
get**(int index**) Returns the element at the specified position in this list.
boolean
isEmpty**() Returns** true if this list contains no elements.
Iterator**<E>**
iterator**() Returns an iterator over the elements in this list in proper sequence.**
E
remove**(int index**) Removes the element at the specified position in this list (optional operation).
boolean
remove**(Object o**) Removes the first occurrence of the specified element from this list, if it is present.
E
set**(int index, E element**) Replaces the element at the specified position in this list with the specified element.
int
size**() Returns the number of elements in this list.**
class MyException extends Exception { //创建自定义异常类 String message; //定义String类型变量 public MyException(String ErrorMessagr) { //父类方法 message = ErrorMessagr; } public String getMessage(){ //覆盖getMessage()方法 return message; } } public class Captor { //创建类 static int quotient(int x,int y) throws MyException{//定义方法抛出异常 if(y < 0){ //判断参数是否小于0 throw new MyException("除数不能是负数");//异常信息 } return x/y;//返回值 } public static void main(String args\[\]){ //主方法 try{ //try语句包含可能发生异常的语句 int result = quotient(3,-1);//调用方法quotient() }catch (MyException e) { //处理自定义异常 System.out.println(e.getMessage()); //输出异常信息 } catch (ArithmeticException e) { //处理ArithmeticException异常 System.out.println("除数不能为0");//输出提示信息 } catch (Exception e) { //处理其他异常 System.out.println("程序发生了其他的异常"); //输出提示信息 } }