POI+Set_JavaClass_1028

POI与Excel文件+集合

1.大纲

  • 表示文件
  • 建立考生库
  • POI
  • 集合类
  • 读取**Excel**文件
  • 写入**Excel**文件
  1. 考生库
    成绩库

  2. 实验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**类型的文件名nameFile类型的文件对象**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**(); //按类型获取数据,如:StringNumeric**

in.close**();**


·2使用**POI写入Excel**文件

1

String**类型的文件名nameFile类型的文件对象**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.STRINGCellType.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**菱形****推断** ![%e6%8d%95%e8%8e%b71028](http://123.206.69.206/wp-content/uploads/2016/10/捕获1028-300x212.png)
  • 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![搜索](https://gss0.bdstatic.com/70cFsjip0QIZ8tyhnq/img/iknow/qb/select-search.png); } 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("程序发生了其他的异常"); //输出提示信息 } }
(っ•̀ω•́)っ✎⁾⁾ 坚持技术学习、内容输出与分享,您的支持将鼓励我继续创作!(*/ω\*)
( • ̀ω•́ )✧如有疑问或需要技术讨论,请留言或发邮件到 aclearzhang@qq.com.(*・ω< ) 
  • 本文作者:: AClearZhang
  • 本文链接:: 214.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处!