话不多说 , : :
Excelutil.java
Package pers.kangxu.datautils.utils; import java.io.file; import java.io.fileInputStream; import java.io.fileOutStream; import java.io.ioexception; import java.util.hashmap; java.util.list; importer 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; 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.cellrangaDress; / ** * <b> * Excelutil {/ ** * 导出 Excel * @param filepath 文件全路径 * @param sheetname sheet 页名称 * @param sheetIndex 当前 feuille 下表 从 0 开始 * @param fileheader 头部 * @param datas 内容 * / public static writeExcel (String filepath, string sheetname, inthewxer, string [] fileadder, list [string []> data) wb = nouveau hssfworkbook (); // 创建工作表 feuille de feuille s = wb.createSheet (); wb.setSheetName (sheetIndex, sheetname); Row r = S.CreaterOW (0); Cellule 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.borter_thin); // 下边框 StyleHeader.SetBorderLeft (hssfcellStyle.border_thin); // 左边框 StyleHeader.SetBorderTop (hssfcellStyle.border_thin); // 上边框 StyleHeader.SetborderRight (hssfcellStyle.border_thin); // 右边框 // 设置内容样式 StyleContent = WB.CreateCellStyle (); StyleContent.SetBorderBottom (hssfcellStyle.border_thin); // 下边框 StyleContent.SetBorderLeft (hssfcellStyle.border_thin); // 左边框 StyleContent.SetBorderTop (hssfcellStyle.border_thin); // 上边框 StyleContent.SetborderRight (hssfcrellSyle.fer_thin); // 右边框 // pour (int pour (int pour (int pour (int pour (int pour (int pour (int pour (int pour (int pour (int pour (int pour (int pour (int pour (int pour (int pour (int pour (int pour (int pour (int pour (int pour (int pour (int pour (int pour (int pour (int pour (int pour (int pour (int pour (int pour (int pour (int pour (int pour (int pour (int pour (int pour (int pour (int pour (int pour (int pour (int pour (int pour (. i = 0; i <fileHeader.length;) {c = r.createCell (i); C.SetCellStyle (StyleHeader); C.SetCellValue (FileHeader [i]); i ++; } // 设置内容 pour (int rownum = 0; rownum <dataS.size ();) {// 行 row datas.size () r = s.createrow (rownum + 1); // 创建行 pour (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 (nouveau fichier (filepath))) {out = new FileOutputStream (filePath); WB.Write (Out); }} catch (exception e) {e.printStackTrace (); } enfin {try {// 关闭流 if (out! = null) {out.flush (); out.close (); }} catch (ioException e) {e.printStackTrace (); }}} / ** * 读取 Excel 文件内容 * @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); // 建立输入流 classeur wb = null; wb = new HSSFWorkBook (entrée); // 获取 feuille 页 feuille de feuille = wb.getSheetAt (sheetIndex); Iterator <row> lignes = sheet.rowiterator (); while (rows.hasnext ()) {row row = wows.next (); Iterator <Colle> Cell = Row.Celliterator (); Map <string, string> map = new hashmap <string, string> (); if (cnt == 0) {// 将头放进 list 中 while (cells.hasnext ()) {cell cell = cells.next (); if (isContainMerGell (feuille)) {CancelMergell (feuille); } list.add (getStringCellValue (Cell)); } cnt ++; continuer; } else {while (cells.hasnext ()) {Cell Cell = Cells.Next (); if (isContainMerGell (feuille)) {CancelMergell (feuille); } // 区别相同的头 list = listUtil.ChangeSameVal (list); map.put (list.get (idx ++), getStringCellValue (cellule)); }} idx = 0; maplist.add (map); } return maplist; } catch (ioException ex) {ex.printStackTrace (); } return null; } / ** * 合并单元格 * @Param Sheet 当前 Sheet 页 * @param firstrow 开始行 * @param lastrow 结束行 * @param firstcol 开始列 * @param lastcol 结束列 * / public static int megecell (feuille de feuille, int Firstrow, int lastrow, reprend -1; } return sheet.addmergegion (new CellRangeAddress (Firstrow, Lastrow, FirstCol, LastCol)); } / ** * 取消合并单元格 * @Param Sheet * @param idx * / public static void annulemerCell (feuille de feuille) {int shegegeCount = sheet.getNummerGeDregions (); pour (int idx = 0; idx <sheetmergeCount;) {cellRangeAddress range = sheet.getMergeGeGion (idx); String Val = getMergellValue (sheet, range.getFirStrow (), range.getLastrow ()); // 取消合并单元格 sheet.removemergedRegion (idx); for (int rownum = range.getFirStrow (); rownum <range.getLastrow () + 1;) {for (int CellNum = range.getFirstColumn (); Cellnum <range.getlastColumn () + 1;) {sheet.getRow (Rownum) .getCell (Cellnum) .SetCellValue (Val); Cellnum ++; } Rownum ++; } idx ++; }} / ** * 判断指定单元格是否是合并单元格 * @param feuille 当前 feuille 页 * @param firstrow 开始行 * @param lastrow 结束行 * @param firstcol 开始列 * @param lastcol 结束列 * @return * / public static boolean isMergell (feuille de feuille, introw, int colonne) {inthegeCount = sheet.getNatherged Sheet, introw (); pour (int i = 0; i <sheetmerCount;) {CellRangeAddress range = sheet.getMergeGeGion (i); int premiersColumn = range.getFirstColumn (); int lastColumn = range.getlastColumn (); int FirStrow = range.getFirStrow (); int lastrow = range.getLastrow (); if (row> = firStrow && row <= lastrow) {if (colonnel> = firstColumn && colonnes <= lastColumn) {return true; }} i ++; } return false; } / ** * 判断 feuille 页中是否含有合并单元格 * @param sheet * @return * / public static boolean isContainMergell (feuille de feuille) {if (sheet == null) {return false; } return sheet.getNummergedRegions ()> 0? vrai: false; } / ** * 获取指定合并单元的值 * @Param Sheet * @param Row * @param colonne * @return * / public static String getMercellValue (feuille de feuille, ligne int, colonne int) {int sheetmergeCount = sheet.getNummergedRegions (); pour (int i = 0; i <sheetmerCount;) {CellRangeAddress range = sheet.getMergeGeGion (i); int premiersColumn = range.getFirstColumn (); int lastColumn = range.getlastColumn (); int FirStrow = range.getFirStrow (); int lastrow = range.getLastrow (); if (row> = firStrow && row <= lastrow) {if (colonnel> = firstColumn && colonnes <= lastColumn) {row frow = sheet.getRow (firstrow); Cell fCell = frow.getCell (FirstColumn); return getStringCellValue (fCell); }} i ++; } return null; } / ** * 获取单元格的值 * @param cellule * @return * / public static static getStringCellValue (Cell Cell) {String strCell = ""; if (cell == null) return strCell; switch (Cell.getCellType ()) {case Cell.Cell_type_string: strCell = cell.getRichStringCellValue (). GetString (). Trim (); casser; case Cell.Cell_Type_Numeric: StrCell = String.ValueOf (Cell.GetNumericCellValue ()); casser; case Cell.Cell_Type_Boolean: strCell = String.Valueof (Cell.getBoolEanCellValue ()); casser; case Cell.Cell_Type_Formula: FormulaEvaluator Evaluator = Cell.getSheet (). GetWorkBook (). GetCreationHelper (). CreateFormulaEvaluator (); Evaluator.EvaluateFormulacell (cellule); CellValue CellValue = Evaluator.Evaluate (Cell); strCell = string.valueof (cellValue.getNumberValue ()); casser; par défaut: strCell = ""; } return strCell; }}调用方式如下
Exceluilteter.java
package pers.kangxu.datautils.test; import java.util.arraylist; import java.util.list; import pers.kangxu.datautils.utils.excelutil; public class excelutilteter {public static void main (string [] args) {list <string []> datas = new ArrayList <String [String []> (); datas.add (new String [] {"狗熊", "母", "250"}); datas.add (new String [] {"猪粮", "不明", "251"}); //Excelutil.writeExcel ("c://users//administrator//desktop//test//test//test.xls" ,"sheet1",0, nouvelle chaîne [] {"姓名", "年龄", "性别"}, datas); System.out.println (excelutil.readExcel ("c: //users//administrator//desktop//test//test//test.xls", 0)); }}以上就是本文的全部内容 , 希望本文的内容对大家的学习或者工作能带来一定的帮助 , 同时也希望多多支持武林网!