用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
DecimalFormat df=new DecimalFormat(\ try {
list = readExcel(\ } catch (ParseException e) { e.printStackTrace(); }
for (Iterator
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
List
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;