poi import and export

Reference:
--The role of each Jar package of POIhttp :
//blog.csdn.net/szwangdf/article/details/39053859
https://www.cnblogs.com/wangqc/p/poi.html
https://www. cnblogs.com/zhangchengbing/p/6340036.html

-- Import and export principle or code reference
http://blog.csdn.net/houxuehan/article/details/50960259
http://blog.csdn.net/u013871100/article/ details/52901996
http://wanghongxu.iteye.com/blog/1879893
http://yl-fighting.iteye.com/blog/1726285
https://www.cnblogs.com/stono/p/6713158.html
http: //blog.csdn.net/johnstrive/article/details/8393541
https://www.cnblogs.com/zhuixun/p/6600331.html
http://blog.csdn.net/aqsunkai/article/details/52270198
- -POI general export Excel (.xls,.xlsx)
http://blog.csdn.net/houxuehan/article/details/50960259
--java, poi cancel scientific notationhttp
:
//blog.csdn.net/this_super/article/details/6766139 --POI reads excel cells cell, get the value of each type of cell, and return the string type
https://www.cnblogs.com/zy2009/p/6759933.html
--POI's operation on EXCEL [Key: How to set the CELL format to text format]
http: //blog.csdn.net/frist_huangsuli/article/details/7701446


demo
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import 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.NumberToTextConverter;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * poi import and export excel file tool class
 */
public class PoiUtils {
	/**
	 * 1. The jar poi component that needs to be imported--file format--jar HSSF Excel XLS poi HSSF is the POI project to Excel
	 * 97(-2007) Pure Java implementation of file operations XSSF Excel XLSX poi-ooxml XSSF is POI project for Excel 2007
	 * Pure Java implementation of OOXML (.xlsx) file operations Common SS Excel XLS and XLSX poi-ooxml
	 * 2. Issues to consider Excel file format: xls and xlsx excel file number of lines: small number, large number
	 */
	private static final Log logger = LogFactory.getLog(PoiUtils.class);
	
	public static void main(String[] args) {
	File file=new File("D:\\test data 222.xlsx");
	FileInputStream fileInputStream=null;
	try {
		fileInputStream=new FileInputStream(file);
	} catch (FileNotFoundException e) {
		e.printStackTrace ();
	}
	readExcelFileDta2List(fileInputStream, ".xlsx", 1);
		
	}

	
	/**
	 * Get the cell value as a string
	 * @param cell
	 * @return
	 */
	private static String getCellStringValue(Cell cell) {
		/**
		 * CellType type value CELL_TYPE_NUMERIC Numeric 0 CELL_TYPE_STRING String 1
		 * CELL_TYPE_FORMULA formula type 2 CELL_TYPE_BLANK null value 3 CELL_TYPE_BOOLEAN boolean type 4
		 * CELL_TYPE_ERROR error 5
		 */
		if (null == cell) {
			return "";
		}
		String result = "";
		switch (cell.getCellType()) {
		case Cell.CELL_TYPE_STRING:// String类型
			result = cell.getStringCellValue();
			break;
		case Cell.CELL_TYPE_NUMERIC:// numeric type
			result = NumberToTextConverter.toText(cell.getNumericCellValue());
			break;
		case Cell.CELL_TYPE_BOOLEAN:// boolean
			result = String.valueOf(cell.getBooleanCellValue());
			break;
		case Cell.CELL_TYPE_FORMULA:// 公式
			result = cell.getCellFormula();
			break;
		case Cell.CELL_TYPE_BLANK:// 空格
			result = "";
			break;
		default: // 其它 CELL_TYPE_ERROR
			result = "";
			break;
		}
		return result;
	}

	/**
	 * Parse the file stream of imported data
	 *
	 * @param inputStream
	 * @param suffix like ".xls" or ".xlsx"
	 * @param startRowIndex
	 * @return
	 */
	public static List<String[]> readExcelFileDta2List(InputStream inputStream, String suffix, int startRowIndex) {
		Workbook workbook = null;
		if (ExcelCommonEnum.SUFFIX_XLS.getValue().equals(suffix)) {
			// The suffix of the excel file name is ".xls"
			try {
				// HSSF is a pure Java implementation of POI project's Excel 97 (-2007) file operations
				workbook = new HSSFWorkbook(inputStream);
			} catch (IOException e) {
				e.printStackTrace ();
			}
		} else if (ExcelCommonEnum.SUFFIX_XLSX.getValue().equals(suffix)) {
			// The suffix of the excel file name is ".xlsx"
			try {
				// XSSF is a pure Java implementation of POI project for Excel 2007 OOXML (.xlsx) file operations
				workbook = new XSSFWorkbook(inputStream);
			} catch (IOException e) {
				e.printStackTrace ();
			}
		}
		Sheet sheet = workbook.getSheetAt(0);
		if (sheet == null) {
			return null;
		}
		ArrayList<String[]> list = new ArrayList<String[]>();
		int lastRowNum = sheet.getLastRowNum(); // maximum number of rows
		Row titleRow = sheet.getRow(0);//Title row, used to define the length of String[]
		for (int rowNum = startRowIndex; rowNum <= lastRowNum; rowNum++) {
			if (null != sheet.getRow(rowNum)) {
				Row row = sheet.getRow(rowNum);// Take the rowNum row of the current traversed row
				short firstCellNum = row.getFirstCellNum();// 0
				short lastCellNum = row.getLastCellNum();// The maximum number of columns in the current row
				if (firstCellNum != lastCellNum) {
					// If the number of columns is not 0, that is, the sheet is not empty
					String[] values ​​= new String[titleRow.getLastCellNum()];// Define a string array whose length is the maximum number of columns in the current row
					for (int cellNum = firstCellNum; cellNum < titleRow.getLastCellNum(); cellNum++) {
						Cell cell = row.getCell(cellNum);
						if (null == cell) {
							values[cellNum] = ExcelCommonEnum.NULL.getValue();
						} else {
							String cellValue=getCellStringValue(cell);// Get the value of the cell
							values[cellNum] = StringUtils.isNotBlank(cellValue)?cellValue.trim():ExcelCommonEnum.NULL.getValue();
						}
					}
					list.add(values);
				}
			}
		}
		return list;
	}
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326563090&siteId=291194637