话不多说 话不多说, 请看代码 :
Excelutil.java
packing 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; java.util.hashmap; java.util.hashmap; 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; org.apache.poi.ss.usermodel.cell; import org.apache.poi.ss.usermodel.cellstyle; import org.apache.poi.ss.usermodel.cellvalue; import org.apache.poi.ss.usermodel.font; import org.apache.poi.ss.sormulle eleermulelel org.apache.poi.ss.usermodel.row; import org.apache.poi.ss.usermodel.sheet; import org.apache.poi.ss.usermodel.workbook; import org.apache.poi.ss.util.cellrangeaddress;/** * <b> * * </bogxo */bogxo * */by kangor classe * <b> * <b> * Excelutil { / ** * 导出 actel * @param filepath 文件全路径 * @param sheetname sheet 页名称 * @param sheetindex 当前 시트 下表 从 从 0 开始 * @param 파일 헤더 头部 * @param datas 内容 * / public static void writeexcel (문자열 파일, 문자열 시트name, int sheetindex, 문자열 [] <string [] {) <] WB = 새로운 hssfworkbook (); // 创建工作表 시트 시트 s = wb.createsheet (); wb.setsheetname (sheetindex, sheetname); 행 R = S.Createrow (0); 셀 c = null; 글꼴 font = null; CellStyle StyleHeader = NULL; 셀 스타일 스타일 컨텐츠 = 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); // 左边框 左边框 styleHeader.setBorderTop (hssfcellstyle.border_thin); // 上边框 上边框 styleHeader.setBorderright (hssfcellstyle.border_thin); StyleContent.setborderbottom (hssfcellstyle.border_thin); // stylecontent.setborderleft (hssfcellstyle.border_thin); // 左边框 左边框 左边框 左边框 左边框 左边框 左边框 左边框 左边框 左边框 左边框 左边框 左边框 左边框 左边框 左边框 左边框 左边框 左边框 左边框 左边框 stylecontent.setbordertop (hssfcellstyle.border_thin); // 上边框 StyleContent.setborderright (hssfcellstyle.border_thin); // 右边框 // (int i = 0; i <fileheader.length;) {c = r.createcell (i); C.SetCellStyle (StyleHeader); C.SetCellValue (FileHeader [i]); i ++; } // 设置内容 for (int rownum = 0; rownum <datas.size ();) {// 行 row datas.size () r = s.createrow (rownum+1); // (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 (새 파일 (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 (); }}}} / ** * 读取 accel 文件内容 * @param filepath * @param sheetindex * / public static list <map <string, string >> readexcel (String filepath, int sheetindex) {list <map <string, string >> maplist = new arraylist <map <string, String >> (); // 头 list <string> list = new ArrayList <string> (); // int cnt = 0; int idx = 0; try {inputStream input = new FileInputStream (FilePath); // 建立输入流 통합 문서 wb = null; WB = 새로운 hssfworkbook (입력); // 获取 시트 获取 시트 시트 = wb.getSheetat (sheetIndex); iterator <줄> 행 = 시트 .rowiterator (); while (rows.hasnext ()) {row row = rows.next (); 반복자 <cell> cells = row.celliterator (); map <string, string> map = new Hashmap <String, String> (); if (cnt == 0) {// 将头放进 list 中 while (cells.hasnext ()) {cell cell = cells.next (); if (iscontainmergecell (시트)) {cancelmergecell (시트); } list.add (getStringCellValue (Cell)); } cnt ++; 계속하다; } else {while (cells.hasnext ()) {cell cell = cells.next (); if (iscontainmergecell (시트)) {cancelmergecell (시트); } // list = listUtil.ChangesameVal (list); map.put (list.get (idx ++), getStringCellValue (Cell)); }} idx = 0; maplist.add (지도); } 리턴 메이플리스트; } catch (ioexception ex) {ex.printstacktrace (); } return null; } / ** * * * @param sheet 当前 当前 시트 页 * @param firstrow 开始行 * @param lastrow 结束行 * @param firstcol 开始列 * @param lastcol 结束列 * / public static int mergecell (시트 시트, int firstrow, int lastrow, int firstcol, int lastcol) {if (sheet == null) {return) {if == null); } return sheet.Addmerged Region (New CellRangeAddress (Firstrow, Lastrow, FirstCol, LastCol)); } / ** * * * @param sheet * @param idx * / public static void cancelMergecell (시트 시트) {int shetmergecount = sheet.getNummerged Regions (); for (int idx = 0; idx <shertmergecount;) {CellRangeAddress 범위 = 시트. String val = getMergeCellValue (시트, range.getFirstrow (), range.getLastrow ()); // 取消合并单元格 시트. removeMerged Region (IDX); for (int rownum = range.getfirstrow (); rownum <Rownum <Rownum <range.getLastrow ()+1;) {for (int cellnum = range.getFirstColumn (); cellNum <Range.getLastColumn ()+1;) {sheet.getrow (Rownum) .getCell (val). Cellnum ++; } rownum ++; } idx ++; }} / ** * 判断指定单元格是否是合并单元格 * @param sheet 当前 시트 页 * @param firstrow 开始行 * @param lastrow 结束行 * @param firstcol 开始列 * @param lastcol 结束列 * @return * / public static boolean ismergecell (시트, int row, int column) {int sheethmergecount = sheet.getnummmergedreger (); for (int i = 0; i <shetmergecount;) {CellRangeAddress 범위 = 시트 .getmerged Region (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 sheet * @return * / public static boolean iscontainmergecell (시트 시트) {if (sheet == null) {return false; } return sheet.getNummerged Regreations ()> 0? 사실 : 거짓; } / ** * * * @param sheet * @param row * @param 열 * @return * / public static string getmergecellValue (시트 시트, int 열) {int shetmergecount = sheet.getNummergedRegions (); for (int i = 0; i <shetmergecount;) {CellRangeAddress 범위 = 시트 .getmerged Region (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); cell fcell = frow.getCell (FirstColumn); return getStringCellValue (fcell); }} i ++; } return null; } / ** * * * @param cell * @return * / public static string getstringCellValue (셀 셀) {String strcell = ""; if (cell == null) 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 (). create formulaEvaluator (); Evaluator.evaluateformulacell (셀); CellValue CellValue = Evaluator.evaluate (Cell); strcell = string.valueof (cellValue.getNumberValue ()); 부서지다; 기본값 : strcell = ""; } return strcell; }}调用方式如下
Excelutiltester.java
packing pers.kangxu.datautils.test; import java.util.arraylist; import java.util.list; import pers.kangxu.datautils.utils.excelutil; public static void main (String [] arg []]> dataS = new Araylist <string []> (); datas.add (new String [] { "狗熊", "母", "250"}); datas.add (new String [] { "猪粮", "不明", "251"}); //excelutil.writeexcel("c://users//administrator//desktop//test//test//test.xls",“{ "姓名", "年龄", "性别"}, Datas); System.out.println (Excelutil.readexcel ( "c : //users//administrator//desktop//test//test//test.xls", 0)); }}以上就是本文的全部内容 以上就是本文的全部内容, 希望本文的内容对大家的学习或者工作能带来一定的帮助, 同时也希望多多支持武林网!