java怎么读取excel数据

Python08

java怎么读取excel数据,第1张

引入poi的jar包,大致如下:

读取代码如下,应该能看得明白吧

import java.io.FileInputStream

import java.io.FileNotFoundException

import java.io.IOException

import java.io.InputStream

import java.math.BigDecimal

import java.text.DecimalFormat

import java.text.SimpleDateFormat

import org.apache.poi.xssf.usermodel.XSSFCell

import org.apache.poi.xssf.usermodel.XSSFRow

import org.apache.poi.xssf.usermodel.XSSFSheet

import org.apache.poi.xssf.usermodel.XSSFWorkbook

public class ExcelUtil2007 {

/**读取excel文件流的指定索引的sheet

 * @param inputStream excel文件流

 * @param sheetIndex 要读取的sheet的索引

 * @return

 * @throws FileNotFoundException

 * @throws IOException

 */

public static XSSFSheet readExcel(InputStream inputStream,int sheetIndex) throws FileNotFoundException, IOException

{

return readExcel(inputStream).getSheetAt(sheetIndex)

}

/**读取excel文件的指定索引的sheet

 * @param filePath excel文件路径

 * @param sheetIndex 要读取的sheet的索引

 * @return

 * @throws IOException 

 * @throws FileNotFoundException 

 */

public static XSSFSheet readExcel(String filePath,int sheetIndex) throws FileNotFoundException, IOException

{

return readExcel(filePath).getSheetAt(sheetIndex)

}

/**读取excel文件的指定索引的sheet

 * @param filePath excel文件路径

 * @param sheetName 要读取的sheet的名称

 * @return

 * @throws IOException 

 * @throws FileNotFoundException 

 */

public static XSSFSheet readExcel(String filePath,String sheetName) throws FileNotFoundException, IOException

{

return readExcel(filePath).getSheet(sheetName)

}

/**读取excel文件,返回XSSFWorkbook对象

 * @param filePath excel文件路径

 * @return 

 * @throws FileNotFoundException

 * @throws IOException

 */

public static XSSFWorkbook readExcel(String filePath) throws FileNotFoundException, IOException

{

XSSFWorkbook wb=new XSSFWorkbook(new FileInputStream(filePath))

return wb

}

/**读取excel文件流,返回XSSFWorkbook对象

 * @param inputStream excel文件流

 * @return

 * @throws FileNotFoundException

 * @throws IOException

 */

public static XSSFWorkbook readExcel(InputStream inputStream) throws FileNotFoundException, IOException

{

XSSFWorkbook wb=new XSSFWorkbook(inputStream)

return wb

}

/***读取excel中指定的单元格,并返回字符串形式的值

 * 1.数字

 * 2.字符

 * 3.公式(返回的为公式内容,非单元格的值)

 * 4.空

 * @param st 要读取的sheet对象

 * @param rowIndex 行索引

 * @param colIndex 列索引

 * @param isDate 是否要取的是日期(是则返回yyyy-MM-dd格式的字符串)

 * @return

 */

public static String getCellString(XSSFSheet st,int rowIndex,int colIndex,boolean isDate){

String s=""

XSSFRow row=st.getRow(rowIndex)

if(row == null) return ""

XSSFCell cell=row.getCell(colIndex)

if(cell == null) return ""

if (cell.getCellType() == 0) {//数字

if(isDate)s=new SimpleDateFormat("yyyy-MM-dd").format(cell.getDateCellValue())

else s = trimPointO(String.valueOf(getStringValue(cell)).trim())

}else if (cell.getCellType() == 1){//字符(excel中的空格,不是全角,也不是半角,不知道是神马,反正就是" "这个)

s=cell.getRichStringCellValue().getString().replaceAll(" ", " ").trim()

// s=cell.getStringCellValue()//07API新增,好像跟上一句一致

}

else if (cell.getCellType() == 2){//公式

s=cell.getCellFormula()

}

else if (cell.getCellType() == 3){//空

s=""

}

return s

}

/**如果数字以 .0 结尾,则去掉.0

 * @param s

 * @return

 */

public static String trimPointO(String s) {

if (s.endsWith(".0"))

return s.substring(0, s.length() - 2)

else

return s

}

/**处理科学计数法和百分比模式的数字单元格

 * @param cell

 * @return

 */

public static String getStringValue(XSSFCell cell) {

String sValue = null

short dataFormat = cell.getCellStyle().getDataFormat()

double d = cell.getNumericCellValue()

BigDecimal b = new BigDecimal(Double.toString(d))

//百分比样式的

if (dataFormat == 0xa || dataFormat == 9) {

b=b.multiply(new BigDecimal(100))

//String temp=b.toPlainString()

DecimalFormat df=new DecimalFormat("0.00")//保留两位小数的百分比格式

sValue = df.format(b) + "%"

}else{

sValue = b.toPlainString()

}

return sValue

}

}

初始化表格时,类比一下这个

table.setModel(new DefaultTableModel(new Object[][]{{4,2,3},{null,null,null}},new String[]{"第一列","第二列","第三列"}))