java表格更新javadb_Java解析excel表格(新)
第一步:引入相關依賴
org.apache.poi
poi-ooxml
4.1.2
第二步:工具類
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ImportExcel
{
/** 總行數 */
private int totalRows = 0;
/** 總列數 */
private int totalCells = 0;
/** 錯誤信息 */
private String errorInfo;
public int getTotalRows()
{
return totalRows;
}
public void setTotalRows(int totalRows)
{
this.totalRows = totalRows;
}
public int getTotalCells()
{
return totalCells;
}
public void setTotalCells(int totalCells)
{
this.totalCells = totalCells;
}
public String getErrorInfo()
{
return errorInfo;
}
public void setErrorInfo(String errorInfo)
{
this.errorInfo = errorInfo;
}
/** 構造方法 */
public ImportExcel()
{
}
/**
*
* @描述:是否是2003的excel,返回true是2003
*
* @參數:@param filePath 文件完整路徑
*
* @參數:@return
*
* @返回值:boolean
*/
public static boolean isExcel2003(String filePath)
{
return filePath.matches("^.+\\.(?i)(xls)$");
}
/**
*
* @描述:是否是2007的excel,返回true是2007
*
* @參數:@param filePath 文件完整路徑
*
* @參數:@return
*
* @返回值:boolean
*/
public static boolean isExcel2007(String filePath)
{
return filePath.matches("^.+\\.(?i)(xlsx)$");
}
public static boolean isCSV(String filePath)
{
return filePath.matches("^.+\\.(?i)(csv)$");
}
public static boolean isTxt(String filePath)
{
return filePath.matches("^.+\\.(?i)(txt)$");
}
/**
*
* @描述:驗證excel文件
*
* @參數:@param filePath 文件完整路徑
*
* @參數:@return
*
* @返回值:boolean
*/
public boolean validateExcel(String filePath)
{
/** 檢查文件名是否為空或者是否是Excel格式的文件 */
if (filePath == null || !(ImportExcel.isExcel2003(filePath) || ImportExcel.isExcel2007(filePath) ))
{
errorInfo = "文件名不是excel格式";
return false;
}
return true;
}
public List read(String filePath, Class cla, Integer sheetNo,Integer startRow)
{
try
{
if( startRow == null){
startRow = 1;
}
List listObjects = new ArrayList();
List> list = read(filePath,sheetNo, startRow);
Constructor[] constructors = cla.getDeclaredConstructors();
Constructor> constructor = null;
for (Constructor> item : constructors)
{
if (item.getParameterTypes().length == 1)//獲取一個參數的構造方法
{
constructor = item;
}
}
if (list != null)
{
for (int i = 0; i < list.size(); i++)
{
Object p = cla.newInstance();
List cellList = list.get(i);
p = constructor.newInstance(cellList);
listObjects.add(p);
}
}
return listObjects;
}
catch (InstantiationException e)
{
e.printStackTrace();
errorInfo = "解析excel數據出現錯誤";
return null;
}
catch (IllegalArgumentException e)
{
e.printStackTrace();
errorInfo = "解析excel數據出現錯誤";
return null;
}
catch (IllegalAccessException e)
{
e.printStackTrace();
errorInfo = "解析excel數據出現錯誤";
return null;
}
catch (InvocationTargetException e)
{
e.printStackTrace();
errorInfo = "excel文件模版錯誤";
return null;
}
catch (SecurityException e)
{
e.printStackTrace();
errorInfo = "解析excel數據出現錯誤";
return null;
}
}
/**
*
* @描述:根據文件名讀取excel文件
*
* @參數:@param filePath 文件完整路徑
*
* @參數:@return
*
* @返回值:List
*/
public List> read(String filePath, Integer sheetNo,Integer startRow)
{
List> dataLst = new ArrayList>();
InputStream is = null;
try
{
/** 驗證文件是否合法 */
if (!validateExcel(filePath))
{
System.out.println(errorInfo);
return null;
}
/** 判斷文件的類型,是2003還是2007 */
boolean isExcel2003 = true;
if (ImportExcel.isExcel2007(filePath))
{
isExcel2003 = false;
}
/** 調用本類提供的根據流讀取的方法 */
File file = new File(filePath);
is = new FileInputStream(file);
dataLst = read(is, isExcel2003, sheetNo, startRow);
is.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
if (is != null)
{
try
{
is.close();
}
catch (IOException e)
{
is = null;
e.printStackTrace();
}
}
}
/** 返回最后讀取的結果 */
return dataLst;
}
/**
*
* @描述:根據流讀取Excel文件
*
* @參數:@param inputStream
*
* @參數:@param isExcel2003
*
* @參數:@return
*
* @返回值:List
*/
public List> read(InputStream inputStream, boolean isExcel2003, Integer sheetNo,Integer startRow)
{
List> dataLst = null;
try
{
/** 根據版本選擇創建Workbook的方式 */
Workbook wb = null;
if (isExcel2003)
{
wb = new HSSFWorkbook(inputStream);
}
else
{
wb = new XSSFWorkbook(inputStream);
}
dataLst = read(wb, sheetNo, startRow);
}
catch (IOException e)
{
e.printStackTrace();
}
return dataLst;
}
/**
*
* @描述:讀取數據
*
* @參數:@param Workbook
*
* @參數:@param sheetNo
*
* @參數:@return
*
* @返回值:List>
*/
private List> read(Workbook wb, Integer sheetNo,Integer startRow)
{
List> dataLst = new ArrayList>();
/** 得到第一個shell */
Sheet sheet = null;
if(sheetNo == null){
sheet = wb.getSheetAt(0);
}else{
sheet = wb.getSheetAt(sheetNo.intValue());
}
/** 得到Excel的行數 */
this.totalRows = sheet.getPhysicalNumberOfRows();
/** 得到Excel的列數 */
if (this.totalRows >= 1 && sheet.getRow(startRow) != null)
{
this.totalCells = sheet.getRow(startRow).getPhysicalNumberOfCells();
}
/** 循環Excel的行 */
for (int r = startRow; r < this.totalRows; r++)
{
Row row = sheet.getRow(r);
if (row == null)
{
continue;
}
List rowLst = new ArrayList();
/** 循環Excel的列 */
for (int c = 0; c < this.getTotalCells(); c++)
{
Cell cell = row.getCell(c);
String cellValue = "";
if (null != cell)
{
// 以下是判斷數據的類型
switch (cell.getCellType())
{
case NUMERIC: // 數字
cellValue = String.valueOf(cell.getNumericCellValue());
break;
case STRING: // 字符串
cellValue = cell.getStringCellValue();
break;
case BOOLEAN: // Boolean
cellValue = cell.getBooleanCellValue() + "";
break;
case FORMULA: // 公式
cellValue = cell.getCellFormula() + "";
break;
case BLANK: // 空值
cellValue = "";
break;
case ERROR: // 故障
cellValue = "非法字符";
break;
default:
cellValue = "未知類型";
break;
}
}
rowLst.add(cellValue);
}
/** 保存第r行的第c列 */
dataLst.add(rowLst);
}
return dataLst;
}
}
第三步:運行
/**
*
* @描述:main測試方法
*
* @參數:@param args
*
* @參數:@throws Exception
*
* @返回值:void
*/
public static void main(String[] args) throws Exception
{
ImportExcel poi = new ImportExcel();
List> list = poi.read("/Users/zhuangjy/Desktop/test.xlsx",1,0);
if (list != null) {
for (int i = 0; i < list.size(); i++) {
System.out.print("第" + (i) + "行");
List cellList = list.get(i);
for (int j = 0; j < cellList.size(); j++) {
System.out.print(" " + cellList.get(j));
}
System.out.println();
}
}
}
效果截圖
總結
以上是生活随笔為你收集整理的java表格更新javadb_Java解析excel表格(新)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 经验分享band 7.5已经工作的人如何
- 下一篇: shell中冒号 : 用途说明