話不多說,請看代碼::
excelutil.java
package pers.kangxu.datautils.utils;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.usermodel.HSSFFont;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.cell; import org.apache.poi.ss.s.usermodel.cellstyle; import org.apache.poi.ss.s.s.s.s.sermodel.cellvalue; import org.apache.apache.poi.poi.poi.ss.s.usermodel.font; import ofimpore ofimpore offort; import ofimpore offer..pare.pare.apache.apcace; org.apache.poi.ss.usermodel.row; import org.apache.poi.ss.s.usermodel.sheet; import org.apache.poi.ss.usermodel.workbook; import org.apache.poi.poi.poi.ss.ss.util.cellrangeaddress; cellrangeaddress; excelutil { / ** *導出excel * @param filepath文件全路徑 * @param sheetname egne * * @param sheetIndex當前表當前當前下表下表下表下表下表下表下表下表下表 * @param fileheader頭部wb = new HSSFWorkbook(); //創建工作表表s = wb.createsheet(); wb.setSheetName(SheetIndex,SheetName);行r = s.Createrow(0);細胞C = null;字體字體= null; CellStyle styleheader = null; CellStyle stylecontent = null; //粗體font = wb.createfont(); font.setBoldWeight(hssfont.boldweight_bold); //設置頭樣式styleHeader = wb.createcellstyle(); styleheader.setfont(font); styleheader.setborderbottom(hssfcellstyle.border_thin); //下邊框styleborderLeft(hssfcellstyle.border_thin); //左邊框styleborder.setbordertop(hssfcellstyle.border_thin); // styletheader.setheader.setborder.setborder(hssfcellstyle.border.border.border.border.border.border.border.border.border) wb.createcellstyle(); StyleContent.SetBorderBottom(hssfcellstyle.border_thin); //下邊框STYLECONTENT.SETBORDERLEFT(HSSFCELLSTYLE.BORDER_THIN); //左邊框STYLECONTENT.SETBORDERTOP(HSSFCELLSTYLE.BORDER.BORDER_THIN); //- i = 0; i <fileheader.length;){c = r.CreateCell(i); C.SetCellstyle(StyleHeader); C.SetCellValue(fileheader [i]); i ++; } //設置內容for(int ronnum = 0; rownum <datas.size();){//行行datas.size()r = s.createrow(Rownum+1); //創建行for(int cellnum = 0; cellnum <fileheader.length;){c = r.CreateCell(cellnum); C.setCellvalue(datas.get(Rownum)[CellNum]); C.setCellstyle(StyleContent); Cellnum ++; } Rownum ++; } fileOutputStream out = null;嘗試{//創建文件或者文件夾,將內容寫進去if(fileutil.createfile(new file(filepath))){out = new fileOutputstream(filepath); wb.write(OUT); }} catch(異常e){e.printstacktrace(); }最後{嘗試{//關閉流if(out!= null){out.flush(); out.close(); }} catch(ioexception e){e.printstacktrace(); }}}} / ** *讀取excel文件內容 * @param filepath * @param sheetIndex * / public static list <map <string,string,string >> readexcel(string filepath,int sheet index){list <map <string,string >> maplist> maplist = maplist = new arraylist = new arraylist <new arraylist <map <map <map <string <string> strig> strig> strint>> strint>>>() //頭list<string> list = new ArrayList <string>(); // int cnt = 0; int idx = 0;嘗試{InputStream Input = new FileInputStream(filepath); //建立輸入流工作簿WB = null; WB =新的HSSFWorkBook(輸入); //獲取表頁紙= wb.getSheetat(SheetIndex);迭代<row> rows = sheet.rowiterator(); while(rows.hasnext()){行row = rows.next();迭代器<cell> cells = row.celliterator(); MAP <String,String> map = new Hashmap <String,String>(); if(cnt == 0){//將頭放進列表中while while(cells.hasnext()){cell cell = cells.next();如果(iscontainmergecell(Sheet)){cancelmergecell(ewne); } list.add(getStringCellvalue(cell)); } cnt ++;繼續; } else {while(cells.hasnext()){cell = cells.next();如果(iscontainmergecell(Sheet)){cancelmergecell(ewne); } // list = listutil.changesameval(list); map.put(list.get(idx ++),getStringCellvalue(cell)); }} idx = 0; maplist.add(地圖); }返回映射; } catch(ioexception ex){ex.printstacktrace(); }返回null; } / ** *合併單元格 * @param表格當前} return sheet.addmergedRegion(new CellRangeadDress(firstrow,lastrow,firstcol,lastcol)); } / ** *取消合併單元格 * @param表 * @param IDX * / public static void cancelmergecell(表格){int SheetMergeCount = ewn.getNummergedRegions(); for(int idx = 0; idx <sheetMergeCount;){cellRangeadDress range = Sheet.getMergedRegion(idx);字符串val = getMergeCellValue(Sheet,range.getFirstrow(),range.getLastrow()); //取消合併單元格Sheet.RemoveMergedRegion(IDX); for(int rownum = range.getFirstrow(); rownum <range.getLastrow()+1;){for(int cellnum = range.get.getFirstColumn(); cellnum <range.get.getlastcolumn(getLastColumn() Cellnum ++; } Rownum ++; } idx ++; }} / ** *判斷指定單元格是否是合併單元格 * @param表格當前for(int i = 0; i <sheetmergeCount;){cellRangeadDress range = Sheet.getMergedRegion(i); int firstColumn = range.getFirstColumn(); int lastColumn = range.getLastColumn(); int firstrow = range.getFirstrow(); int lastrow = range.getLastrow(); if(row> = firstrow && row <= lastrow){if(column> = firstColumn && column <= lastColumn){return true; }} i ++; }返回false; } / ** *判斷板頁中是否含有合併單元格 * @param表 * @return * / public static boolean iscontainmergecell(表格){if(sheet == null){return false; } return sheet.getNummergedRegions()> 0? true:false; } / ** *獲取指定合併單元的值 * @param sheet * @param row * @param列 * @return * / public static String getMergeCellValue(表格,int int row,int low colun){int SheetMergeCount = eghingnummergedRegions(); for(int i = 0; i <sheetmergeCount;){cellRangeadDress range = Sheet.getMergedRegion(i); int firstColumn = range.getFirstColumn(); int lastColumn = range.getLastColumn(); int firstrow = range.getFirstrow(); int lastrow = range.getLastrow(); if(row> = firstrow && row <= lastrow){if(column> = firstColumn && column <= lastColumn){row frow = Sheet.getRow(firstrow);細胞fcell = frow.getCell(firstColumn);返回get getTringCellValue(fcell); }} i ++; }返回null; } / ** *獲取單元格的值 * @param cell * @return * / public static String getStringCellValue(cell){字符串strcell =“”; if(cell == null)返回strcell; switch(cell.getCellType()){case.cell_type_string:strcell = cell.getRichStringCellValue()。getString()。trim();休息; case cell.cell_type_numeric:strcell = string.valueof(cell.getNumericCellValue());休息; case cell.cell_type_boolean:strcell = string.valueof(cell.getBooleanCellvalue());休息; case cell.cell_type_formula:formulaeValuator評估器= cell.getSheet()。getWorkbook()。getCreationHelper()。createformulaevaluator();評估師。 cellValue cellValue =評估器。 strcell = string.valueof(cellValue.getNumberValue());休息;默認值:strcell =“”; }返回strcell; }}}調用方式如下
Excelutiltester.java
package pers.kangxu.datautils.test;import java.util.ArrayList;import java.util.List;import pers.kangxu.datautils.utils.ExcelUtil;public class ExcelUtilTester { public static void main(String[] args) { List<String[]> datas = new ArrayList<String[]>(); datas.add(new String [] {“狗熊”,“母”,“ 250”}); datas.add(new String [] {“豬糧”,“不明”,“ 251”}); //EXCELUTIL.WRITEEXCEL(“c://users//administrator//desktop//test//test//test/test.xls",”,“,”,“sheet1",0,new String [] {] {“姓名”,“”,“年齡”,“年齡”,“性別”},datas),datas); system.out.println(excelutil.readexcel(“ c://users//administrator//desktop//test/test/test/test/test/test.xls ”,0)); }}}以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持武林網! ,同時也希望多多支持武林網!