java實(shí)現(xiàn)對(duì)excel文件的處理合并單元格的操作
<dependency><groupId>net.sourceforge.jexcelapi</groupId><artifactId>jxl</artifactId><version>2.6.12</version></dependency>二、表格操作
1、讀取xls文件
測(cè)試文件為:
代碼:
public void test() throws IOException, BiffException {// 1、獲取文件,創(chuàng)建workbookFile file = new File('D:/test/自動(dòng)化監(jiān)測(cè)數(shù)據(jù)上傳模板20210525.xls');Workbook workbook = Workbook.getWorkbook(file);// 2.獲取第一個(gè)工作表Sheet sheet = workbook.getSheet(0);// 3.獲取表中數(shù)據(jù)Range[] rangecell = sheet.getMergedCells();System.out.println('行:' + sheet.getRows());System.out.println('列:' + sheet.getColumns());for (int i = 0; i < sheet.getRows(); i++) { for (int j = 0; j < sheet.getColumns(); j++) {Cell cell = sheet.getCell(j, i);String contents = cell.getContents();System.out.print(contents + ' '); } System.out.println();}workbook.close(); }
輸出結(jié)果(注意合并單元格處,需要特殊處理):
改造代碼如下:
public void test() throws IOException, BiffException {// 1、獲取文件,創(chuàng)建workbookFile file = new File('D:/test/自動(dòng)化監(jiān)測(cè)數(shù)據(jù)上傳模板20210525.xls');Workbook workbook = Workbook.getWorkbook(file);// 2.獲取第一個(gè)工作表Sheet sheet = workbook.getSheet(0);// 3.獲取表中數(shù)據(jù)// 返回合并單元格數(shù)據(jù)Range[] rangecell = sheet.getMergedCells();System.out.println('行:' + sheet.getRows());System.out.println('列:' + sheet.getColumns());for (int i = 0; i < sheet.getRows(); i++) { for (int j = 0; j < sheet.getColumns(); j++) {Cell cell = sheet.getCell(j, i);String contents = cell.getContents();// 判斷當(dāng)前單元格,是否為合并單元格for (Range r : rangecell) { if (i > r.getTopLeft().getRow() && i <= r.getBottomRight().getRow() && j >= r.getTopLeft().getColumn() && j <= r.getBottomRight().getColumn()) {contents = sheet.getCell(r.getTopLeft().getColumn(), r.getTopLeft().getRow()).getContents(); }}System.out.print(contents + ' '); } System.out.println();}workbook.close(); }
結(jié)果:
到此這篇關(guān)于java實(shí)現(xiàn)對(duì)excel文件的處理合并單元格的文章就介紹到這了,更多相關(guān)java excel文件合并單元格內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ppt水滴主題在哪設(shè)置2. 微信小程序新手教程之彈框組件modal3. iphone11如何下載小紅書(shū)4. 蘋(píng)果手機(jī)微信視頻怎么設(shè)置鈴聲5. 微信艾特所有人的消息可以不提示嗎6. iphone11pro怎么關(guān)閉微信免密支付7. 快手小店我的評(píng)價(jià)在哪里能找到8. 怎么使用高級(jí)篩選?excel2007高級(jí)篩選使用方法9. 小紅書(shū)app能開(kāi)發(fā)票嗎?小紅書(shū)app開(kāi)發(fā)票的方法10. QQ瀏覽器手機(jī)版怎么設(shè)置屏幕旋轉(zhuǎn)?屏幕旋轉(zhuǎn)設(shè)置流程分享
