jxl操作excel

上传人:第*** 文档编号:31077883 上传时间:2018-02-04 格式:DOCX 页数:53 大小:65.63KB
返回 下载 相关 举报
jxl操作excel_第1页
第1页 / 共53页
jxl操作excel_第2页
第2页 / 共53页
jxl操作excel_第3页
第3页 / 共53页
jxl操作excel_第4页
第4页 / 共53页
jxl操作excel_第5页
第5页 / 共53页
点击查看更多>>
资源描述

《jxl操作excel》由会员分享,可在线阅读,更多相关《jxl操作excel(53页珍藏版)》请在金锄头文库上搜索。

1、用 jxl 读取 excel 的数据,由于 excel 数据在录入时的各种原因,数据后面都有空格,而且读出来以后(也许是编码原因),数据口面不是出 现?就是出现一个不知所谓的乱码符,不要考虑用替换,因为替换只有在你的项目编码方式和内存中 excel 数据编码方式一样的时候才能替换,否则你连保 存都会提示编码问题而保存不了。直接用 subSequence(0, cellContent.length()-1) 就可以了同时提醒一下,读取出来的数据时 Cell 类型的话,直接 getContent 是可以得到内容的,但具体内容最好依靠下面的方法获Java 代码 1. if (cell.getType

2、() = CellType.LABEL) 2. LabelCell labelCell = (LabelCell) cell; 3. String cellContent = labelCell.getString(); 4. cellContent = (String) cellContent.subSequence(0, cellContent.length()-1); 5. column_contentscols = cellContent; 6. else 7. if (cell.getType() = CellType.NUMBER) 8. /number 的话不用去空格就可以,我测

3、试是这样 9. NumberCell numberCell = (NumberCell) cell; 10. String cellContent = numberCell.getContents(); 11. column_contentscols = cellContent; 12.else 13.if (cell.getType() = CellType.DATE) 14. DateCell dateCell = (DateCell) cell; 15. Date dateDemo = dateCell.getDate(); 16. String cellContent = dateDe

4、mo.toString(); 17. column_contentscols = 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.tex

5、t.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;impor

6、t 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 ClassInteger.class,String.class,String.class,Integer.class,Date

7、.class,Double.class,Double.class,Integer.class;List list=null;DecimalFormat df=new DecimalFormat(0.00);try list = readExcel(d:+File.separator+test.xls,clazz); catch (ParseException e) e.printStackTrace();for (Iterator iter=list.iterator(); iter.hasNext();) Emp emp=iter.next();System.out.println(Emp:

8、+emp.getEmpno()+t+emp.getEname()+t+df.format(emp.getSal()+t+df.format(emp.getComm()+t+new SimpleDateFormat(yyyy-MM-dd).format(emp.getHiredate()+t+emp.getDeptno(); private static int version2003=2003;private static int version2007=2003;private static int version=version2003;private static Workbook wb

9、;private static Sheet sheet;private static Row row;private static Cell cell;public static List readExcel(String excelFilePath,Class clazz) throws ParseExceptionList list=new ArrayList();Emp emp;version=(excelFilePath.endsWith(.xls)?version2003:version2007);if(version=2003)try InputStream stream=new

10、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=s

11、heet.getLastRowNum();int cells=sheet.getRow(0).getPhysicalNumberOfCells();for (int i = 0; i rit = (Iterator)sheet.rowIterator(); rit.hasNext(); ) 11. HSSFRow row = rit.next(); 12. for (Iterator cit = (Iterator)row.cellIterator(); cit.hasNext(); ) 13. HSSFCell cell = cit.next(); 14. / Do something he

12、re 15. 16. 1.jxl 生成报表Java 代码 1. package excel; 2. 3. import java.io.FileOutputStream; 4. import java.io.OutputStream; 5. import java.text.SimpleDateFormat; 6. import java.util.Date; 7. 8. import jxl.Workbook; 9. import jxl.format.Alignment; 10.import jxl.format.Border; 11.import jxl.format.BorderLin

13、eStyle; 12.import jxl.format.CellFormat; 13.import jxl.write.Label; 14.import jxl.write.WritableCellFormat; 15.import jxl.write.WritableFont; 16.import jxl.write.WritableSheet; 17.import jxl.write.WritableWorkbook; 18./* 19. * 20. * jxlCreate.java 21. * copyright Copyright: 2009-2012 22. * creator 周

14、辉 23. * create-time Mar 9, 2010 1:35:19 PM 24. * revision $Id: * 25. */ 26.public class jxlCreate 27. 28. /* 29. * param args 30. */ 31. public static void main(String args) 32. / 准备设置 excel 工作表的标题 33. String title = 编号,产品名称,产品价格,产品数量,生产日期,产地,是否出口; 34. try 35. / 获得开始时间 36. long start = System.currentTimeMillis(); 37. / 输出的 excel 的路径 38. String filePath = c:test.xls; 39. / 创建 Excel 工作薄 40. WritableWorkbook wwb; 41. / 新建立一个 jxl

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号