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