package com.util;java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
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;
import org.springframework.util.StringUtils;spring
/**
* 讀取上傳Excel數據
* @author
*
*/
public class ReadPlansInfoExcel {
static XSSFRow row;
@SuppressWarnings({ "deprecation" })
public ArrayList<Data_excel> Read(String username,File file) throws Exception{
XSSFWorkbook workbook = null;
FileInputStream fis = null;
ArrayList<Data_excel> arrlist=new ArrayList<Data_excel>();
Date date = new Date();
Timestamp timestamp = new Timestamp(date.getTime());
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss " );
SimpleDateFormat sd = new SimpleDateFormat( "yyyy-MM-dd" );
try {
fis = new FileInputStream(file);
try {
workbook = new XSSFWorkbook(fis);
XSSFSheet spreadsheet = workbook.getSheetAt(0);
Iterator < Row > rowIterator = spreadsheet.iterator();
int totalRowNum = spreadsheet.getLastRowNum()+1;//獲得總行數
int totalListNum=spreadsheet.getRow(1).getPhysicalNumberOfCells();//獲得總列數
Data_excel infos =null;
for (int i = 0 ; i < totalRowNum ; i++) {
infos = new Data_excel();
row = (XSSFRow) rowIterator.next();
//Iterator < Cell > cellIterator = row.cellIterator();
XSSFRow xssfRow = spreadsheet.getRow(i);
if(i > 0){//第一行不讀取列,第一行是名稱
for (int j = 0 ; j < totalListNum ; j++)
{
// infos.setCreateTime(timestamp.toString());//建立時間
// infos.setCreateUser(username);//獲取登錄狀態的用戶名
// infos.setLogTime(sd.format(timestamp));//插入時間
XSSFCell cell = xssfRow.getCell(j);
if(cell ==null){
continue;
}
//讀取每一列的屬性值,cell.getStringCellValue()
//Cell cell = cellIterator.next();
String value = new String();
switch (cell.getCellType())
{
case Cell.CELL_TYPE_NUMERIC://獲取整型的值
DecimalFormat df = new DecimalFormat("0");
value = df.format(cell.getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING: //字符串類型的值
value = cell.getStringCellValue().trim();
break;
case Cell.CELL_TYPE_BLANK: //空值
// value=null;
value="";
break;
case Cell.CELL_TYPE_BOOLEAN:
value= String.valueOf(cell.getBooleanCellValue()).trim();
}
if(j ==0){sql
infos.setCustomer(value);//(Integer.parseInt(value.trim()));// 生產順序
}else if(j==1){
infos.setCode(value); //編碼
System.out.println(infos);
}else if(j ==2 ){
infos.setFName(value); //設備編號
}else if(j==3){
infos.setFitemid(Integer.parseInt(value.trim()));//生產日期
}
}
// System.out.println("customer:"+infos.getCustomer());
boolean cus=infos.getCustomer()!=null && !infos.getCustomer().equals("");
boolean co =infos.getCode()!=null && !infos.getCode().equals("");
if(cus || co){
arrlist.add(infos);
}
}
}
fis.close();
} catch (IOException e) {
e.printStackTrace();
}apache
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
return arrlist;
}
} xss