POI设置excel格式为文本格式
生活随笔
收集整理的這篇文章主要介紹了
POI设置excel格式为文本格式
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
java使用POI下載導(dǎo)入模板,常常會(huì)因?yàn)樽詭У母袷綖槌R?guī),導(dǎo)致導(dǎo)入的數(shù)據(jù)型內(nèi)容,默認(rèn)帶四位小數(shù),或者超過(guò)12位后使用科學(xué)計(jì)數(shù)型表示,但是服務(wù)器又需要驗(yàn)證數(shù)據(jù)的準(zhǔn)確性,所以往往很難校驗(yàn)數(shù)據(jù)的準(zhǔn)確性,用戶輸入也很麻煩,為了解決此種情況發(fā)生,往往給模板的默認(rèn)格式為文本格式,這樣就可以無(wú)視用戶的輸入,能夠更好的校驗(yàn)內(nèi)容,下面是使用java將Excel格式設(shè)置成文本的方法
/** * @desc 設(shè)置excel文本格式 * @param targetWorkbook * @param targetSheet * @param startRow * @param startColumn * @param endRow * @param endColumn */ public static void setColumnToTextFormat(XSSFWorkbook targetWorkbook, XSSFSheet targetSheet, int startRow, int startColumn, int endRow, int endColumn){XSSFCellStyle cellStyle = targetWorkbook.createCellStyle(); XSSFDataFormat format = targetWorkbook.createDataFormat(); cellStyle.setDataFormat(format.getFormat("@")); if(startRow <0 || endRow <0 || startColumn <0 || endColumn <0 || (startRow > endRow) || (startColumn > endColumn)){throw new ApplicationException("生成Excel格式參數(shù)錯(cuò)誤!"); }for(int rowIndex = startRow ; rowIndex <= endRow ; rowIndex ++ ){XSSFRow row = targetSheet.getRow(rowIndex); if(row == null ) {row = targetSheet.createRow(rowIndex); }for (int columnIndex = startColumn ; columnIndex <= endColumn ; columnIndex ++ ){XSSFCell cell = row.getCell(columnIndex); if (cell == null){cell = row.createCell(columnIndex); }String rawValue1 = cell.getRawValue(); cell.setCellStyle(cellStyle); if(StringUtil.isNotBlank(rawValue1)){cell.setCellValue(rawValue1); }}} }
總結(jié)
以上是生活随笔為你收集整理的POI设置excel格式为文本格式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Oracle system表空间用满解决
- 下一篇: c语言浮点常量,C 浮点常量