下面是我写的一个运用jxl生成Excel文件的方法
该方法接收一个保存MarkesData数据的ArrayList arlist和文件生成路径7a64e4b893e5b19e339Path
通过取得arlist里的数据生成Excel文件
感觉应该是你想要的 我没调试过也许有错 你也可以根据你的需要进行修改^_^
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class WriteExcel {
WritableWorkbook book=null;
public void OutputExcel(ArrayList arlist,String Path){
try{
book = Workbook.createWorkbook(new File(Path));
//设置表名
WritableSheet sheet = book.createSheet("考试单",0);
//生成表格题头
Label labe1 = new Label(0, 0, "考生姓名" );
Label labe2 = new Label(1, 0, "地区");
Label labe3 = new Label(2, 0, "所属院校");
Label labe4 = new Label(3, 0, "班级");
Label labe5 = new Label(4, 0, "考试号");
Label labe6 = new Label(5, 0, "考试时间");
Label labe7 = new Label(6, 0, "科目名称");
//将生成的单元格添加到工作表中
sheet.addCell(labe1);
sheet.addCell(labe2);
sheet.addCell(labe3);
sheet.addCell(labe4);
sheet.addCell(labe5);
sheet.addCell(labe6);
sheet.addCell(labe7);
Iterator it = arlist.iterator();
int i = 1;
while(it.hasNext()){
//通过迭代获得arlist里的MarkesData对象
MarkesData temp = (MarkesData)it.next();
//取得数据生成单元格
Label label1=new Label(0,i,temp.getUser_name());
Label label2=new Label(1,i,temp.getArea_name());
Label label3=new Label(2,i,temp.getCollege_name());
Label label4=new Label(3,i,temp.getClass_name());
Label label5=new Label(4,i,temp.getTest_name());
Label label6=new Label(5,i,temp.getStarttime());
Label label7=new Label(6,i,temp.getSubject_name());
//将生成的单元格添加到工作表中
sheet.addCell(label1);
sheet.addCell(label2);
sheet.addCell(label3);
sheet.addCell(label4);
sheet.addCell(label5);
sheet.addCell(label6);
sheet.addCell(label7);
i++;
}
book.write();
book.close();
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try{
if(book!=null)book.close();
}catch(Exception e){
System.out.println("exception when closing Connection in finally");
System.out.println(e.getMessage().toString());
}
}
}
}

jxl.jar 虽然过迟了,但是好用
这个我会留下你的企蛾号

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.sql.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import jxl.*;
public class SimUpdate {
private String fileName;
public ZfzSimUpdate(String fileName){
this.fileName = fileName;
}
static Map tNames;
static{
tNames = new HashMap();
}
private static String getDtime() {
String rid;
Date nd = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
rid = sdf.format(nd);
return rid;
}
public String getSeqNumber(String tableName) {
if(tableName == null || "".equals(tableName))
tableName = "GENERY";
Integer it;
// noinspection SynchronizeOnNonFinalField
synchronized(tNames){
it = (Integer)tNames.get(tableName);
if(it == null){
it = new Integer(100);
tNames.put(tableName, it);
}else{
if(it.intValue() > 998)
it = new Integer(100);
else
it = new Integer(1 + it.intValue());
tNames.put(tableName, it);
}
}
return getDtime() + String.valueOf(it);
}
private void updateDb(){
try{
Connection conn = DbPool.connectDB();
if(conn != null){
Statement stmt = conn.createStatement();
jxl.Workbook rwb = null;
try{
//构建Workbook对象 只读Workbook对象
//直接从本地文件创建Workbook
//从输入流创建Workbook
InputStream is = new FileInputStream(fileName);
rwb = Workbook.getWorkbook(is);
//Sheet(术语:工作表)就是Excel表格左下角的Sheet1,Sheet2,Sheet3但在程序中
//Sheet的下标是从0开始的
//获取第一张Sheet表
Sheet rs = rwb.getSheet(0);
//获取Sheet表中所包含的总列数
int rsColumns = rs.getColumns();
//获取Sheet表中所包含的总行数
int rsRows = rs.getRows();
//获取指这下单元格的对象引用
String simNumber = "",termSeqId = "";
//指定SIM卡号及序列号
for(int i=0;i<rsRows;i++){
for(int j=0;j<rsColumns;j++){
Cell cell = rs.getCell(j,i);
if(j==0){
simNumber = cell.getContents();
}
termSeqId = "633"+simNumber;
}
String sql = "查询SQL";
int isOk = stmt.executeUpdate(sql);
if(isOk == 0 && !simNumber.equals("")){
String termId = getSeqNumber("termInf");
String insertSql = "自定义INSERT";
int isAdd = stmt.executeUpdate(insertSql);
if(isAdd > 0){
System.out.println("成功插入第"+i+"条数据");
}
}
//System.out.println("SIM卡号:"+simNumber+",序列号:"+termSeqId);
}
//以下代码为写入新的EXCEL,这里不使用,所以注释
}catch(Exception e){
e.printStackTrace();
}
finally{
//操作完成时,关闭对象,翻译占用的内存空间
rwb.close();
}
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String args[]){
DbPool dbPool = new DbPool("dbConn.cfg");//连接数据库
SimUpdate simUpdate = new SimUpdate("zfz_sim.xls");
simUpdate.updateDb();
}
}
我只用了读取XLS,写入没试,应该没问题吧,你把注释了的拿 来试一下吧
评论已关闭!