赞
踩
Workbook解析excel文件 读取信息
- package com.dept.web.context;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.List;
-
- import jxl.Cell;
- import jxl.Sheet;
- import jxl.Workbook;
- public class Read_excel{
- public static void main(String[] args) {
- Sheet sheet;
- Workbook book;
- Cell cell1,cell2,cell3,cell4,cell5,cell6,cell7;
- try {
- //t.xls为要读取的excel文件名
- book= Workbook.getWorkbook(new File("E:/zhejiang.xls"));
- //获得第一个工作表对象(ecxel中sheet的编号从0开始,0,1,2,3,....)
- sheet=book.getSheet(1);
- //获取左上角的单元格
- cell1=sheet.getCell(0,0);
- // 注意:一定要设成这个,否则可能会出现乱码
- cell.setEncoding(HSSFCell.ENCODING_UTF_16);
- System.out.println("标题:"+cell1.getContents());
- int num=sheet.getColumns();
- List<Stock> list=new ArrayList<>();
- for (int i=2 ; i < num; i++) {
- cell1=sheet.getCell(1,i);//(列,行)
- cell2=sheet.getCell(2,i);
- cell3=sheet.getCell(3,i);
- cell4=sheet.getCell(4,i);
- cell5=sheet.getCell(5,i);
- cell6=sheet.getCell(6,i);
- cell7=sheet.getCell(7,i);
- //获取每一行的单元格
- System.out.println(cell1.getContents()+"\t"+cell2.getContents()+"\t"+cell3.getContents()+"\t"+cell4.getContents()+"\t"+cell5.getContents()+"\t"+cell6.getContents()+"\t"+cell7.getContents()+"\t");
- Stock stock=new Stock();
- stock.setGpbk(cell1.getContents());
- stock.setDm(Integer.valueOf(cell3.getContents()));
- stock.setQymc(cell4.getContents());
- stock.setZyyw(cell5.getContents());
- stock.setDz(cell6.getContents());
- stock.setSspt(cell7.getContents());
- list.add(stock);
- }
- book.close();
- }
- catch(Exception e) {
- e.printStackTrace();
- }
- }
-
- /**
- * 判断字符串是否为空
- *
- * @param str
- * @return
- */
- public static boolean isEmpty(String str) {
- if (str == null || "".equals(str)) {
- return true;
- }
- return false;
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。