Java實(shí)現(xiàn)操作excel表格
最近老師布置了個(gè)任務(wù),用Java對(duì)excel后綴名為xlsx的文件進(jìn)行簡(jiǎn)單的增,刪,改,查操作;雖說(shuō)是個(gè)簡(jiǎn)單的程序,可作為剛接觸的我來(lái)說(shuō)還是有些磕磕碰碰。不過(guò)好在還是完成了,進(jìn)行一個(gè)簡(jiǎn)單的總結(jié)。
首先導(dǎo)入了一個(gè)poi.jar 網(wǎng)上有很多這個(gè)資源可以下載
XSSFSheet sheet=null;
XSSFWorkbook book=null;
一:查 (查找本地指定位置的excel表格,在控制臺(tái)輸出)
public void print_excel(){ //獲取excel表格的行數(shù) int lastrownumber = sheet.getLastRowNum(); String ret=" "; //獲取數(shù)據(jù) for(a=0;a<lastrownumber;a++){ XSSFRow row=sheet.getRow(a); //獲取excel表格的列數(shù) int lastcellnum=row.getLastCellNum(); for(b=0;b<lastcellnum;b++){ XSSFCell cell =row.getCell(b); //判斷cell返回的類(lèi)型并賦值給ret ret=excel_operation.getExcelCellValue(cell); System.out.print(ret+" "); } System.out.println(); } }
二:改 (修改excel表格中某一單元格的內(nèi)容)
public void set_excelcell(int i,int j,String str){ //獲取行的信息 XSSFRow row=sheet.getRow(i-1); //獲取列的信息 XSSFCell cell =row.getCell(j-1); //獲取被修改單元格的內(nèi)容 String string = excel_operation.getExcelCellValue(cell); //修改單元格的內(nèi)容為str cell.setCellValue(str); System.out.println("已將"+string+"改為"+str); }
三:增 (在excel表格中插入一行內(nèi)容到指定位置)
public void insert(int rowIndex, String[] objs) { if(rowIndex == 0) { throw new IllegalArgumentException("不能插在第0行,第0行是用來(lái)定義的!"); } if(rowIndex > sheet.getLastRowNum() + 1) { throw new IllegalArgumentException("最多只能插入在最后一行的后面。"); } int referRowIndex = -1; //參考行的行號(hào)。 if(sheet.getPhysicalNumberOfRows() <= 1) { referRowIndex = rowIndex - 1; } else { referRowIndex = rowIndex - 1; if(rowIndex == sheet.getLastRowNum() + 1) { //是插入最后一行 //不做任何處理 } else { //往下移動(dòng)一位 sheet.shiftRows(rowIndex, sheet.getLastRowNum(), 1, true, false); } } Row targetRow = sheet.createRow(rowIndex); Row referRow = sheet.getRow(referRowIndex); // 參考行 Cell targetCell, referCell; for (int i = 0; i < objs.length; i++) { targetCell = targetRow.createCell(i); referCell = referRow.getCell(i); targetCell.setCellStyle(referCell.getCellStyle()); targetCell.setCellType(referCell.getCellType()); targetCell.setCellValue(objs[i]);// 設(shè)置值 } }
四: 刪 (刪除指定行的內(nèi)容)
// 刪除一行數(shù)據(jù)(Excel表中,行是從0起算的) public void delete(int rowIndex) { //刪除的是最后一行 if(rowIndex == sheet.getLastRowNum()) { sheet.removeRow(sheet.getRow(sheet.getLastRowNum())); //刪除的不是最后一行 } else { sheet.shiftRows(rowIndex + 1, sheet.getLastRowNum(), -1, true, false); sheet.removeRow(sheet.getRow(sheet.getLastRowNum() + 1)); } }
五: 判斷返回類(lèi)型 (因?yàn)閑xcel表格中的內(nèi)容不同,有字符型的,有整數(shù)型的等等,必須進(jìn)行判斷其類(lèi)型才能進(jìn)行輸出)
private static String getExcelCellValue(XSSFCell cell) { String ret=" "; try { //當(dāng)返回值的類(lèi)型為空返回空格 if (cell == null) { ret = " "; //當(dāng)返回值的類(lèi)型為字符串類(lèi)型 } else if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING) { ret = cell.getStringCellValue(); //當(dāng)返回值的類(lèi)型為數(shù)值類(lèi)型 } else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) { ret = "" + cell.getNumericCellValue(); //當(dāng)返回值的類(lèi)型為表達(dá)式類(lèi)型 } else if (cell.getCellType() == XSSFCell.CELL_TYPE_FORMULA) { ret = cell.getCellFormula(); //當(dāng)返回值的類(lèi)型為異常類(lèi)型 } else if (cell.getCellType() == XSSFCell.CELL_TYPE_ERROR) { ret = " " + cell.getErrorCellValue(); //當(dāng)返回值的類(lèi)型為布爾類(lèi)型 } else if (cell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) { ret = " " + cell.getBooleanCellValue(); //當(dāng)返回值的類(lèi)型為空的時(shí)候 } else if (cell.getCellType() == XSSFCell.CELL_TYPE_BLANK) { ret = " "; } } catch (Exception ex) { ex.printStackTrace(); ret = " "; } return ret; }
相關(guān)文章
java把字符串寫(xiě)入文件里的簡(jiǎn)單方法分享
這篇文章主要介紹了java把字符串寫(xiě)入到文件里的簡(jiǎn)單方法,這是跟一個(gè)外國(guó)朋友學(xué)的代碼,需要的朋友可以參考下2014-03-03Java CountDownLatch計(jì)數(shù)器與CyclicBarrier循環(huán)屏障
CountDownLatch是一種同步輔助,允許一個(gè)或多個(gè)線程等待其他線程中正在執(zhí)行的操作的ASET完成。它允許一組線程同時(shí)等待到達(dá)一個(gè)共同的障礙點(diǎn)2023-04-04Springboot插件開(kāi)發(fā)實(shí)戰(zhàn)分享
這篇文章主要介紹了Springboot插件開(kāi)發(fā)實(shí)戰(zhàn)分享,文章通過(guò)新建aop切面執(zhí)行類(lèi)MonitorLogInterceptor展開(kāi)詳細(xì)的相關(guān)內(nèi)容,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-05-05Idea 解決 Could not autowire. No beans of ''xxxx'' type found
這篇文章主要介紹了Idea 解決 Could not autowire. No beans of 'xxxx' type found 的錯(cuò)誤提示,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01SpringMVC如何正確接收時(shí)間的請(qǐng)求示例分析
這篇文章主要為大家介紹了SpringMVC如何正確接收時(shí)間的請(qǐng)求示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09java集成開(kāi)發(fā)SpringBoot生成接口文檔示例實(shí)現(xiàn)
這篇文章主要為大家介紹了java集成開(kāi)發(fā)SpringBoot如何生成接口文檔的示例實(shí)現(xiàn)過(guò)程,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-10-10Mybatis + js 實(shí)現(xiàn)下拉列表二級(jí)聯(lián)動(dòng)效果
這篇文章給大家介紹基于Mybatis + js 實(shí)現(xiàn)下拉列表二級(jí)聯(lián)動(dòng)效果,實(shí)現(xiàn)代碼分為前端界面實(shí)現(xiàn)和后端處理方法,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-06-06