Java jxl导入excel文件,导入的数字、身份证号码、手机号变成了科学计数法,解决方案

这是一个execl文件导入数据库操作,使用jxl解析execl导入数据库过程出现了科学计数法,与想要导入的数据不匹配,以下是案例以及解决方案:

导入成功后示例
1、手机号:15388886666 科学计数法:1.54E+10
2、数字:123456789000000 科学计数法:1.23E+14
3、身份证:432222198808083789 科学计数法:4.32E+17

解决思路
1、判断是否为数字类型(NUMBER)或数字计算公式(NUMBER_FORMULA);
2、获取解析后的值进行判断是否包含有(E、e、+等符号);
3、使用java自带数学类,将科学计算公式转换成所需类型。

具体代码
所需maven依赖:

<dependencies>
	<dependency>
		<groupId>net.sourceforge.jexcelapi</groupId>
		<artifactId>jxl</artifactId>
		<version>2.6.10</version>
	</dependency>
	<dependency>
	  <groupId>commons-fileupload</groupId>
	  <artifactId>commons-fileupload</artifactId>
	  <version>1.3.1</version>
	</dependency>
	<dependency>
	  <groupId>commons-io</groupId>
	  <artifactId>commons-io</artifactId>
	  <version>2.4</version>
	</dependency>
</dependencies>

操作excel工具类:

//  解析Execl
package com.seesun2012.util.execl;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import jxl.Cell;
import jxl.CellType;
import jxl.DateCell;
import jxl.LabelCell;
import jxl.NumberCell;
import jxl.Sheet;
import jxl.Workbook;

public class ImportExeclFile {

	
	/**
	 * 根据【路径】读取excel表格
	 * 
	 * @param filePath		文件路径
	 * @param startRow		行,开始读取的位置(默认从第1行)
	 * @param startCell		列,开始读取的位置(默认从第A列)
	 * @param cutEndNum		截取不必要的列(从表格数据区最后一行往前截取,默认不截取,必须是正整数)
	 * @return
	 */
	public static List<String[]> importExcelByPath(String filePath, Integer startRow, Integer startCell, Integer cutEndNum) throws Exception {
		List<String[]> list = new ArrayList<String[]>();
		// 日期的格式化
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			// 以IO流的形式读取文件
			InputStream in = new FileInputStream(filePath);
			// 获取工作簿
			Workbook book = Workbook.getWorkbook(in);
			// 获取工作表
			Sheet sheet = book.getSheet(0);
			// 得到总列数
			int columns = sheet.getColumns();
			cutEndNum = (cutEndNum == null || cutEndNum < 0) ? 0 : cutEndNum;
			// 得到总行数
			int rows = sheet.getRows()-cutEndNum;
			startRow = (startRow == null || startRow < 0) ? 0 : startRow;
			startCell = (startCell == null || startCell < 0) ? 0 : startCell;
			for (int k = startRow; k < rows; k++) { // 行
				String[] row = new String[columns];
				for (int i = startCell; i < columns; i++) { // 列
					Cell cell = sheet.getCell(i, k);
					// 获得cell具体类型值的方式
					if (cell.getType() == CellType.LABEL) {
						LabelCell labelcell = (LabelCell) cell;
						row[i] = labelcell.getString();
					} else if (cell.getType() == CellType.DATE) {// excel 类型为时间类型处理;
						DateCell dc = (DateCell) cell;
						row[i] = sdf.format(dc.getDate());
					} else if (cell.getType() == CellType.NUMBER || cell.getType() == CellType.NUMBER_FORMULA) {// excel
						NumberCell nc = (NumberCell) cell;
						// 判断是否为科学计数法(包含E、e、+等符号)
						if (("" + nc.getValue()).indexOf("E") != -1 || ("" + nc.getValue()).indexOf("e") != -1 || ("" + nc.getValue()).indexOf("+") != -1) {
							BigDecimal bd = new BigDecimal("" + nc.getValue());
							if (("" + nc.getValue()).indexOf("E") != -1 || ("" + nc.getValue()).indexOf("e") != -1 || ("" + nc.getValue()).indexOf("+") != -1) {
								bd = new BigDecimal(Double.parseDouble(bd.toString()));
								row[i] = bd.toString();
							} else {
								row[i] = "" + nc.getValue();
							}
						} else {
							row[i] = "" + nc.getValue();
						}
					} else {
						// 通用的获取cell值的方式,返回字符串
						row[i] = cell.getContents();
					}
				}
				// 添加到list集合中
				list.add(row);
			}
			// 关闭工作薄
			book.close();
		} catch (Exception exception) {
			throw exception;	//继续向外抛
		}
		return list;
	}
	
	
	
	/**
	 * 根据【文件流】读取excel表格
	 * 
	 * @param filePath		文件流
	 * @param startRow		行,开始读取的位置(默认从第1行)
	 * @param startCell		列,开始读取的位置(默认从第A列)
	 * @param cutEndNum		截取不必要的列(从表格数据区最后一行往前截取,默认不截取,必须是正整数)
	 * @return
	 */
	public static List<String[]> importExcelByFile(File file, Integer startRow, Integer startCell, Integer cutEndNum) throws Exception {
		List<String[]> list = new ArrayList<String[]>();
		// 日期的格式化
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			// 以IO流的形式读取文件
			InputStream in = new FileInputStream(file);
			// 获取工作簿
			Workbook book = Workbook.getWorkbook(in);
			// 获取工作表
			Sheet sheet = book.getSheet(0);
			// 得到总列数
			int columns = sheet.getColumns();
			cutEndNum = (cutEndNum == null || cutEndNum < 0) ? 0 : cutEndNum;
			// 得到总行数
			int rows = sheet.getRows()-cutEndNum;
			startRow = (startRow == null || startRow < 0) ? 0 : startRow;
			startCell = (startCell == null || startCell < 0) ? 0 : startCell;
			for (int k = startRow; k < rows; k++) { // 行
				String[] row = new String[columns];
				for (int i = startCell; i < columns; i++) { // 列
					Cell cell = sheet.getCell(i, k);
					// 获得cell具体类型值的方式
					if (cell.getType() == CellType.LABEL) {
						LabelCell labelcell = (LabelCell) cell;
						row[i] = labelcell.getString();
					} else if (cell.getType() == CellType.DATE) {// excel 类型为时间类型处理;
						DateCell dc = (DateCell) cell;
						row[i] = sdf.format(dc.getDate());
					} else if (cell.getType() == CellType.NUMBER || cell.getType() == CellType.NUMBER_FORMULA) {// excel
						NumberCell nc = (NumberCell) cell;
						// 判断是否为科学计数法(包含E、e、+等符号)
						if (("" + nc.getValue()).indexOf("E") != -1 || ("" + nc.getValue()).indexOf("e") != -1 || ("" + nc.getValue()).indexOf("+") != -1) {
							BigDecimal bd = new BigDecimal("" + nc.getValue());
							if (("" + nc.getValue()).indexOf("E") != -1 || ("" + nc.getValue()).indexOf("e") != -1 || ("" + nc.getValue()).indexOf("+") != -1) {
								bd = new BigDecimal(Double.parseDouble(bd.toString()));
								row[i] = bd.toString();
							} else {
								row[i] = "" + nc.getValue();
							}
						} else {
							row[i] = "" + nc.getValue();
						}
					} else {
						// 通用的获取cell值的方式,返回字符串
						row[i] = cell.getContents();
					}
				}
				// 添加到list集合中
				list.add(row);
			}
			// 关闭工作薄
			book.close();
		} catch (Exception exception) {
			throw exception;	//继续向外抛
		}
		return list;
	}

}

测试类:

package com.seesun2012.test.demo;

import com.seesun2012.util.execl.ImportExeclFile;

public class ImportExeclFile {

	public static void main(String[] args) throws Exception {
		List<String[]> excel = ImportExeclFile.importExcelByFile(new File("D:\\微信支付宝交易统计2015-01-20.xls"), 1, 0, 1);
		for (String[] arrStr : excel) {
			for (String str : arrStr) {
				System.out.print(str+"\t");
			}
			System.out.println();
		}
	}
	
}


























注:以上内容仅提供参考和交流,请勿用于商业用途,如有侵权联系本人删除!


持续更新中…

如有对思路不清晰或有更好的解决思路,欢迎与本人交流,QQ群:273557553,个人微信:
你遇到的问题是小编创作灵感的来源!


  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值