如何实现在JSP页面下,通过类似于文件上传的组件,选择本地的Excel文件,点击“导入”按钮,Excel文件的内容保存到数据库中相应的表中,同时Excel文件的内容显示在页面下,数据库的表已经建好。
思路是:
1.把excel文件上传到服务器的目录下
2.通过Java读取文件的内容
3.写SQL语句将内容插入数据库
4.从数据库中查询上一步所插入的记录,通过JSP显示
步骤是:
第一是文件上传,可以参照Jakarta的FileUpload组件,其实也不一定要用这个,用普通的Post也就行了。
第二是Excel解析,用JXL或者POI都行
第三是数据保存,这个应该简单吧,一个循环,一行对应一条数据,写好了方法循环赋值调用就行了。
第四是查询和显示。
引入包 jxl.jar;mysql-connector-java-5.1.7-bin.jar;InsertExcel.java
public class InsertExcel
{
static String UserName = "newExcel"; // 生成的数据库名字
private static Connection conn = null;
private static String drive = "com.mysql.jdbc.Driver";
private static String DBurl ="jdbc:mysql://localhost:3306/test";
private static String name = "root"; //数据库账号
private static String pwd = "123456"; //数据库,密码
private static Statement st=null;
public static void main(String[] args){
readExcel("E:/newExcel.xls");
System.out.println( getExtensionName("newExcel.xls"));
}
public static void readExcel(String url) {
File filename = new File(url);
Workbook wb = null;
String sql = "insert into " + UserName ;
String value = " ";
String insert = "";
try{
wb = Workbook.getWorkbook(filename);
Sheet s = wb.getSheet(0);// 第1个sheet
Cell c = null;
int rows = s.getRows();// 总行数
int cols = s.getColumns();// 总列数
for (int i = 1; i < rows; i++) {
value="";
for (int j = 0; j < cols; j++) {
c = s.getCell(j, i);
if (j < cols - 1) {
value += "'" + c.getContents() + "',";
} else {
value += "'" + c.getContents() + "'";
}
}
insert = sql + " values(" + value + ")";
System.out.println("添加语句-------" + insert);
int a = insert(insert);
if (a > 0) {
System.out.println("成功添加" + i + "次");
} else {
System.out.println("第" + i + "次失败了");
}
}
}catch(Exception e){
System.out.println(e.getMessage());
}
}
public static String getExtensionName(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot >-1) && (dot < (filename.length() - 1))) {
return filename.substring(dot + 1);
}
}
return filename;
}
public static Connection getConn() {
try {
Class.forName(drive);
conn = (Connection) DriverManager.getConnection(DBurl, name, pwd);
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "数据库连接错误");
}
return conn;
}
public static void Close() {
if (conn != null)
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static int insert(String sql){
int result=0;
try {
st=(Statement) getConn().createStatement();
result=st.executeUpdate(sql);
} catch (Exception e) {
System.out.println("添加失败");
}finally{
Close();
}
return result;
}
public static int Create(String sql) throws Exception{
int result=0;
try {
st=(Statement) getConn().createStatement();
result=st.executeUpdate(sql);
}finally{
Close();
}
return result;
}
}
CreateExcel.java
public class CreateExcel
{
public static void main(String args[]) {
try {
// 打开文件
WritableWorkbook book = Workbook.createWorkbook( new File( " test.xls " ));
// 生成名为“第一页”的工作表,参数0(没用,按照创建顺序 )表示这是第一页
WritableSheet sheet = book.createSheet( " 第一页 " , 0 );
WritableSheet sheet2 = book.createSheet( " sheet2 " , 5 );
WritableSheet sheet3 = book.createSheet( " 第er页 " , 2 );
// 将第一行的高度设为200
sheet.setRowView( 0 , 200 );
// 将第一列的宽度设为30
sheet.setColumnView( 0 , 30 );
WritableFont font1 = new WritableFont(WritableFont.TIMES, 16 ,WritableFont.BOLD);
WritableCellFormat format1 = new WritableCellFormat(font1);
Label label4 = new Label( 0 , 2 ,"data 4 test",format1);
// 在Label对象的构造子中指名单元格位置是第一列第一行(0,0)(一个label不能插入两张sheet)
Label label = new Label( 0 , 0 , " test0 " );
Label label2 = new Label( 10 , 10 , " test10 " );
Label label3 = new Label( 0 , 0 , " test0 " );
// 将定义好的单元格添加到工作表中
sheet.addCell(label);
sheet.addCell(label2);
sheet2.addCell(label3);
sheet2.addCell(label4);
/*
* 生成一个保存数字的单元格 必须使用Number的完整包路径,否则有语法歧义 单元格位置是第二列,第一行,值为789.123
*/
jxl.write.Number number = new jxl.write.Number( 1 , 0 , 555.12541 );
sheet.addCell(number);
// 写入数据并关闭文件
book.write();
// 得到第一列第一行的单元格
int columnum = sheet.getColumns(); // 得到列数
int rownum = sheet.getRows(); // 得到行数
System.out.println(columnum);
System.out.println(rownum);
for ( int i = 0 ; i < rownum; i ++ ) // 循环进行读写
{
for ( int j = 0 ; j < columnum; j ++ ) {
Cell cell1 = sheet.getCell(j, i);
String result = cell1.getContents();
System.out.print(result);
System.out.print( " \t " );
}
System.out.println();
}
book.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
ReadExcel.java
public class ReadExcel
{
public static void main(String args[]) {
try {
Workbook book = Workbook.getWorkbook( new File( " test.xls " ));
int sheetNum = book.getNumberOfSheets();
for(int i=0;i<sheetNum;i++){
Sheet sheet = book.getSheet( i );
int columnNum = sheet.getColumns();
String sheetName = sheet.getName();
int rowNum = sheet.getRows();
for(int row=0;row<rowNum;row++){
for(int column=0;column<columnNum;column++){
Cell cell1 = sheet.getCell( row , column );
String result = cell1.getContents();
}
}
}
book.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
UpdateExcel.java
public class UpdateExcel
{
public static void main(String args[]) {
try {
// Excel获得文件
Workbook wb = Workbook.getWorkbook( new File( " test.xls " ));
// 打开一个文件的副本,并且指定数据写回到原文件
WritableWorkbook book = Workbook.createWorkbook( new File( " test.xls " ), wb);
// 添加一个工作表
WritableSheet sheet = book.createSheet( " 第二页 " , 1 );
sheet.addCell( new Label( 0 , 0 , " 第二页的测试数据 " ));
book.write();
book.close();
} catch (Exception e) {
System.out.println(e);
}
}
}