高分相送,java读取excel表格数据,急急急!!!

Rabbit_Lap 2006-09-06 05:28:15
在JAVA中如何读取excel表格数据?我只知道可以读取一个单元格或单元行,但如果数据保存在多列或多行的话,那该如何读取???
...全文
311 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
日总是我哥 2006-09-08
  • 打赏
  • 举报
回复
你指合并的行和列?
ilove8 2006-09-08
  • 打赏
  • 举报
回复
jf
TinyJimmy 2006-09-07
  • 打赏
  • 举报
回复
设计到多行多列都好, 数据总是属于某一个行列的, 在Excel的宏中读取的是什么值, POI读取的就是什么值
聪头 2006-09-07
  • 打赏
  • 举报
回复
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();
}
}
}
liuelite 2006-09-07
  • 打赏
  • 举报
回复
可 不可以进行一些运算呢?
Rabbit_Lap 2006-09-07
  • 打赏
  • 举报
回复
如果一数据保存在多列或多行,而这数据是中文的话,那你按一个行列来取值再组合,那就很可能存在乱码,当然对于英文或数字是不存在这样的问题的,可是我的excel表格中很多是中文,所以比较痛苦!!!不过还是很感谢各位的回帐,其中也得到了不少启发
  • 打赏
  • 举报
回复
sheet.getCell(i,j);
用这个你就可以读取任意一个单元格的内容
KelvenCheung 2006-09-07
  • 打赏
  • 举报
回复
我是用jxl,感觉上比poi好用点~
laixiaonian88 2006-09-06
  • 打赏
  • 举报
回复
我专门做excel处理的,我的博客上http://drivemewild.blogchina.com的文章可以帮的上你
ray_alex 2006-09-06
  • 打赏
  • 举报
回复
使用JXL
Rabbit_Lap 2006-09-06
  • 打赏
  • 举报
回复
是的,excle文档有这样的情况,单列单行知道如何处理
wizardblue 2006-09-06
  • 打赏
  • 举报
回复
你指合并的行和列?
gefengxztg 2006-09-06
  • 打赏
  • 举报
回复
没见过,学习+up
67676373 2006-09-06
  • 打赏
  • 举报
回复
jf

23,407

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧