久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

java POI 如何實現(xiàn)Excel單元格內(nèi)容換行

瀏覽:133日期:2022-06-14 17:57:50
java POI Excel單元格內(nèi)容換行

java POI 如何實現(xiàn)Excel單元格內(nèi)容換行

pom.xml

<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.15</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.15</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.5</version> </dependency>核心代碼

@RestControllerpublic class MyController {@RequestMapping('/ip/v5')public void getExcel(HttpServletResponse response) throws IOException {ArrayList<String> arrayList = new ArrayList<String>();arrayList.add('this is 單元格第1行');arrayList.add('this is 單元格第2行');arrayList.add('this is 單元格第3行');arrayList.add('this is 單元格第4行');XSSFWorkbook workBook = new XSSFWorkbook();XSSFSheet sheet = workBook.createSheet();workBook.setSheetName(0, 'ip-v4表');XSSFCellStyle cs = workBook.createCellStyle(); // 換行的關(guān)鍵,自定義單元格內(nèi)容換行規(guī)則cs.setWrapText(true);String fileName = 'china-ip-v4' + '.xls';// 設(shè)置要導(dǎo)出的文件的名字String[] headers = { '掩碼' };XSSFRow titleRow = sheet.createRow(0);// 在excel表中添加表頭for (int i = 0; i < headers.length; i++) {titleRow.createCell(i).setCellValue(headers[i]);}String content = String.join('n', arrayList);int rowNum = 1;XSSFRow row1 = sheet.createRow(rowNum); // 創(chuàng)建一行XSSFCell cell = row1.createCell(0); // 創(chuàng)建一個單元格// 如下也是可以的//cell.setCellValue('this is 單元格第1行 n this is單元格第2行 n this is 單元格第3行 n this is 單元格第4行');cell.setCellValue(content);cell.setCellStyle(cs);response.setContentType('application/octet-stream');response.setHeader('Content-disposition', 'attachment;filename=' + fileName);response.flushBuffer();workBook.write(response.getOutputStream());}}

結(jié)果:

java POI 如何實現(xiàn)Excel單元格內(nèi)容換行

poi單元格寫值強制換行

String str='強制rn換行'

在字符串中間加上rn就行了~

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: excel
相關(guān)文章:
主站蜘蛛池模板: 东平县| 蓬莱市| 安阳市| 漯河市| 遵化市| 通化县| 乐清市| 砚山县| 洛扎县| 和顺县| 洪湖市| 新绛县| 织金县| 嵊州市| 怀柔区| 津市市| 云和县| 墨竹工卡县| 修文县| 甘南县| 邵阳县| 麟游县| 巩留县| 富宁县| 雅安市| 河北省| 东城区| 太谷县| 渭源县| 涞水县| 房产| 霸州市| 响水县| 建宁县| 江油市| 竹溪县| 新宾| 文山县| 永和县| 和田县| 宣恩县|