使用JAVA读取EXCEL文件里面的数据2009-09-25 15:11收集网上的样例:
1.jxl比较方便,写Excel和读Excel分开,很好理解
写入excel
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.write.WritableWorkbook;
import jxl.write.WritableSheet;
import jxl.write.WritableFont;
import jxl.write.WritableCellFormat;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WriteException;
public class CountDownloadExcel {
private WritableWorkbook workbook;
public void write(CountSzyfView cs, java.io.OutputStream outputStream)
throws IOException, WriteException {
WorkbookSettings ws = new WorkbookSettings();
ws.setLocale(new Locale("zh","CN"));
workbook = Workbook.createWorkbook(outputStream, ws);
WWritableSheet(cs,"sheet1", workbook);
workbook.write();
workbook.close();
}
private void WWritableSheet(CountSzyfView cs, String str,
WritableWorkbook workbook) throws IOException, WriteException {
WritableSheet s = workbook.createSheet(str, 0);
WritableFont arial12pt = new WritableFont(WritableFont.ARIAL, 12);
WritableCellFormat arial12format = new WritableCellFormat(arial12pt);
arial12format.setWrap(true);
s.setColumnView(0, 20);
Label lr = new Label(0, 0, "123123", arial12format);
s.addCell(lr);
lr = new Label(1, 0, "bt", arial12format);
s.addCell(lr);
cellLabel(cs, s);
}
private void cellLabel(CountSzyfView cs, WritableSheet ss) {
Label lr;
if (cs != null) {
lr = new Label(0, i + 1, cobject.getName());
ss.addCell(lr);
}
}
}
2.--使用jxl读取xls文件
import java.io.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import jxl.*;
import jxl.write.*;
public class TestJxl{
public static void main(String[] args){
try{
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
java.text.SimpleDateFormat sdf1 = new java.text.SimpleDateFormat("yyyyMMddHHmmss");
List l = new ArrayList();
List o = new ArrayList();
Workbook info = Workbook.getWorkbook(new File("f:/2006FootBall.xls"));
Sheet sheet = info.getSheet(0);
int size = sheet.getRows();
//size = 1;
for(int i = 0;i < size;i++){
Cell c = sheet.getCell(0,i);
String c1 = c.getContents();
c = sheet.getCell(1,i);
String c2 = c.getContents();
c = sheet.getCell(2,i);
String c3 = c.getContents();
c = sheet.getCell(3,i);
String c4 = c.getContents();
c = sheet.getCell(4,i);
String c5 = c.getContents();
c = sheet.getCell(5,i);
String c6 = c.getContents();
if(c3.equals("090000")){
c3 = "210000";
}
else if(c3.equals("100000")){
c3 = "220000";
}
else if(c3.equals("110000")){
c3 = "220000";
}
else if(c3.equals("120000")){
c3 = "000000";
}
System.out.print(c1);
System.out.print("\t" + c2);
System.out.print("\t" + c3);
System.out.print("\t" + c4);
System.out.print("\t" + c5);
System.out.println("\t" + c6);
l.add(c1+","+c2+","+c3+","+c4+","+c5+","+c6);
}
info.close();
size = l.size();
//size = 1;
System.out.println(size);
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String submit = "null";
try{
conn = getMysqlConnection("jdbc:mysql://localhost:3306/ddsms?useUnicode=true&characterEncoding=GBK","test","test");
stmt = conn.createStatement();
for(int i = 0;i < size;i++){
//System.out.println(l.get(i));
String[] m = l.get(i).toString().split(",");
String sql = "insert into ddsms.match_info(match_name,match_id,match_date,match_time,match_addr,match_group,match_info,match_result,match_status,submit_time,update_time)"
+ " values('2006年足球世界杯','"+m[0]+"','"+m[1]+"','"+m[2]+"','"+m[3]+"','"+m[5]+"','"+m[4]+"','',1,now(),now())";
//System.out.println(sql);
stmt.executeUpdate(sql);
}
}catch(Exception exx){
exx.printStackTrace();
}finally{
try{
if(rs != null){
rs.close();
}
if(stmt != null){
stmt.close();
}
rs = null;
stmt = null;
}catch(Exception exxx){
}
}
closeConnection(conn);
conn = null;
//createXLS(o,"f:","1_ok.xls");
}catch(Exception ex){
ex.printStackTrace();
}
}
public static boolean createXLS(List l,String path,String filename){
boolean flag = true;
WritableWorkbook info = null;
WritableSheet sheet = null;
Label c = null;
try{
String file = path+"/"+filename;
File f = new File(file);
info = Workbook.createWorkbook(f);
sheet = info.createSheet("ok",0);
int size = l.size();
for(int i = 0;i < size;i++){
String[] msg = l.get(i).toString().split("/");
c = new Label(0,i,msg[0]);
sheet.addCell(c);
c = new Label(1,i,msg[1]);
sheet.addCell(c);
c = new Label(2,i,msg[2]);
sheet.addCell(c);
c = new Label(3,i,msg[3]);
sheet.addCell(c);
}
info.write();
}catch(Exception ex){
flag = false;
ex.printStackTrace();
}finally{
try{
if(info != null){
info.close();
}
}catch(Exception exx){
}
}
return flag;
}
private static Connection getOracleConnection(String dburl,String dbuser,String dbpass){
Connection conn = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(dburl,dbuser,dbpass);
}catch(Exception ex){
ex.printStackTrace();
}
return conn;
}
private static Connection getMysqlConnection(String dburl,String dbuser,String dbpass){
Connection conn = null;
try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn = DriverManager.getConnection(dburl,dbuser,dbpass);
}catch(Exception ex){
ex.printStackTrace();
}
return conn;
}
private static void closeConnection(Connection conn){
try {
if(conn != null){
conn.close();
}
conn = null;
}catch(Exception ex){
ex.printStackTrace();
}
}
}
3.-------描述:java读取excel文件,首先取出所有的sheets,读取相应列值
package com.easydone.cn.excel;
import java.io.*;
import jxl.*;
/**
* <p>Title: </p>
* <p>Description: 管理目录分类</p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: http://www.easydone.cn</p>
* @authory dirboy
* @version 1.0
*/
public class GetExcelValues {
public static void main(String[] args){
try{
String sourcefile = "c://125.xls";
InputStream is = new FileInputStream(sourcefile);
jxl.Workbook rwb = Workbook.getWorkbook(is);
//获得总 Sheets
Sheet[] sheets = rwb.getSheets();
int sheetLen = sheets.length;
for(int i=0;i<sheetLen;i++){
//获得单个Sheets 含有的行数
jxl.Sheet rs = rwb.getSheet(i); //
Cell[] cell_domain = rs.getColumn(0);//读取第一列的值
for(int j=1;j<cell_domain.length;j++){
System.out.println("Values:"+cell_domain[j].getContents());
}
}
}catch(Exception ex){
ex.printStackTrace();
}
}
4.----------JXL(Java Excel API)是一个用来动态读写Excel文件的开源框架,利用它可以在任何支持Java的操作系统上动态读写Excel文件。JXL的主页是:http://www.andykhan.com/jexcelapi/,可以在这里下载到它的最新的版本。
你可以分别通过如下命令
java -jar jxl.jar -xml test.xls
java -jar jxl.jar -cvs test.xls
以xml和cvs格式查看test.xls,这是因为JXL作者实现了一个用来演示的jxl.demo.Demo的类。
当然我们使用JXL主要是用它来动态读写Excel文件。现在就来看看JXL在对Excel的读和写上都提供了那些支持。
先来看看Excel文件中都有写上面对象
1 文件对象 2工作簿对象 3 单元格对象。
相应的在JXL中就有Workbook,Sheet ,Cell 。通过这三个对象我们就可以实现Excel文件的读取工作。
我们先想想一下读取步骤,不管是什么样的Excel操作框架必定都要经历
1选取Excel文件,2选择工作簿,3选择Cell,4读取信息。
那么现在就可以看看JXL中这四步骤如何体现:
//通过Workbook的静态方法getWorkbook选取Excel文件
Workbook workbook = Workbook.getWorkbook(new File("myfile.xls"));
//通过Workbook的getSheet方法选择第一个工作簿(从0开始)
Sheet sheet = workbook.getSheet(0);
//通过Sheet方法的getCell方法选择位置为C2的单元格(两个参数都从0开始)
Cell c2 = sheet.getCell(2,1);
//通过Cell的getContents方法把单元格中的信息以字符的形式读取出来
String stringc2 = c2.getContents();
可以看到正是刚才所说的四个步骤。
我们都知道Excel单元格是有格式的,那么这些信息如何取得。
Cell提供了一个getType方法能够返回单元格的类型信息,同时JXL提供了一个CellType类用来预设Excel中的类型信息,而且JXL提供了一些Cell类的子类用来分别用来表示各种类型的单元格,如LabelCell,NumberCell,DateCell分别表示字符、数值、日期类型的单元格。所以我们可以这样写:
if (c2.getType() == CellType.NUMBER)
{
NumberCell nc = (NumberCell) c2;
numberb2 = nc.getValue();
}
最后不要忘记关闭workbook以释放资源:
workbook.close();
现在总结一下:三个对象,四个步骤,注意格式。就可以轻松的从Excel文件中读取数据。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wuwenlong527/archive/2008/03/05/2152017.aspx