1. 程式人生 > >在Javaweb中poi實現資料匯入,支援03版和07版Excel匯入

在Javaweb中poi實現資料匯入,支援03版和07版Excel匯入

注意資料型別的轉換,另外由於在後面的sid我不需要插入資料庫,所以最後就沒有set到實體物件

哪些不明白可以直問!

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Workbook;
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 com.ky.jlj_test.bo.Jlj_testStudent;


public class ImportExcel {
String sid = null;
String sname = null;
String sex = null;
String birthday = null;
String address = null;
String tel = null;
String classno = null;
List<Jlj_testStudent> stuList = new ArrayList<Jlj_testStudent>();
Workbook workbook = null;
int k = 0;
int flag = 0; // 指示指標所訪問的位置


@SuppressWarnings("deprecation")
public List<Jlj_testStudent> importExcel(File file) throws IOException,
ParseException {
/*
* 2007版的讀取方法
*/


if (file != null) {
String path = file.getAbsolutePath();
System.out.println("檔案上傳的路徑:" + path);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/M/d");// 日期轉換


try {
workbook = new XSSFWorkbook(path);// 初始化workbook物件
for (int numSheets = 0; numSheets < workbook
.getNumberOfSheets(); numSheets++) { // 讀取每一個sheet
System.out
.println("============2007版進入讀取sheet的迴圈===========");


if (null != workbook.getSheetAt(numSheets)) {
XSSFSheet aSheet = (XSSFSheet) workbook
.getSheetAt(numSheets);// 定義Sheet物件


for (int rowNumOfSheet = 1; rowNumOfSheet <= aSheet
.getLastRowNum(); rowNumOfSheet++) { // 跳過第一行欄位,一般為中文解釋詞


// 進入當前sheet的行的迴圈
if (null != aSheet.getRow(rowNumOfSheet)) {
XSSFRow aRow = aSheet.getRow(rowNumOfSheet); // 定義行,並賦值


for (int cellNumOfRow = 0; cellNumOfRow <= aRow
.getLastCellNum(); cellNumOfRow++) { // 讀取rowNumOfSheet值所對應行的資料
XSSFCell xCell = aRow.getCell(cellNumOfRow); // 獲得行的列數
// //獲得列值


// System.out.println("type="+xCell.getCellType());
if (null != aRow.getCell(cellNumOfRow)) {
if (xCell.getCellType() == XSSFCell.CELL_TYPE_STRING) { // 為字串型


if (cellNumOfRow == 0) {
aRow.getCell(0).setCellType(
Cell.CELL_TYPE_STRING);
sid = xCell
.getStringCellValue();
if (sid == null) {
System.out
.println("錯誤:在Sheet"
+ (numSheets + 1)
+ "中的第"
+ (rowNumOfSheet + 1)
+ "行的第"
+ (cellNumOfRow + 1)
+ "列的學號不能為空");
}
} else if (cellNumOfRow == 1) {
aRow.getCell(1).setCellType(
Cell.CELL_TYPE_STRING);
sname = xCell
.getStringCellValue();
if (sname == null) {
System.out
.println("錯誤:在Sheet"
+ (numSheets + 1)
+ "中的第"
+ (rowNumOfSheet + 1)
+ "行的第"
+ (cellNumOfRow + 1)
+ "列的姓名不能為空");
}
} else if (cellNumOfRow == 2) {
aRow.getCell(2).setCellType(
Cell.CELL_TYPE_STRING);
sex = xCell
.getStringCellValue();
if (sex == null) {
System.out
.println("錯誤:在Sheet"
+ (numSheets + 1)
+ "中的第"
+ (rowNumOfSheet + 1)
+ "行的第"
+ (cellNumOfRow + 1)
+ "列的性別不能為空");
}
} else if (cellNumOfRow == 3) {
Date date = HSSFDateUtil
.getJavaDate(xCell
.getNumericCellValue());
birthday = sdf.format(date);
if (birthday == null) {
System.out
.println("錯誤:在Sheet"
+ (numSheets + 1)
+ "中的第"
+ (rowNumOfSheet + 1)
+ "行的第"
+ (cellNumOfRow + 1)
+ "列的出生日期不能為空");
}
} else if (cellNumOfRow == 4) {
aRow.getCell(4).setCellType(
Cell.CELL_TYPE_STRING);
address = xCell
.getStringCellValue();
if (address == null) {
System.out
.println("錯誤:在Sheet"
+ (numSheets + 1)
+ "中的第"
+ (rowNumOfSheet + 1)
+ "行的第"
+ (cellNumOfRow + 1)
+ "列的住址不能為空");
}
} else if (cellNumOfRow == 5) {
aRow.getCell(5).setCellType(
Cell.CELL_TYPE_STRING);
tel = xCell
.getStringCellValue();
if (tel == null) {
System.out
.println("錯誤:在Sheet"
+ (numSheets + 1)
+ "中的第"
+ (rowNumOfSheet + 1)
+ "行的第"
+ (cellNumOfRow + 1)
+ "列的聯絡方式不能為空");
}
} else if (cellNumOfRow == 6) {
aRow.getCell(6).setCellType(
Cell.CELL_TYPE_STRING);
classno = xCell
.getStringCellValue();
if (classno == null) {
System.out
.println("錯誤:在Sheet"
+ (numSheets + 1)
+ "中的第"
+ (rowNumOfSheet + 1)
+ "行的第"
+ (cellNumOfRow + 1)
+ "列的班級不能為空");
}
}
}
if (xCell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) { // 為數值型
if (cellNumOfRow == 0) {
aRow.getCell(0).setCellType(
Cell.CELL_TYPE_STRING);
sid = xCell
.getStringCellValue()
+ "";
if (sid == null) {
System.out
.println("錯誤:在Sheet"
+ (numSheets + 1)
+ "中的第"
+ (rowNumOfSheet + 1)
+ "行的第"
+ (cellNumOfRow + 1)
+ "列的學號不能為空");
}
} else if (cellNumOfRow == 1) {
aRow.getCell(1).setCellType(
Cell.CELL_TYPE_STRING);
sname = String.valueOf(xCell
.getStringCellValue());
if (sname == null) {
System.out
.println("錯誤:在Sheet"
+ (numSheets + 1)
+ "中的第"
+ (rowNumOfSheet + 1)
+ "行的第"
+ (cellNumOfRow + 1)
+ "列的姓名不能為空");
}
} else if (cellNumOfRow == 2) {
aRow.getCell(2).setCellType(
Cell.CELL_TYPE_STRING);
sex = String.valueOf(xCell
.getStringCellValue());
if (sex == null) {
System.out
.println("錯誤:在Sheet"
+ (numSheets + 1)
+ "中的第"
+ (rowNumOfSheet + 1)
+ "行的第"
+ (cellNumOfRow + 1)
+ "列的性別不能為空");
}
} else if (cellNumOfRow == 3) {
Date date = HSSFDateUtil
.getJavaDate(xCell
.getNumericCellValue());
birthday = sdf.format(date);
if (birthday == null) {
System.out
.println("錯誤:在Sheet"
+ (numSheets + 1)
+ "中的第"
+ (rowNumOfSheet + 1)
+ "行的第"
+ (cellNumOfRow + 1)
+ "列的出生日期不能為空");
}
} else if (cellNumOfRow == 4) {
aRow.getCell(4).setCellType(
Cell.CELL_TYPE_STRING);
address = String.valueOf(xCell
.getStringCellValue());
if (address == null) {
System.out
.println("錯誤:在Sheet"
+ (numSheets + 1)
+ "中的第"
+ (rowNumOfSheet + 1)
+ "行的第"
+ (cellNumOfRow + 1)
+ "列的住址不能為空");
}
} else if (cellNumOfRow == 5) {
aRow.getCell(5).setCellType(
Cell.CELL_TYPE_STRING);
tel = String.valueOf(xCell
.getStringCellValue());
if (tel == null) {
System.out
.println("錯誤:在Sheet"
+ (numSheets + 1)
+ "中的第"
+ (rowNumOfSheet + 1)
+ "行的第"
+ (cellNumOfRow + 1)
+ "列的聯絡方式不能為空");
}
} else if (cellNumOfRow == 6) {
aRow.getCell(6).setCellType(
Cell.CELL_TYPE_STRING);
classno = String.valueOf(xCell
.getStringCellValue());
if (classno == null) {
System.out
.println("錯誤:在Sheet"
+ (numSheets + 1)
+ "中的第"
+ (rowNumOfSheet + 1)
+ "行的第"
+ (cellNumOfRow + 1)
+ "列的班級不能為空");
}
}
} else if (xCell.getCellType() == XSSFCell.CELL_TYPE_BLANK) {
System.out.println("提示:在Sheet"
+ (numSheets + 1) + "中的第"
+ (rowNumOfSheet + 1)
+ "行的第"
+ (cellNumOfRow + 1)
+ "列的值為空,請檢視核對是否符合約定要求");
}
}
}
}
if (flag == 7) {
System.out.println("Excel格式與預定格式相符");
}
if (sname != null && sex != null
&& birthday != null && address != null
&& tel != null && classno != null) {
Jlj_testStudent stu = new Jlj_testStudent();
stu.setSname(sname);
stu.setSex(sex);
stu.setBirthday(sdf.parse(birthday));
stu.setAddress(address);
stu.setTel(tel);
stu.setClassno(classno);


stuList.add(stu);
System.out.println("這是第" + (k + 1) + "次讀到的資料:"
+ stuList.get(k));
k++;
} // 獲得一行,即讀取每一行
}
}
}
} catch (Exception e) {


// 下面使用的是2003除了workbook的賦值不同其它與2007基本相同,
InputStream is = new FileInputStream(path);
workbook = new HSSFWorkbook(is);
try {
for (int numSheets = 0; numSheets < workbook
.getNumberOfSheets(); numSheets++) { // 讀取每一個sheet
System.out
.println("=============2003版進入讀取sheet的迴圈==============");
if (null != workbook.getSheetAt(numSheets)) {
HSSFSheet aSheet = (HSSFSheet) workbook
.getSheetAt(numSheets);


for (int rowNumOfSheet = 1; rowNumOfSheet <= aSheet
.getLastRowNum(); rowNumOfSheet++) { // 獲得一行
// 進入當前sheet的行的迴圈
if (null != aSheet.getRow(rowNumOfSheet)) {
HSSFRow aRow = aSheet.getRow(rowNumOfSheet); // 定義行,並賦值


for (int cellNumOfRow = 0; cellNumOfRow <= aRow
.getLastCellNum(); cellNumOfRow++) { // 讀取rowNumOfSheet值所對應行的資料
HSSFCell xCell = aRow
.getCell(cellNumOfRow); // 獲得行的列數
// //獲得列值


// System.out.println("type="+xCell.getCellType());
if (null != aRow.getCell(cellNumOfRow)) {
if (xCell.getCellType() == XSSFCell.CELL_TYPE_STRING) { // 為字串型


if (cellNumOfRow == 0) {
aRow.getCell(0)
.setCellType(
Cell.CELL_TYPE_STRING);
sid = xCell
.getStringCellValue();
if (sid == null) {
System.out
.println("錯誤:在Sheet"
+ (numSheets + 1)
+ "中的第"
+ (rowNumOfSheet + 1)
+ "行的第"
+ (cellNumOfRow + 1)
+ "列的學號不能為空");
}
} else if (cellNumOfRow == 1) {
aRow.getCell(1)
.setCellType(
Cell.CELL_TYPE_STRING);
sname = xCell
.getStringCellValue();
if (sname == null) {
System.out
.println("錯誤:在Sheet"
+ (numSheets + 1)
+ "中的第"
+ (rowNumOfSheet + 1)
+ "行的第"
+ (cellNumOfRow + 1)
+ "列的姓名不能為空");
}
} else if (cellNumOfRow == 2) {
aRow.getCell(2)
.setCellType(
Cell.CELL_TYPE_STRING);
sex = xCell
.getStringCellValue();
if (sex == null) {
System.out
.println("錯誤:在Sheet"
+ (numSheets + 1)
+ "中的第"
+ (rowNumOfSheet + 1)
+ "行的第"
+ (cellNumOfRow + 1)
+ "列的性別不能為空");
}
} else if (cellNumOfRow == 3) {
Date date = HSSFDateUtil
.getJavaDate(xCell
.getNumericCellValue());
birthday = sdf.format(date);
if (birthday == null) {
System.out
.println("錯誤:在Sheet"
+ (numSheets + 1)
+ "中的第"
+ (rowNumOfSheet + 1)
+ "行的第"
+ (cellNumOfRow + 1)
+ "列的出生日期不能為空");
}


} else if (cellNumOfRow == 4) {
aRow.getCell(4)
.setCellType(
Cell.CELL_TYPE_STRING);
address = xCell
.getStringCellValue();
if (address == null) {
System.out
.println("錯誤:在Sheet"
+ (numSheets + 1)
+ "中的第"
+ (rowNumOfSheet + 1)
+ "行的第"
+ (cellNumOfRow + 1)
+ "列的住址不能為空");
}
} else if (cellNumOfRow == 5) {
aRow.getCell(5)
.setCellType(
Cell.CELL_TYPE_STRING);
tel = xCell
.getStringCellValue();
if (tel == null) {
System.out
.println("錯誤:在Sheet"
+ (numSheets + 1)
+ "中的第"
+ (rowNumOfSheet + 1)
+ "行的第"
+ (cellNumOfRow + 1)
+ "列的聯絡方式不能為空");
}
} else if (cellNumOfRow == 6) {
aRow.getCell(6)
.setCellType(
Cell.CELL_TYPE_STRING);
classno = xCell
.getStringCellValue();
if (classno == null) {
System.out
.println("錯誤:在Sheet"
+ (numSheets + 1)
+ "中的第"
+ (rowNumOfSheet + 1)
+ "行的第"
+ (cellNumOfRow + 1)
+ "列的班級不能為空");
}
}
}
if (xCell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) { // 為數值型
if (cellNumOfRow == 0) {
aRow.getCell(0)
.setCellType(
Cell.CELL_TYPE_STRING);
sid = xCell
.getStringCellValue()
+ "";
if (sid == null) {
System.out
.println("錯誤:在Sheet"
+ (numSheets + 1)
+ "中的第"
+ (rowNumOfSheet + 1)
+ "行的第"
+ (cellNumOfRow + 1)
+ "列的學號不能為空");
}
} else if (cellNumOfRow == 1) {
aRow.getCell(1)
.setCellType(
Cell.CELL_TYPE_STRING);
sname = String
.valueOf(xCell
.getStringCellValue());
if (sname == null) {
System.out
.println("錯誤:在Sheet"
+ (numSheets + 1)
+ "中的第"
+ (rowNumOfSheet + 1)
+ "行的第"
+ (cellNumOfRow + 1)
+ "列的姓名不能為空");
}
} else if (cellNumOfRow == 2) {
aRow.getCell(2)
.setCellType(
Cell.CELL_TYPE_STRING);
sex = String
.valueOf(xCell
.getStringCellValue());
if (sex == null) {
System.out
.println("錯誤:在Sheet"
+ (numSheets + 1)
+ "中的第"
+ (rowNumOfSheet + 1)
+ "行的第"
+ (cellNumOfRow + 1)
+ "列的性別不能為空");
}
} else if (cellNumOfRow == 3) {
Date date = HSSFDateUtil
.getJavaDate(xCell
.getNumericCellValue());
birthday = sdf.format(date);
if (birthday == null) {
System.out
.println("錯誤:在Sheet"
+ (numSheets + 1)
+ "中的第"
+ (rowNumOfSheet + 1)
+ "行的第"
+ (cellNumOfRow + 1)
+ "列的出生日期不能為空");
}
} else if (cellNumOfRow == 4) {
aRow.getCell(4)
.setCellType(
Cell.CELL_TYPE_STRING);
address = String
.valueOf(xCell
.getStringCellValue());
if (address == null) {
System.out
.println("錯誤:在Sheet"
+ (numSheets + 1)
+ "中的第"
+ (rowNumOfSheet + 1)
+ "行的第"
+ (cellNumOfRow + 1)
+ "列的住址不能為空");
}
} else if (cellNumOfRow == 5) {
aRow.getCell(5)
.setCellType(
Cell.CELL_TYPE_STRING);
tel = String
.valueOf(xCell
.getStringCellValue());
if (tel == null) {
System.out
.println("錯誤:在Sheet"
+ (numSheets + 1)
+ "中的第"
+ (rowNumOfSheet + 1)
+ "行的第"
+ (cellNumOfRow + 1)
+ "列的聯絡方式不能為空");
}
} else if (cellNumOfRow == 6) {
aRow.getCell(6)
.setCellType(
Cell.CELL_TYPE_STRING);
classno = String
.valueOf(xCell
.getStringCellValue());
if (classno == null) {
System.out
.println("錯誤:在Sheet"
+ (numSheets + 1)
+ "中的第"
+ (rowNumOfSheet + 1)
+ "行的第"
+ (cellNumOfRow + 1)
+ "列的班級不能為空");
}
}
} else if (xCell.getCellType() == XSSFCell.CELL_TYPE_BLANK) {
System.out
.println("提示:在Sheet"
+ (numSheets + 1)
+ "中的第"
+ (rowNumOfSheet + 1)
+ "行的第"
+ (cellNumOfRow + 1)
+ "列的值為空,請檢視核對是否符合約定要求");
}
}
}


if (flag == 7) {
System.out.println("Excel格式與預定格式相符");
}
if (sname != null && sex != null
&& birthday != null
&& address != null && tel != null
&& classno != null) {
Jlj_testStudent stu = new Jlj_testStudent();
stu.setSname(sname);
stu.setSex(sex);
stu.setBirthday(sdf.parse(birthday));
stu.setAddress(address);
stu.setTel(tel);
stu.setClassno(classno);


stuList.add(stu);
System.out.println("這是第" + (k + 1)
+ "次讀到的資料:" + stuList.get(k));
k++;
}
}
}
}
}


} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (is != null)
is.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}


}
return stuList;
}
}