话不多说 , 请看代码 :
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; org.apache.poi.ss.usmodel.cell; import org.apache.poi.s.usmodel.cellstyle; import org.apache.poi.s.usmodel.cellvalue; import org.apache.poi.s.usmodel.font; org.apache.poi.ss.usermodel.row; import org.apache.poi.ss.usermodel.sheet; import org.apache.poi.ss.usermodel.workbook; ExcelUtil { /** * 导出 excel * @param filePath 文件全路径 * @param sheetName sheet页名称 * @param sheetIndex 当前sheet下表 从0开始 * @param fileHeader 头部 * @param datas 内容 */ public static void writeExcel(String filePath,String sheetName, int sheetIndex, String[] fileHeader, List<String[]> datas){ // 创建工作簿 Workbook wb = new HSSFWorkbook (); // 创建工作表 Blatt S = wb.CreateSheet (); wb.setSheetName (SheetIndex, SheetName); Reihe r = S. Createrow (0); Zelle C = NULL; Schriftart = null; CellStyle styleHeader = null; CellStyle stylecontent = null; // 粗体 font = wb.CreateIneT (); font.setBoldWeight (hssffont.boldgewicht_bold); // 设置头样式 styleHeader = wb.createcellstyle (); styleHeader.setfont (Schriftart); styleHeader.setBorderBottom (HSSFCellStyle.border_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 (hssfcellstyle.Border_thin); // 右边框 // 设置头SfcellStyle.Border_thin); // 右边框 // ldtyle.Border_thin); // 右边框 // llstyle.Border_thin); // 右边框 // llstyle.Border_thin); // 右边框 // llstyle.krorder_thin); // 右边框 // 设置头sfcellStyle.Border_thin); // 右边框 // 设置头 设置头 设置头. i = 0; i <FileHeader.length;) {c = R.CreateCell (i); C.SetCellStyle (StyleHeader); C.SetCellValue (Dateiheader [i]); i ++; } // 设置内容 für (int Rownum = 0; Rownum <datas.size ();) {// 行 Zeilendaten.SIZE () r = S.Createrow (Rownum+1); // 创建行 für (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 (neue Datei (filepath)) {out = new FileOutputStream (filepath); wb.write (out); }} catch (Ausnahme e) {e.printstacktrace (); } endlich {try {// 关闭流 if (out! = null) {out.flush (); out.close (); }} catch (ioException e) {e.printstacktrace (); }}} / ** * 读取 excel 文件内容 * @param filepath * @param SheetIndex * / public staticlist <map <String, String >> ReadExcel (String filePath, int SheetIndex) {list <map <String, String >> maplist = new ArrayList <map <string >> (); // 头 list <string> list = new ArrayList <string> (); // int cnt = 0; int idx = 0; try {inputStream input = new FileInputStream (filepath); // 建立输入流 Arbeitsbuch WB = NULL; WB = New HSSFWorkbook (Eingabe); // 获取 Blatt 页 Blatt = wb.getsheetat (SheetIndex); Iterator <row> rows = Shew.Rowiterator (); while (rows.hasnext ()) {row row = rows.next (); Iterator <cell> cells = row.celliterator (); Karte <String, String> map = new HashMap <String, String> (); if (cnt == 0) {// 将头放进 list 中 while (cells.hasnext ()) {cell cell = cells.Next (); if (isContainMergeCell (Blatt)) {CancelMergeCell (Blatt); } list.add (GetStringCellValue (Zelle)); } cnt ++; weitermachen; } else {while (cells.hasnext ()) {cell Cell = Cells.Next (); if (isContainMergeCell (Blatt)) {CancelMergeCell (Blatt); } // 区别相同的头 list = listutil.changesameval (list); map.put (list.get (idx ++), getStringCellValue (Zelle)); }} idx = 0; mapList.add (map); } return mapList; } catch (ioException ex) {ex.printstacktrace (); } return null; } / ** * 合并单元格 * @param Blatt 当前 Blatt 页 * @param firstrow 开始行 * @param lastrow 结束行 * @param firstcol 开始列 * @param lastcol 结束列 * / public static int mergecell (Blattblatt, int Firstrow, int lastrow, int Firstcol, int lastcol) {fall (fall) {if ({fall) {fall ({fall) (falls (null) {fall ({fall) {fall ({fall) {wenn (fall (null) {{if ((null)) {{return -1; } return Sheet.AddmergedRegion (New CellRangeaddress (Firstrow, Lastrow, FirstCol, LastCol)); } / ** * 取消合并单元格 * @param Sheet * @param idx * / public static void CancelMergeCell (Blattblatt) {int SheetmerGeCount = Sheet.getNummergedRegions (); für (int idx = 0; idx <SheetmerGeCount;) {CellRangeaddress -Bereich = Sheet.GetMerged Region (idx); String val = getMergeCellValue (Blatt, Bereich.getFirstrow (), Bereich.getLastrow ()); // 取消合并单元格 Sheet.Removemergedregion (IDX); für (int rownum = range cellnum ++; } Rownum ++; } idx ++; } } /** * 判断指定单元格是否是合并单元格 * @param sheet 当前sheet页 * @param firstRow 开始行 * @param lastRow 结束行 * @param firstCol 开始列 * @param lastCol 结束列 * @return */ public static boolean isMergeCell(Sheet sheet, int row ,int column){ int sheetMergeCount = sheet.getNumMergedRegions(); für (int i = 0; i <SheetmerGeCount;) {cellRangeaddress -Bereich = Sheet.GetmergedRegion (i); int firstColumn = Bereich.getFirstcolumn (); int lastcolumn = range.getLastcolumn (); int firstrow = Bereich.getFirStrow (); int lastrow = Bereich.getLastrow (); if (row> = firstrow && row <= lastrow) {if (column> = firstColumn && Spalte <= lastColumn) {return true; }} i ++; } return false; } / ** * 判断 Blatt 页中是否含有合并单元格 * @Param Blatt * @return * / public static boolean isContainMergeCell (Blattblatt) {if (Sheet == null) {return false; } return Sheet.getNummergedRegions ()> 0? wahr: falsch; } / ** * 获取指定合并单元的值 * @param Sheet * @param row * @param column * @return * / public static String getMergeCellValue (Blattblatt, int row, int column) {int SheetmerGeCount = Sheet.getNummergedRegions (); für (int i = 0; i <SheetmerGeCount;) {cellRangeaddress -Bereich = Sheet.GetmergedRegion (i); int firstColumn = Bereich.getFirstcolumn (); int lastcolumn = range.getLastcolumn (); int firstrow = Bereich.getFirStrow (); int lastrow = Bereich.getLastrow (); if (row> = firstrow && row <= lastrow) {if (column> = firstColumn && Spalte <= lastColumn) {row frow = Sheet.getRow (firstrow); Cell fcell = frow.getCell (FirstColumn); return getStringcellValue (fcell); }} i ++; } return null; } / ** * 获取单元格的值 * @param cell * @return * / public static String getStringCellValue (Zellenzelle) {String strcell = ""; if (cell == null) return strcell; switch (cell.getCellType ()) {case cell.cell_type_string: strcell = cell.getRichstringCellValue (). getString (). trim (); brechen; case cell.cell_type_numeric: strcell = string.valueof (cell.getNumericcellValue ()); brechen; case cell.cell_type_boolean: strcell = string.valueof (cell.getBooleancellValue ()); brechen; case cell.cell_type_formula: formulaevaluator evaluator = cell.getsheet (). getworkbook (). getCreationHelper (). createFormulaevaluator (); Evaluator.Valuateformulacell (Zelle); CellValue CellValue = Evaluator.Valuate (Cell); Strcell = String.ValueOf (cellValue.getNumberValue ()); brechen; Standard: strcell = ""; } return Strcell; }}调用方式如下
Excelutilterester.java
Paket pers.kangxu.datautils.test; import Java.util.ArrayList; import Java.util.list; import Pers.kangxu.Datautils.utils.Excelutil; öffentliche Klasse excelutilterester {public static void main (String [] args) {list <]> >>> >> >>>> NEW NEWARLIST <]; datas.add (neuer String [] {"狗熊", "母", "250"}); datas.add (neuer String [] {"猪粮", "不明", "251"}); // System.out.println (excelutil.readexcel ("c: //users//administrator//desktop//test//test//test.xls", 0)); }}以上就是本文的全部内容 , 希望本文的内容对大家的学习或者工作能带来一定的帮助 , 同时也希望多多支持武林网! 同时也希望多多支持武林网!