话不多说、请看代码:
Excelutil.java
パッケージ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.util.util.util.util.util.util.util.util.util.util.util.utilmap; 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.hsfork org.apache.poi.ss.usermodel.cell; Import org.apache.poi.ss.usermodel.cellstyle; Import org.apache.poi.ss.s.usermodel.cellvalue; import org.apache.poi.ss.s.usermodel.font; org.apache.poi.ss.usermodel.row; Import org.apache.poi.ss.usermodel.sheet; Import org.apache.poi.ss.s.usermodel.workbook; Import org.apache.poi.ss.util.cellrangeaddress; excelutil { / ** *导出excel * @param filepath wb = new hssfworkbook(); //创建工作表シートシートs = wb.createsheet(); wb.setsheetName(SheetIndex、SheetName);行r = S.Createrow(0);セルC = null; font font = null; CellStyle StyleHeader = null; CellStyle StyleContent = null; //粗体font= wb.createfont(); font.setboldweight(hssffont.boldweight_bold); //设置头样式styleheader = wb.createcellstyle(); StyleHeader.setFont(font); styleheader.setborderbottom(hssfcellstyle.border_thin); // styleheader.setborderleft(hssfcellstyle.border_thin); // wb.createCellStyle(); stylecontent.setborderbottom(hssfcellstyle.border_thin); //下边框stylecontent.setborderleft(hssfcellstyle.border_thin); // i = 0; i <fileheader.length;){c = r.createcell(i); C.SetCellStyle(StyleHeader); C.SetCellValue(FileHeader [i]); i ++; } // for(int rownum = 0; rownum <datas.size();){// // 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; try {//创建文件或者文件夹、将内容写进去if(fileutil.createfile(new file(filepath))){out = new fileoutputStream(filepath); wb.write(out); }} catch(例外e){e.printstacktrace(); }最後に{try {//关闭流if(out!= null){out.flush(); out.close(); }} catch(ioexception e){e.printstacktrace(); }}} / ** *读取excel文件内容 * @param filepath * @param sheetindex * / public static list <map <map <string、string >> readexcel(string filepath、int sheetindex){list <map <string、string >> maplist = new arraylist <map <map <string、string >>(); //头リスト<文字列>リスト= new ArrayList <String>(); // int cnt = 0; int idx = 0; try {inputstream input = new FileInputStream(filepath); //建立输入流ワークブックwb = null; wb = new hssfworkbook(input); //获取シート页シートシート= wb.getsheetat(sheetindex); iterator <Row> rows = Sheet.Rowiterator(); while(rows.hasnext()){row row = rows.next(); iterator <cell> cell = row.celliterator(); map <string、string> map = new hashmap <string、string>(); if(cnt == 0){//将头放进リストwhile(cells.hasnext()){cell cell = cells.next(); if(iscontainmergecell(sheet)){cancelmergecell(sheet); } list.add(getStringCellValue(cell)); } cnt ++;続く; } else {while(cells.hasnext()){cell cell = cells.next(); if(iscontainmergecell(sheet)){cancelmergecell(sheet); } //区别相同的头list= listutil.changesameval(list); map.put(list.get(idx ++)、getStringCellValue(cell)); }} idx = 0; maplist.add(map); } maplistを返します。 } catch(ioException ex){ex.printstacktrace(); } nullを返します。 } / ** *合并单元格 * @paramシート当前シート页 * @param firstow开始行 * @param lastrow结束行 * @param firstCol } return sheet.addmergedregion(new cellrangeaddress(Firstow、Lastrow、FirstCol、lastCol)); } / ** *取消合并单元格 * @param Sheet * @param idx * / public static void cancelmergecell(シートシート){int sheetmergecount = sheet.getnummergedregions(); for(int idx = 0; idx <sheetmergecount;){cellrangeaddress range = sheet.getMergedRegion(idx); string val = getMergeCellValue(sheet、range.getfirstrow()、range.getlastrow()); // for(int rownum = range.getfirstrow(); rownum <range.getlastrow()+1;){for(int cellnum = range.getFirstColumn(); cellnum <range.getLastColumn()+1; Cellnum ++; } rownum ++; } idx ++; }} / ** *判断指定单元格是否是合并单元格 * @paramシート当前シート页 * @param firstow开始行 * @param lastrow结束行 * @param firstcol开始列 * @param lastcol结束列 * @return * / public static boolean ismergecell(シートシート、int row、int列) for(int i = 0; i <sheetmergecount;){cellrangeaddress range = sheet.getMergedRegion(i); int firstColumn = range.getFirstColumn(); int lastcolumn = range.getLastColumn(); int firstow = range.getFirstrow(); int lastrow = range.getlastrow(); if(row> = firstow && 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 column * @return * / public static string getMergeCellValue(シートシート、int row、int column){int sheetmergecount = sheet.getnummergedregions(); for(int i = 0; i <sheetmergecount;){cellrangeaddress range = sheet.getMergedRegion(i); int firstColumn = range.getFirstColumn(); int lastcolumn = range.getLastColumn(); int firstow = range.getFirstrow(); int lastrow = range.getlastrow(); if(row> = firstow && row <= lastrow){if(column> = firstColumn && column <= lastColumn){row frow = sheet.getrow(firstrow); cell fcell = frow.getCell(FirstColumn); return getStringCellValue(fcell); }} i ++; } nullを返します。 } / ** *获取单元格的值 * @param cell * @return * / public static string getStringCellValue(Cell Cell){string strcell = ""; if(cell == null)return strcell; switch(cell.getCellType()){case cell.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 evaluator = cell.getSheet()。getWorkBook()。getCreationHelper()。createFormulaEvaluator(); Evaluator.EvaluateFormulacell(セル); CellValue CellValue = evaluator.Evaluate(Cell); strcell = string.valueof(cellvalue.getnumbervalue());壊す;デフォルト:strcell = ""; } strcellを返します。 }}调用方式如下
excelutiltester.java
パッケージpers.kangxu.datautils.test; Import java.util.arraylist; import java.util.list; import pers.kangxu.datautils.utils.excelutil; public static void main(string [] args){list <<string []> datas = new Arrist < datas.add(new String [] {"狗熊"、 "母"、 "250"}); datas.add(new String [] {"猪粮"、 "不明"、 "251"}); //excelutil.writeexcel("c://users//administrator//desktop//test//test//test.xls"、 "sheet1",0、new String [] {"姓名 "、"年龄 "、"性别 "}、データsystem.out.println(excelutil.readexcel( "c://users//administrator//desktop//test//test//test.xls"、0)); }}以上就是本文的全部内容、希望本文的内容对大家的学习或者工作能带来一定的帮助、同时也希望多多支持武林网!