日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

POI设置excel格式为文本格式

發布時間:2023/12/16 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POI设置excel格式为文本格式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

java使用POI下載導入模板,常常會因為自帶的格式為常規,導致導入的數據型內容,默認帶四位小數,或者超過12位后使用科學計數型表示,但是服務器又需要驗證數據的準確性,所以往往很難校驗數據的準確性,用戶輸入也很麻煩,為了解決此種情況發生,往往給模板的默認格式為文本格式,這樣就可以無視用戶的輸入,能夠更好的校驗內容,下面是使用java將Excel格式設置成文本的方法

/** * @desc 設置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格式參數錯誤!"); }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); }}} }

總結

以上是生活随笔為你收集整理的POI设置excel格式为文本格式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。