javaexcel数字
⑴ java使用poi解析或处理excel的时候,如何防止数字变成科学计数法的
思路为:为了防止数字变成科学计数法方式表示,在源文件以及java代码中都用文本的方式去生成和解析excel,具体如下:
1.生成Excel时,设置单元格格式为STRING,即:
//关键代码
HSSFCellcell=newHSSFCell();
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
2.同理,解析的时候,首先要保证源excel文件中该单元格格式是文本类型的,然后在java代码里用STRING类型去解析:
//关键代码
Stringvalue=cell.getStringCellValue();
⑵ java读取excel 不知道excel里面具体内容是时间还是数字或者是字符串 如何正确读取呢
java读取excel时间格式出现数字的处理方法:
Excel存储日期、时间均以数值类型进行存储,读取时POI先判断是是否是数值类型,再进行判断转化
1、数值格式(CELL_TYPE_NUMERIC):
1.纯数值格式:getNumericCellValue() 直接获取数据
2.日期格式:处理yyyy-MM-dd, d/m/yyyy h:mm, HH:mm 等不含文字的日期格式
1).判断是否是日期格式:HSSFDateUtil.isCellDateFormatted(cell)
2).判断是日期或者时间
cell.getCellStyle().getDataFormat() == HSSFDataFormat.getBuiltinFormat("h:mm")
OR: cell.getCellStyle().getDataFormat() == HSSFDataFormat.getBuiltinFormat("yyyy-MM-dd")
3.自定义日期格式:处理yyyy年m月d日,h时mm分,yyyy年m月等含文字的日期格式
判断cell.getCellStyle().getDataFormat()值,解析数值格式
yyyy年m月d日----->31
m月d日---->58
h时mm分--->32
举例说明:
private String parseExcel(Cell cell) {
String result = new String();
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC:// 数字类型
if (HSSFDateUtil.isCellDateFormatted(cell)) {// 处理日期格式、时间格式
SimpleDateFormat sdf = null;
if (cell.getCellStyle().getDataFormat() == HSSFDataFormat
.getBuiltinFormat("h:mm")) {
sdf = new SimpleDateFormat("HH:mm");
} else {// 日期
sdf = new SimpleDateFormat("yyyy-MM-dd");
}
Date date = cell.getDateCellValue();
result = sdf.format(date);
} else if (cell.getCellStyle().getDataFormat() == 58) {
// 处理自定义日期格式:m月d日(通过判断单元格的格式id解决,id的值是58)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
double value = cell.getNumericCellValue();
Date date = org.apache.poi.ss.usermodel.DateUtil
.getJavaDate(value);
result = sdf.format(date);
} else {
double value = cell.getNumericCellValue();
CellStyle style = cell.getCellStyle();
DecimalFormat format = new DecimalFormat();
String temp = style.getDataFormatString();
// 单元格设置成常规
if (temp.equals("General")) {
format.applyPattern("#");
}
result = format.format(value);
}
break;
case HSSFCell.CELL_TYPE_STRING:// String类型
result = cell.getRichStringCellValue().toString();
break;
case HSSFCell.CELL_TYPE_BLANK:
result = "";
default:
result = "";
break;
}
return result;
}
⑶ 怎样用java向excel中写数据
public void export(List<PSbLnode> li, String dateString,String[] title) throws WriteException, IOException {
// 准备设置excel工作表的标题
// 创建Excel工作薄
WritableWorkbook wwb = null;
try {
// 输出的excel的路径
String filePath1 = Const.pathStr+Const.pathStr4+Const.pathStr3;
File file = new File(filePath1);
if(!file.exists()){
file.mkdir();
}
String filePath=filePath1+Const.pathStr4+Const.pathStr6+dateString+Const.pathStr5;
// 新建立一个jxl文件,即在C盘下生成testJXL.xls
OutputStream os = new FileOutputStream(filePath);
wwb = Workbook.createWorkbook(os);
// 添加第一个工作表并设置第一个Sheet的名字
WritableSheet sheet = wwb.createSheet("设备清单", 0);
Label label;
for (int i = 0; i < title.length; i++) {
// Label(x,y,z) 代表单元格的第x+1列,第y+1行, 内容z
// 在Label对象的子对象中指明单元格的位置和内容
label = new Label(i, 0, title[i]);
// 将定义好的单元格添加到工作表中
sheet.addCell(label);
}
for (int i = 0; i < li.size(); i++) {
int j = 0;
j = i + 1;
//填充单元格
//获取区域名称
label = new Label(0, j, li.get(i).getQyName());
sheet.addCell(label);
//获取区域名称
label = new Label(1, j, li.get(i).getJzName());
sheet.addCell(label);
//获取设备名称
label = new Label(2, j, li.get(i).getLnodeName());
sheet.addCell(label);
// //获取设备类型名称
label = new Label(3, j, li.get(i).getSbxh());
sheet.addCell(label);
//获取运行状态
label = new Label(4, j, li.get(i).getYxzh());
sheet.addCell(label);
//获取删除状态
label = new Label(5, j, li.get(i).getDeleteFlag());
sheet.addCell(label);
//获取启用状态
label = new Label(6, j, li.get(i).getQyzt());
sheet.addCell(label);
//获取设备投运日期
label = new Label(7, j, li.get(i).getSbtyri());
sheet.addCell(label);
//获取使用年限
jxl.write.Number numb1 = new jxl.write.Number(8, j, li.get(i).getSynx());
sheet.addCell(numb1);
//获取区域名称
label = new Label(9, j, li.get(i).getAddUser());
sheet.addCell(label);
//获取区域名称
label = new Label(10, j, li.get(i).getUpdUser());
sheet.addCell(label);
//获取区域名称
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String newdate = sdf.format(li.get(i).getUpdTime());
label = new Label(11, j, newdate);
sheet.addCell(label);
//获取区域名称
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
String newdate2 = sdf2.format(li.get(i).getAddTime());
label = new Label(12, j, newdate2);
sheet.addCell(label);
//获取区域名称
label = new Label(13, j, li.get(i).getZcbh());
sheet.addCell(label);
//获取区域名称
label = new Label(14, j, li.get(i).getSbcs());
sheet.addCell(label);
//获取区域名称
jxl.write.Number numb2 = new jxl.write.Number(15, j, li.get(i)
.getSbll());
sheet.addCell(numb2);
//获取区域名称
label = new Label(16, j, li.get(i).getRldw());
sheet.addCell(label);
//获取区域名称
label = new Label(17, j, li.get(i).getWxghjl());
sheet.addCell(label);
}
// 写入数据
wwb.write();
} catch (Exception e) {
e.printStackTrace();
}finally{
// 关闭文件
wwb.close();
}
}
Const文件:
/**路径:C盘*/
public static String pathStr = "C:";
/**路径://*/
public static String pathStr2 = "//";
/**文件夹:workspace*/
public static String pathStr3 = "exportFile";
/**文件名:world*/
public static String pathStr6 = "Equipment";
/**路径:/*/
public static String pathStr4 = "/";
/**路径:.xls*/
public static String pathStr5 = ".xls";
⑷ JAVA生成Excel 数字格式化后整数带有.0问题
可以试试 对 value取小数部分 判断是否大于0 大于0使用6位格式化,不大于0直接转整型再cell.setCellValue