如何用JAVA读取EXCEL文件里面的数据 开源项目poijxl等等等等用google搜一下就行了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 jxl比较方便,写Excel和读Excel分开,很好理解 用jxl。很方便的。操作excel的方法和很多。很全。 用POI吧,挺简单的,要实现这种功能,各位大虾们有对WORD进行操作的经验没?那个开源组织关于WORD还不成熟啊 用jxl包,读取都很好用,从网上找点例子 直接用JDBC...........路过.....友情up.....//--------------------送个笑话-------------------一位迷人的女郎夏天开着车子到全国旅行,由于天气实在太热,她全身已经香汗淋漓。开到某个乡下地方时,她看到一座水池,于是她决定停车游个水,凉凉身,她脱光跳进水中,享受几分钟的清凉后,突然发现两位农夫躲在树丛下偷看。由于她的衣服摆在水池的另一边,不过靠近她身边有个澡盆,于是她拿起澡盆遮住身子,往那两位农夫走去。 “你们两个王八蛋,难道没别事好做吗?”她咆哮道,“你们知道我怎么想吗?” “是的,女士。”个子较高的一位说,“你一定想问我们什么地方可以补你那浴盆上的大窟窿!” www.xiaohua007.com 给你一个我写的例子,最好看看jxl提供的源码,你会很清楚的。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); } } } 我这个是写入excel,读相反,你看一下他提供的方法就可以。还有一种方法,就是通过jdbc-odbc桥驱动连接,把它当作一种数据库类型进行对象化,然后把对应的列作为字段列,行表示每行的值,这样就可以做到任意取值了。 首推POI,这里有详细的使用说明http://jakarta.apache.org/poi/ 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(); } }} 我毕设的时候用的POI,很简单的在http://jakarta.apache.org/poi/有例子和详细的文档你把他的API搞懂了就可以用了 mysql数据截断 问题 extjs GridPanel解析不了struts2返回的json数据,郁闷! 下拉框提交本页面后下拉框里面的值保持提交前的怎样去做? 几个关于J2EE框架下的web应用问题。 急! 急! 急!跪求正则表达式高手帮忙!!! 关于ActionMessage的初始化 关于1个web service 问题 strus2 中 select 标签 的传值问题 MS client 和tomcat是不是有冲突? WEBService紧急问题? 用hibernate保存表格内数据的问题 关于Hibernate和mysql的问题
路过.....友情up.....//--------------------送个笑话-------------------
一位迷人的女郎夏天开着车子到全国旅行,由于天气实在太热,她全身已经香汗淋漓。开到某个乡下地方时,她看到一座水池,于是她决定停车游个水,凉凉身,她脱光跳进水中,享受几分钟的清凉后,突然发现两位农夫躲在树丛下偷看。由于她的衣服摆在水池的另一边,不过靠近她身边有个澡盆,于是她拿起澡盆遮住身子,往那两位农夫走去。
“你们两个王八蛋,难道没别事好做吗?”她咆哮道,“你们知道我怎么想吗?”
“是的,女士。”个子较高的一位说,“你一定想问我们什么地方可以补你那浴盆上的大窟窿!” www.xiaohua007.com
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);
} }
}
还有一种方法,就是通过jdbc-odbc桥驱动连接,把它当作一种数据库类型进行对象化,然后把对应的列作为字段列,行表示每行的值,这样就可以做到任意取值了。
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();
}
}
}
在http://jakarta.apache.org/poi/有例子和详细的文档
你把他的API搞懂了就可以用了