jxl读取excel文件
2010-01-21 22:15
330 查看
package inportexcel;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
*
* @author zhangel
* jxl操作excel,读取excel中的数据,导入到数据库中
*
*/
public class ImportExcelAction extends Action {
private Vector sumExcelList;
private Cell cell;
private int rows;
private int cols;
private String sql;
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String fileContext = null;
File file =null;
try {
fileContext = new String(request.getParameter("fileContext")
.getBytes("iso-8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
if(fileContext!=null){
file = new File(fileContext);
}
Workbook workbook = null;
try {
workbook = Workbook.getWorkbook(file);
} catch (BiffException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
Sheet sheet = workbook.getSheet(0);
rows = sheet.getRows();
// System.out.print(rows);
cols = sheet.getColumns();
// System.out.print(cols);
sumExcelList = getSumExcelList(rows, cols, sheet);
// System.out.print(sheet.getCell(1, 0).getContents());
// System.out.print(sumExcelList);
sql = buildSQL(cols);
// System.out.print(sql1);
// mysql测试
String dbDriver = "com.mysql.jdbc.Driver";
String dbUser = "root";
String dbPasswor = "root";
String dbURL = "jdbc:mysql://127.0.0.1:3306/lindb";
try {
Class.forName(dbDriver);
Connection conn = DriverManager.getConnection(dbURL, dbUser,
dbPasswor);
PreparedStatement ps = conn.prepareStatement(sql);
importDB(sumExcelList, ps);
conn.close();
ps.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return mapping.findForward("success");
}
// 构建sql语句
public String buildSQL(int cols) {
String sql = "insert into worker(";
String rowFirst = (String) sumExcelList.get(0);
sql = sql + rowFirst.substring(0, rowFirst.length() - 1) + ") values(";
for (int i = 0; i < cols; i++) {
sql += "?,";
}
sql = sql.subSequence(0, sql.length() - 1) + ")";
return sql;
}
public Vector getSumExcelList(int rows, int cols, Sheet sheet) {
sumExcelList = new Vector();
for (int i = 0; i < rows; i++) {
String rowList = "";
for (int j = 0; j < cols; j++) {
cell = sheet.getCell(j, i);
rowList += cell.getContents() + ",";
}
sumExcelList.add(rowList);
}
return sumExcelList;
}
// 导入数据库中
public void importDB(Vector sumExcelList, PreparedStatement ps)
throws SQLException {
for (int i = 1; i < sumExcelList.size(); i++) {
String rowContext = (String) sumExcelList.get(i);
String[] strs = rowContext.split(",");
for (int j = 0; j < strs.length; j++) {
ps.setString(j + 1, strs[j]);
}
ps.addBatch();
}
ps.executeBatch();
}
}
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
*
* @author zhangel
* jxl操作excel,读取excel中的数据,导入到数据库中
*
*/
public class ImportExcelAction extends Action {
private Vector sumExcelList;
private Cell cell;
private int rows;
private int cols;
private String sql;
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String fileContext = null;
File file =null;
try {
fileContext = new String(request.getParameter("fileContext")
.getBytes("iso-8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
if(fileContext!=null){
file = new File(fileContext);
}
Workbook workbook = null;
try {
workbook = Workbook.getWorkbook(file);
} catch (BiffException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
Sheet sheet = workbook.getSheet(0);
rows = sheet.getRows();
// System.out.print(rows);
cols = sheet.getColumns();
// System.out.print(cols);
sumExcelList = getSumExcelList(rows, cols, sheet);
// System.out.print(sheet.getCell(1, 0).getContents());
// System.out.print(sumExcelList);
sql = buildSQL(cols);
// System.out.print(sql1);
// mysql测试
String dbDriver = "com.mysql.jdbc.Driver";
String dbUser = "root";
String dbPasswor = "root";
String dbURL = "jdbc:mysql://127.0.0.1:3306/lindb";
try {
Class.forName(dbDriver);
Connection conn = DriverManager.getConnection(dbURL, dbUser,
dbPasswor);
PreparedStatement ps = conn.prepareStatement(sql);
importDB(sumExcelList, ps);
conn.close();
ps.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return mapping.findForward("success");
}
// 构建sql语句
public String buildSQL(int cols) {
String sql = "insert into worker(";
String rowFirst = (String) sumExcelList.get(0);
sql = sql + rowFirst.substring(0, rowFirst.length() - 1) + ") values(";
for (int i = 0; i < cols; i++) {
sql += "?,";
}
sql = sql.subSequence(0, sql.length() - 1) + ")";
return sql;
}
public Vector getSumExcelList(int rows, int cols, Sheet sheet) {
sumExcelList = new Vector();
for (int i = 0; i < rows; i++) {
String rowList = "";
for (int j = 0; j < cols; j++) {
cell = sheet.getCell(j, i);
rowList += cell.getContents() + ",";
}
sumExcelList.add(rowList);
}
return sumExcelList;
}
// 导入数据库中
public void importDB(Vector sumExcelList, PreparedStatement ps)
throws SQLException {
for (int i = 1; i < sumExcelList.size(); i++) {
String rowContext = (String) sumExcelList.get(i);
String[] strs = rowContext.split(",");
for (int j = 0; j < strs.length; j++) {
ps.setString(j + 1, strs[j]);
}
ps.addBatch();
}
ps.executeBatch();
}
}
相关文章推荐
- jxl读取excel文件异常:Unable to recognize OLE stream 的解决方法
- 如何使用JavaExcel(jxl)读取一个文件并写入一个新文件
- jxl读取excel文件时的多语言和本地化
- POI or jxl 在不能读取Excel文件记录时的处理办法之一
- 利用jxl.jar读取EXCEL文件
- jxl读取excel文件异常:Unable to recognize OLE stream
- jxl 读取 Excel文件
- jxl读取指定文件下的所有Excel文件,但是是.xls文件,查的是jxl不支持excel2007
- 用jxl.jar读取Excel文件内容
- JXL读取Excel文件测试代码
- java用JXL读取excel文件
- OAF_文件系列9_实现OAF解析Excel并读取至数据库JXL
- jxl 读取excel 文件 Unable to recognize OLE stream
- [JAVA] java读取Excel文件内容,jxl(Excel 2003)
- JXL 读取 Excel java中jxl导出数据到excel的例子 上传文件
- jxl 读取 excel 文件 占用 CPU 99% 的问题
- 1.读取excel文件,将输入存储到数据库中(JXL) 2.完成商品的检索相关功能 1.根据分类,显示分类下所有的商品信息,按照库存量从低到高排序(提供补货依据) 2.模糊搜索,根据商品信息(名
- Java:使用jxl读取excel文件内容
- Jxl创建和读取excel文件
- 在structs框架下使用JXL文件读取工具将Excel表格导入本地数据库