importjava.io.File;importjava.io.FileInputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjava.util.ArrayList;importjava.util.List;importjavax.servlet.http.HttpServletResponse;importorg.apache.poi.hssf.usermodel.HSSFCell;importorg.apache.poi.hssf.usermodel.HSSFCellStyle;importorg.apache.poi.hssf.usermodel.HSSFFont;importorg.apache.poi.hssf.usermodel.HSSFHeader;importorg.apache.poi.hssf.usermodel.HSSFRow;importorg.apache.poi.hssf.usermodel.HSSFSheet;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;importorg.apache.poi.ss.usermodel.Workbook;importorg.apache.poi.xssf.usermodel.XSSFCell;importorg.apache.poi.xssf.usermodel.XSSFRow;importorg.apache.poi.xssf.usermodel.XSSFSheet;importorg.apache.poi.xssf.usermodel.XSSFWorkbook;importorg.apache.struts2.ServletActionContext;importcom.dev.iex.po.Student;
定义的的变量:private File excelFile;//File对象,目的是获取页面上传的文件
private List stuList=new ArrayList();
定义的方法:publicFile getExcelFile() {returnexcelFile;
}public voidsetExcelFile(File excelFile) {this.excelFile =excelFile;
}public ListgetStuList() {returnstuList;
}public void setStuList(ListstuList) {this.stuList =stuList;
}
主要编写的是importExcel方法,如下:
@SuppressWarnings("finally")public String importExcel()throwsException
{
String id=null;
String name=null;
String sex=null;
String Dormitory=null;
String Sept=null;
Workbook workbook= null;int k=0;int flag = 0; //指示指针所访问的位置
if(excelFile!=null)
{
String path=excelFile.getAbsolutePath();//获取文件的路径
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 = 0; 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(rowNumOfSheet == 0)
{//如果rowNumOfSheet的值为0,则读取表头,判断excel的格式和预定格式是否相符
if(xCell.getCellType() ==XSSFCell .CELL_TYPE_NUMERIC)
{
}else if(xCell.getCellType() ==XSSFCell .CELL_TYPE_BOOLEAN)
{
}else if(xCell.getCellType() ==XSSFCell .CELL_TYPE_STRING)
{if(cellNumOfRow == 0)
{if(xCell.getStringCellValue().replace('\t', ' ').replace('\n', ' ').replace('\r', ' ').trim().equals("学号"))
{
flag++;
}else{
System.out.println("错误:第一行的学号不符合约定格式");
}
}else if(cellNumOfRow == 1)
{if(xCell.getStringCellValue().replace('\t', ' ').replace('\n', ' ').replace('\r', ' ').trim().equals("姓名"))
{
flag++;
}else{
System.out.println("错误:第一行的姓名不符合约定格式");
}
}else if(cellNumOfRow == 2)
{if(xCell.getStringCellValue().replace('\t', ' ').replace('\n', ' ').replace('\r', ' ').trim().equals("性别")){
flag++;
}else{
System.out.println("第一行的性别不符合约定格式");
}
}else if (cellNumOfRow == 3) {if(xCell.getStringCellValue().replace('\t', ' ').replace('\n', ' ').replace('\r', ' ').trim().equals("寝室号"))
{
flag++;
System.out.println("=========flag:" +flag);
}else{
System.out.println("第一行的寝室号不符合约定格式");
}
}else if (cellNumOfRow == 4)
{if(xCell.getStringCellValue().replace('\t', ' ').replace('\n', ' ').replace('\r', ' ').trim().equals("所在系")){
flag++;
System.out.println("=========flag:" +flag);
}else{
System.out.println("第一行的所在系不符合约定格式");
}
}
}
}else{//rowNumOfSheet != 0 即开始打印内容
if(id != null && name != null && sex != null && Dormitory != null && Sept != null){
Student stu=newStudent();
stu.setStudentId(id);
stu.setStudentName(name);
stu.setStudentSept(Sept);
stu.setStudentSex(sex);
stu.setStudentDormitory(Dormitory);
stuList.add(stu);
k++;
}
}//获得一行,即读取每一行
}//读取每一个sheet
}
}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 = 0; rowNumOfSheet <= aSheet.getLastRowNum(); rowNumOfSheet++) { //获得一行
if (null != aSheet.getRow(rowNumOfSheet)) {
HSSFRow aRow = aSheet.getRow(rowNumOfSheet);
for (int cellNumOfRow = 0; cellNumOfRow <= aRow.getLastCellNum(); cellNumOfRow++) { //读取rowNumOfSheet值所对应行的数据
HSSFCell aCell = aRow.getCell(cellNumOfRow); //获得列值
if (null != aRow.getCell(cellNumOfRow)){
if(rowNumOfSheet == 0){ // 如果rowNumOfSheet的值为0,则读取表头,判断excel的格式和预定格式是否相符
if(aCell.getCellType() == HSSFCell .CELL_TYPE_NUMERIC){
}else if(aCell.getCellType() == HSSFCell .CELL_TYPE_BOOLEAN){
}else if(aCell.getCellType() == HSSFCell .CELL_TYPE_STRING){
if(cellNumOfRow == 0){
if(aCell.getStringCellValue().replace('\t', ' ').replace('\n', ' ').replace('\r', ' ').trim().equals("学号")){
flag++;
System.out.println("=========flag:" + flag);
}else{
System.out.println("错误:第一行的学号不符合约定格式");
}
}else if(cellNumOfRow == 1){
if(aCell.getStringCellValue().replace('\t', ' ').replace('\n', ' ').replace('\r', ' ').trim().equals("姓名")){
flag++;
System.out.println("=========flag:" + flag);
}else{
System.out.println("错误:第一行的姓名不符合约定格式");
}
}else if(cellNumOfRow == 2){
if(aCell.getStringCellValue().replace('\t', ' ').replace('\n', ' ').replace('\r', ' ').trim().equals("性别")){
flag++;
System.out.println("=========flag:" + flag);
}else{
System.out.println("第一行的性别不符合约定格式");
}
}else if (cellNumOfRow == 3){
if(aCell.getStringCellValue().replace('\t', ' ').replace('\n', ' ').replace('\r', ' ').trim().equals("寝室号")){
flag++;
System.out.println("=========flag:" + flag);
}else{
System.out.println("第一行的寝室号不符合约定格式");
}
}else if (cellNumOfRow == 4){
if(aCell.getStringCellValue().replace('\t', ' ').replace('\n', ' ').replace('\r', ' ').trim().equals("所在系")){
flag++;
System.out.println("=========flag:" + flag);
}else{
System.out.println("第一行的所在系不符合约定格式");
}
}
}
}
else {
if(aCell.getCellType() == HSSFCell .CELL_TYPE_NUMERIC){ //为数值型
System.out.println("======进入XSSFCell .CELL_TYPE_NUMERIC模块==========");
if(cellNumOfRow == 0){
id = String.valueOf(aCell.getStringCellValue().replace('\t', ' ').replace('\n', ' ').replace('\r', ' ').trim());
if(id == null){
System.out.println("错误:在Sheet"+(numSheets+1)+"中的第"+(rowNumOfSheet+1)+"行的第"+(cellNumOfRow+1)+"列的学号不能为空");
}
}else if(cellNumOfRow == 1){
name = String.valueOf(aCell.getStringCellValue().replace('\t', ' ').replace('\n', ' ').replace('\r', ' ').trim());
if(name == null){
System.out.println("错误:在Sheet"+(numSheets+1)+"中的第"+(rowNumOfSheet+1)+"行的第"+(cellNumOfRow+1)+"列的姓名不能为空");
}
}else if(cellNumOfRow == 2){
sex = String.valueOf(aCell.getStringCellValue().replace('\t', ' ').replace('\n', ' ').replace('\r', ' ').trim());
if(sex == null){
System.out.println("错误:在Sheet"+(numSheets+1)+"中的第"+(rowNumOfSheet+1)+"行的第"+(cellNumOfRow+1)+"列的性别不能为空");
}
}else if (cellNumOfRow == 3){ Dormitory = String.valueOf(aCell.getStringCellValue().replace('\t', ' ').replace('\n', ' ').replace('\r', ' ').trim());
if(Dormitory == null){
System.out.println("错误:在Sheet"+(numSheets+1)+"中的第"+(rowNumOfSheet+1)+"行的第"+(cellNumOfRow+1)+"列的寝室号不能为空");
}
}else if (cellNumOfRow == 4){ Sept = String.valueOf(aCell.getStringCellValue().replace('\t', ' ').replace('\n', ' ').replace('\r', ' ').trim());
if(Sept == null){
System.out.println("错误:在Sheet"+(numSheets+1)+"中的第"+(rowNumOfSheet+1)+"行的第"+(cellNumOfRow+1)+"列的所在系不能为空");
}
}
}else if(aCell.getCellType() == HSSFCell .CELL_TYPE_STRING){ //为字符串型
System.out.print("===============进入XSSFCell .CELL_TYPE_STRING模块============");
if(cellNumOfRow == 0){
id = aCell.getStringCellValue().replace('\t', ' ').replace('\n', ' ').replace('\r', ' ').trim();
if(id == null){
System.out.println("错误:在Sheet"+(numSheets+1)+"中的第"+(rowNumOfSheet+1)+"行的第"+(cellNumOfRow+1)+"列的学号不能为空");
}
}else if(cellNumOfRow == 1){
name = aCell.getStringCellValue().replace('\t', ' ').replace('\n', ' ').replace('\r', ' ').trim();
if(name == null){
System.out.println("错误:在Sheet"+(numSheets+1)+"中的第"+(rowNumOfSheet+1)+"行的第"+(cellNumOfRow+1)+"列的姓名不能为空");
}
}else if(cellNumOfRow == 2){
sex = aCell.getStringCellValue().replace('\t', ' ').replace('\n', ' ').replace('\r', ' ').trim();
if(sex == null){
System.out.println("错误:在Sheet"+(numSheets+1)+"中的第"+(rowNumOfSheet+1)+"行的第"+(cellNumOfRow+1)+"列的性别不能为空");
}
}else if (cellNumOfRow == 3){
Dormitory =aCell.getStringCellValue().replace('\t', ' ').replace('\n', ' ').replace('\r', ' ').trim();
if(Dormitory == null){
System.out.println("错误:在Sheet"+(numSheets+1)+"中的第"+(rowNumOfSheet+1)+"行的第"+(cellNumOfRow+1)+"列的寝室号不能为空");
}
}else if (cellNumOfRow == 4){ Sept =aCell.getStringCellValue().replace('\t', ' ').replace('\n', ' ').replace('\r', ' ').trim();
if(Sept == null){
System.out.println("错误:在Sheet"+(numSheets+1)+"中的第"+(rowNumOfSheet+1)+"行的第"+(cellNumOfRow+1)+"列的所在系不能为空");
}
}
}else if (aCell.getCellType() == HSSFCell .CELL_TYPE_BLANK) {
System.out.println("提示:在Sheet"+(numSheets+1)+"中的第"+(rowNumOfSheet+1)+"行的第"+(cellNumOfRow+1)+"列的值为空,请查看核对是否符合约定要求".toString());
}
}
}
}
if (flag!=5){
System.out.println("请核对后重试");
}
}
if(id != null && name != null && sex != null && Dormitory != null && Sept != null ){
Student stu=new Student();
stu.setStudentId(id);
stu.setStudentName(name);
stu.setStudentSept(Sept);
stu.setStudentSex(sex);
stu.setStudentDormitory(Dormitory);
stuList.add(stu);
k++;
}
}
if(k!=0){
System.out.println("提示:您导入的数据已存在于数据库,请核对!k 为:" + k);
}else{
System.out.println("提示:成功导入了"+k+"条数据");
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}finally{
try {
if(is!=null)
is.close();
}catch (Exception e1) {
e1.printStackTrace();
}
}
}
}
return "SUCCESS";
}