: : : :
excelutil.java
package pers.kangxu.datautils.Utils ؛ import java.io java.util.list ؛ استيراد java.util.map ؛ استيراد org.apache.poi.hssf.usermodel.hssfcellstyle ؛ استيراد org.apache.poi.hssf.usermodel.hssffont ؛ import org.apache.poi.hssf.usermodel.hssbook org.apache.poi.ss.usermodel.cell ؛ استيراد org.apache.poi.ss.usermodel.cellstyle ؛ استيراد org.apache.poi.ss.usermodel.cellvalue ؛ import org.apache.poi.ss.usermodel.font ؛ org.apache.poi.ss.usermodel.row ؛ استيراد org.apache.poi.ss.usermodel.sheet ؛ استيراد org.apache.poi.ss.usermodel.workbook ؛ import org.apache.poi.ss.util.cellrangeaddress ؛ excelutil { / ** * 导出 excel * param filepath 文件全路径 * param sheetname sheet 页名称 * param sheetIndex 当前 sheet 下表 从 0 开始 * @param fileHeader 头部 * @param datas 内容 * / public static void writeexcel (String filepath ، string sheetn. WB = جديد HSSFWorkBook () ؛ // 创建工作表 sheet sheet s = wb.createshet () ؛ WB.SetSheetName (sheetIndex ، sheetName) ؛ الصف r = s.creachrow (0) ؛ الخلية C = فارغة ؛ خط الخط = فارغ ؛ CellStyle StyleHeader = NULL ؛ CellStyle StyleContent = null ؛ // 粗体 font = wb.createfont () ؛ font.setBoldweight (hssffont.boldweight_bold) ؛ // 设置头样式 styleHeader = wb.createcellstyle () ؛ StyleHeader.setFont (الخط) ؛ StyleHeader.setBorderBottom (HSSfCellStyle.border_thin) ؛ // 下边框 styleHeader.setBorderLeft (HSSFCellStyle.border_thin) ؛ // 左边框 styleHeader.setBorderTop (hssfcellstyle.border_thin) ؛ // 上边框 styleHeader.setBorderRight (hssfcellstyle.border_thin) ؛ WB.CreateCellStyle () ؛ StyleContent.setBorderBottom (HSSFCellStyle.border_thin) ؛ // 下边框 stylecontent.setborderleft (hssfcellstyle.border_thin) ؛ // 左边框 styleContent.setBorderTop (hssfcellstyle.border_thin) ؛ // 上边框 stylecont.setbordright (hssfcellstyle.border_thin) ؛ // 右边框 for i = 0 ؛ i <fileDheader.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.creatrow (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 (ملف جديد (filePath)))) {Out = newOutputStream (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 * / قائمة ثابتة عامة <map <string ، string >> readexcel (String filepath ، int sheetIndex) {list <map ، string >> maplist = new ArrayList <map ، string >> () ؛ // 头 list <String> list = new ArrayList <Tring> () ؛ // int cnt = 0 ؛ int idx = 0 ؛ حاول {inputStream input = new FileInputStream (filePath) ؛ // 建立输入流 Workbook wb = null ؛ WB = جديد HSSFWorkbook (إدخال) ؛ // 获取 sheet 页 sheet sheet = wb.getsheetat (sheetIndex) ؛ iterator <row> صفوف = sheet.rowiterator () ؛ بينما (rows.hasnext ()) {row row = rows.next () ؛ ITERATAR <LED> cells = row.celliterator () ؛ خريطة <string ، string> map = new hashmap <string ، string> () ؛ if (cnt == 0) {// 将头放进 list 中 بينما (cells.hasNext ()) {cell cell = cells.next () ؛ if (isContainmerGecell (sheet)) {cancelmergecell (sheet) ؛ } list.add (getTringCellValue (cell)) ؛ } cnt ++ ؛ يكمل؛ } آخر {بينما (cells.hasnext ()) {cell cell = cells.next () ؛ if (isContainmerGecell (sheet)) {cancelmergecell (sheet) ؛ } // 区别相同的头 list = listUtil.ChangesameVal (list) ؛ map.put (list.get (idx ++) ، getTringCellValue (cell)) ؛ }} idx = 0 ؛ MapList.Add (MAP) ؛ } return maplist ؛ } catch (ioException ex) {ex.printStackTrace () ؛ } إرجاع فارغ ؛ } / ** * 合并单元格 * param sheet 当前 sheet 页 * param firstrow 开始行 * param lastrow 结束行 * param firstcol 开始列 * param lastcol 结束列 * / public static int mergecell (ورقة ورقة ، int firstrow ، int lastrow ، intcol ، int lastcol) } return sheet.addmergedregion (cellRangeadDress جديد (Firstrow ، Lastrow ، FirstCol ، LastCol)) ؛ } / ** * 取消合并单元格 * param sheet * param idx * / public static void cancelmergeCell (ورقة ورقة) {int shetmergeCount = sheet.getNummergedReactregions () ؛ لـ (int idx = 0 ؛ idx <sheetmergeCount ؛) {cellRangeadDress range = sheet.getmergedregion (idx) ؛ String val = getMergeCellValue (sheet.getfirstrow () ، range.getlastrow ()) ؛ // 取消合并单元格 sheet.removemergedregion (IDX) ؛ لـ (int rownum = range.getfirstrow () ؛ rownum <range.getlastrow ()+1 ؛) {for (int cellnum = range.getFirstColumn () ؛ cellnum <raze.getlastcolumn ()+1 ؛) {sheet.get.getRow (rownum) .getcell (cellnum) cellnum ++ ؛ } rownum ++ ؛ } idx ++ ؛ }} / ** * 判断指定单元格是否是合并单元格 * param sheet 当前 sheet 页 * param firstrow 开始行 * param lastrow 结束行 * param firstcol 开始列 * param lastCol 结束列 * @return * / public static boolean ismergecell (ورقة ورقة ، int row ، العمود int sheatmergeCount = لـ (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 ++ ؛ } إرجاع خطأ ؛ } / ** * 判断 sheet 页中是否含有合并单元格 * param sheet * return * / public static boolean isContainmergeCell (sheet sheet) {if (sheet == null) {return false ؛ } sheet.getNummergedReagmions ()> 0؟ صحيح: خطأ } / ** * 获取指定合并单元的值 * param sheet * param row * param column * return * / public static string getmergeCellValue (ورقة ، int row ، عمود int) {int skinmeCount = sheet.getNummergedReachs () ؛ لـ (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) ؛ العودة getTringCellValue (fcell) ؛ }} i ++ ؛ } إرجاع فارغ ؛ } / *: إذا (الخلية == فارغة) إرجاع 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 (). المقيِّم. cellvalue cellValue = evaluator.evaluate (cell) ؛ strcell = string.valueof (cellValue.getNumberValue ()) ؛ استراحة؛ الافتراضي: strcell = "" ؛ } إرجاع strcell ؛ }}调用方式如下
excelutiltester.java
package pers.kangxu.datautils.test ؛ استيراد java.util.arraylist ؛ استيراد java.util.list ؛ استيراد pers.kangxu.datautils.utils.excelutil ؛ الطبقة العامة excelutiltester {public static void main (string) {list <s string [] datas.add (سلسلة جديدة [] {"狗熊" ، "母" ، "250"}) ؛ datas.add (سلسلة جديدة [] {"猪粮" ، "不明" ، "251"}) ؛ //excelutil.writeExcel("c://users//administrator//desktop//test//test//test.xls"،"sheet1"،0 ، string new [] {"姓名" ، "年龄" ، "性别"} ، datas) ؛ system.out.println (excelutil.readexcel ("c: //users//administrator//desktop//test//test//test.xls" ، 0)) ؛ }}以上就是本文的全部内容 , 希望本文的内容对大家的学习或者工作能带来一定的帮助 , 同时也希望多多支持武林网!