作者:冷漠自逍遥2602897565 | 来源:互联网 | 2023-08-26 01:41
版本问题如果提示:classnotfoundexcepiont:orgapachepoiss.usermodel.Workbook需要下载的包dependencies{com
版本问题
如果提示:
classnotfoundexcepiont:org/apache/poi/ss.usermodel.Workbook
需要下载的包
dependencies {compile "org.apache.poi:poi:3.9"compile "org.apache.poi:poi-excelant:3.9"compile "org.apache.poi:poi-examples:3.9"compile "org.apache.poi:poi-ooxml:3.9"compile "org.apache.poi:poi-scratchpad:3.9"compile "org.apache.poi:poi-ooxml-schemas:3.9"compile "commons-io:commons-io:2.4"compile "net.sourceforge.jexcelapi:jxl:2.6.12"compile "dom4j:dom4j:1.6.1"
}
示例代码:
package web;import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFHyperlink;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.util.CellRangeAddress;import java.io.*;
import java.util.*;public class ExcelUtil{public void fileInput() throws IOException {XSSFWorkbook hw = new XSSFWorkbook(new FileInputStream( "./poi.xls"));XSSFSheet hsheet = hw.getSheet("poi test");XSSFRow hrow = hsheet.getRow(0);XSSFCell hcell = hrow.getCell(0);String cellValue = this.getCellValue(hcell);System.out.println(cellValue);}public static String getCellValue(XSSFCell cell) {String value = null;if (cell != null) {switch (cell.getCellType()) {case XSSFCell.CELL_TYPE_FORMULA:try {value = String.valueOf(cell.getNumericCellValue());} catch (IllegalStateException e) {value = String.valueOf(cell.getRichStringCellValue());}break;case XSSFCell.CELL_TYPE_NUMERIC:value = String.valueOf(cell.getNumericCellValue());break;case XSSFCell.CELL_TYPE_STRING:value = String.valueOf(cell.getRichStringCellValue());break;case XSSFCell.CELL_TYPE_BOOLEAN:value = String.valueOf(cell.getBooleanCellValue());break;}}return value;}public static void main(String[] args)throws Exception {XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream("./a.xlsx")); XSSFSheet hsheet = workbook.getSheetAt(0);Iterator rows = hsheet.rowIterator();while(rows.hasNext()){XSSFRow hrow = (XSSFRow)rows.next();System.out.println("ROW:"+hrow.getRowNum());Iterator cells = hrow.cellIterator();while(cells.hasNext()){XSSFCell hcell = (XSSFCell)cells.next();String value = getCellValue(hcell);System.out.println(value);}}System.out.println(" createworkbook.xlsx written successfully");}
}