jxl操作excel

2019-02-15 13:06

用jxl读取excel的数据,由于excel数据在录入时的各种原因,数据后面都有空格,而且读出来以后(也许是编码原因),数据口面不是出 现\就是出现一个不知所谓的乱码符,不要考虑用替换,因为替换只有在你的项目编码方式和内存中excel数据编码方式一样的时候才能替换,否则你连保 存都会提示编码问题而保存不了。

直接用subSequence(0, cellContent.length()-1) 就可以了

同时提醒一下,读取出来的数据时Cell类型的话,直接getContent是可以得到内容的,但具体内容最好依靠下面的方法获 Java代码

1. if (cell.getType() == CellType.LABEL) {

2. LabelCell labelCell = (LabelCell) cell; 3. String cellContent = labelCell.getString();

4. cellContent = (String) cellContent.subSequence(0, cellContent.length()-1);

5. column_contents[cols] = cellContent; 6. }else

7. if (cell.getType() == CellType.NUMBER) {

8. //number的话不用去空格就可以,我测试是这样 9. NumberCell numberCell = (NumberCell) cell; 10. String cellContent = numberCell.getContents(); 11. column_contents[cols] = cellContent; 12.}else

13.if (cell.getType() == CellType.DATE) { 14. DateCell dateCell = (DateCell) cell; 15. Date dateDemo = dateCell.getDate();

16. String cellContent = dateDemo.toString(); 17. column_contents[cols] = cellContent; 18.}

package com.study.poi;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream;

import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Iterator; import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFDateUtil; 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.usermodel.Workbook;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.study.entity.Emp;

public class PoiExcelTest {

public static void main(String[] args) { Class[] clazz=new

Class[]{Integer.class,String.class,String.class,Integer.class,Date.class,Double.class,Double.class,Integer.class}; List list=null;

DecimalFormat df=new DecimalFormat(\ try {

list = readExcel(\ } catch (ParseException e) { e.printStackTrace(); }

for (Iterator iter=list.iterator(); iter.hasNext();) { Emp emp=iter.next();

System.out.println(\e()+\ew

SimpleDateFormat(\Deptno());

} }

private static int version2003=2003; private static int version2007=2003; private static int version=version2003; private static Workbook wb; private static Sheet sheet; private static Row row; private static Cell cell;

public static List readExcel(String excelFilePath,Class[] clazz) throws ParseException{

List list=new ArrayList(); Emp emp;

version=(excelFilePath.endsWith(\07);

if(version==2003){ try {

InputStream stream=new FileInputStream(new File(excelFilePath));

wb=new HSSFWorkbook(stream); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }

}else if(version==2007){ try {

wb=new XSSFWorkbook(excelFilePath); } catch (IOException e) { e.printStackTrace(); } }

sheet=wb.getSheetAt(0);

int rows=sheet.getLastRowNum();

int cells=sheet.getRow(0).getPhysicalNumberOfCells(); for (int i = 0; i < rows; i++) { row=sheet.getRow(i+1); emp=new Emp();

for (int j = 0; j < cells; j++) { cell=row.getCell(j);

Object obj=getCellValue(cell, clazz[j]); switch (j) { case 0:

emp.setEmpno((Integer)obj);

break; case 1:

emp.setEname((String)obj); break; case 2:

emp.setJob((String)obj); break; case 3:

emp.setMgr((Integer)obj); break; case 4:

emp.setHiredate(new

SimpleDateFormat(\ break; case 5:

emp.setSal((Double)obj); break; case 6:

emp.setComm((Double)obj); break; case 7:

emp.setDeptno((Integer)obj); break; default: break; } }

list.add(emp); }

return list; }

public static Object getCellValue(Cell cell,Class clazz){ String name=clazz.getSimpleName(); if(\

return cell.getStringCellValue();

}else if(\ try {

double valued=cell.getNumericCellValue(); return valued;

} catch (Exception e) { return 0.0; }

}else if(\

try {

int valuei=(int)cell.getNumericCellValue(); return valuei;

} catch (Exception e) { return 0; }

}else if(\

if(HSSFDateUtil.isCellDateFormatted(cell)){ Date date=cell.getDateCellValue(); if(date==null){ return new

SimpleDateFormat(\ }else{

return new

SimpleDateFormat(\ } } }

return null; } }

1. public class SummaryHSSF { 2.

3. public static void main(String[] args) throws IOException {

4. //创建Workbook对象(这一个对象代表着对应的一个Excel文件)

5. //HSSFWorkbook表示以xls为后缀名的文件 6. Workbook wb = new HSSFWorkbook();

7. //获得CreationHelper对象,这个应该是一个帮助类 8. CreationHelper helper = wb.getCreationHelper(); 9. //创建Sheet并给名字(表示Excel的一个Sheet)

10. Sheet sheet1 = wb.createSheet(\11. Sheet sheet2 = wb.createSheet(\12. //Row表示一行Cell表示一列 13. Row row = null; 14. Cell cell = null;


jxl操作excel.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:电阻炉温度控制系统毕业设计

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: