话不多说,请看代码::
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)); }}}以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持武林网!,同时也希望多多支持武林网!