如何用java导入Excel数据到数据库
2016-08-12 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
参考下面代码:
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());
}
}
}
}
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());
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |