package com.neusoft.serviceImp;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.*;
import java.sql.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.neusoft.daoImp.PeoplesDaoImp;
import com.neusoft.entity.Peoples;
import jxl.Workbook;
import jxl.Sheet;
import jxl.Cell;
//本实例为在jxl.jar包下开发的,下载jxl.jar包并导入到自己的工程中
// 把excel 导入到数据库中Connection con为连接数据库的对象
public class ExceltoResult {
public static synchronized void createExcelFile(String name)
throws Exception {
try {
// name为文件名 直接从本地文件创建Workbook一旦创建了Workbook,我们就可以通过它来访问Excel
// Sheet(术语:工作表)
System.out.println("ASASASASAASASASA");
InputStream is = new FileInputStream(name);
// 从输入流创建Workbook
System.out.println("ASASASASAASASASA");
jxl.Workbook rwb = Workbook.getWorkbook(is);
// 获取第一张Sheet表一旦得到了Sheet,我们就可以通过它来访问Excel Cell(术语:单元格)
Sheet rs1 = rwb.getSheet(0);
sheettoResult(rs1);
System.out.println("ASASASASAASASASA");
// 关闭Excel工作薄对象
rwb.close();
} catch (Exception e) {
e.getStackTrace();
}
}
// 函数为获取单元格的内容
private static void sheettoResult(Sheet sheet)
throws Exception {
// 因为第一行是列名,所以内容从第二行开始取。(列名最好对应数据库中相关的字段)
int row = 1;
// 获得excel中的列的个数
int columns = sheet.getColumns();
// 获得单元格的行数
int c = sheet.getRows();
while (row <= c) {
// 用列表存放相关的内容
Peoples people = new Peoples();
// 一次按列取相关的值
Cell cel = sheet.getCell(0, row);
// 获得单元格中的内容。
String strc = cel.getContents();
people.setGroupname(strc);
cel = sheet.getCell(1, row);
strc = cel.getContents();
people.setName(strc);
cel = sheet.getCell(2, row);
strc = cel.getContents();
people.setBirthday(strc);
cel = sheet.getCell(3, row);
strc = cel.getContents();
people.setTelephone(strc);
cel = sheet.getCell(4, row);
strc = cel.getContents();
people.setEmail(strc);
cel = sheet.getCell(5, row);
strc = cel.getContents();
people.setAddress(strc);
// 相关的sql语句。按自己的需求写
System.out.println("1!!!!!");
ApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("1@@@@@@");
PeoplesDaoImp dao=(PeoplesDaoImp) factory.getBean("peoplesDaoImp");
dao.insert(people);
row++;
}
System.out.println("ASASASASAASASASA");
}
public static void main(String args[]) throws Exception{
ExceltoResult e = new ExceltoResult();
ExceltoResult.createExcelFile("d:/output.xls");
}
}