java导入导出Excel数据的要点记录

《 客户端的B/S架构输出方式

 加下划线这部分代码是B/S模式中采用的输出方式,
 //获得输出流,该输出流的输出介质是客户端浏览器

  OutputStream output=response.getOutputStream();

  response.reset();

  response.setHeader("Content-disposition","attachment;           filename=temp.xls");

  response.setContentType("application/msexcel");》

1.导入jxl.jar的jar包

2.在导出数据主要要点

package com.wch.test;

import java.io.File;
import java.util.List;

import com.wch.entity.StuEntity;
import com.wch.service.StuService;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

/**
* mysql中的数据库导入到Excel中
*
* @author chen
*
*/
public class TestDbToExcel {

/**
* @param args
*/
public static void main(String[] args) {

try {
WritableWorkbook wwb = null;
// 创建本地可写入的文件夹
String fileName = "D://bookTest.xls";
File file = new File(fileName);
if (!file.exists()) {
file.createNewFile();
}
// 以fileName为文件名来创建一个Workbook
wwb = Workbook.createWorkbook(file);
// 创建工作表
WritableSheet ws = wwb.createSheet("测试从数据导入Excel", 0);
ws.mergeCells(0, 0, 3, 0);
// 创建WritableFont 字体对象,参数依次表示黑体、字号18、粗体、非斜体、不带下划线、亮蓝色

WritableFont titleFont = new WritableFont(
WritableFont.createFont("黑体"), 14, WritableFont.BOLD,
false, UnderlineStyle.NO_UNDERLINE, Colour.LIGHT_BLUE);

// 创建WritableCellFormat对象,将该对象应用于单元格从而设置单元格的样式

WritableCellFormat titleFormat = new WritableCellFormat(titleFont);//设置标题
// 设置文本水平居中对齐
titleFormat.setAlignment(Alignment.CENTRE);
// 设置文本垂直居中对齐
titleFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
// 设置自动换行
titleFormat.setWrap(true);
// 添加Label对象,参数依次表示在第一列,第一行,内容,使用的格式
Label lab_00 = new Label(0, 0, "员工薪资情况表", titleFormat);
// 将定义好的Label对象添加到工作表上,这样工作表的第一列第一行的内容为‘学员考试成绩一览表’并应用了titleFormat定义的样式
ws.addCell(lab_00);
// 查询数据库所有的数据
List<StuEntity> list = StuService.getAllByDb();
// 进行第一行标题的编写
Label labelId = new Label(0, 1, "编号");
Label labelName = new Label(1, 1, "姓名");
Label labelSex = new Label(2, 1, "性别");
Label labelNum = new Label(3, 1, "新水");
ws.addCell(labelId);
ws.addCell(labelName);
ws.addCell(labelSex);
ws.addCell(labelNum);
// 把数据库查询到的时间编写到Excel中去
for (int i = 0; i < list.size(); i++) {
Label labelId_i = new Label(0, i + 2, list.get(i).getId() + "");
Label labelName_i = new Label(1, i + 2, list.get(i).getName());
Label labelSex_i = new Label(2, i + 2, list.get(i).getSex());
Label labelNum_i = new Label(3, i + 2, list.get(i).getNum()
+ "");
ws.addCell(labelId_i);
ws.addCell(labelName_i);
ws.addCell(labelSex_i);
ws.addCell(labelNum_i);
}
// 写入文档里面
wwb.write();
// 关闭文档工作簿
wwb.close();
} catch (Exception e) {
e.printStackTrace();
}
}

}

2.吧Excel数据导入到数据库。

package com.wch.test;

import java.util.List;

import com.wch.entity.StuEntity;
import com.wch.service.StuService;
import com.wch.util.DBhepler;

public class TestExcelToDb {

/**
* @param args
*/
public static void main(String[] args) {
//得到表格中所有的数据
List<StuEntity> listExcel = StuService.getAllByExcel("D://bookTest.xls");

DBhepler db=new DBhepler();

for (StuEntity stuEntity : listExcel) {
int id=stuEntity.getId();
if (!StuService.isExist(id)) {
//不存在就添加
String sql="insert into stu (name,sex,num) values(?,?,?)";
String[] str=new String[]{stuEntity.getName(),stuEntity.getSex(),stuEntity.getNum()+""};
db.AddU(sql, str);
}else {
//存在就更新
String sql="update stu set name=?,sex=?,num=? where id=?";
String[] str=new String[]{stuEntity.getName(),stuEntity.getSex(),stuEntity.getNum()+"",id+""};
db.AddU(sql, str);
}
}

}

}

/*一个调用的方法

/**
* 查询指定目录中电子表格中所有的数据
* @param file 文件完整路径
* @return
*/
public static List<StuEntity> getAllByExcel(String file){
List<StuEntity> list=new ArrayList<StuEntity>();
try {
Workbook rwb=Workbook.getWorkbook(new File(file));
//得到创建工作表
Sheet rs=rwb.getSheet("测试从数据导入Excel");//或者rwb.getSheet(0)
int clos=rs.getColumns();//得到所有的列
int rows=rs.getRows();//得到所有的行

System.out.println(clos+" rows:"+rows);
for (int i = 2; i < rows; i++) {
for (int j = 0; j < clos; j++) {
//第一个是列数,第二个是行数
String id=rs.getCell(j++, i).getContents();//默认最左边编号也算一列 所以这里得j++
String name=rs.getCell(j++, i).getContents();
String sex=rs.getCell(j++, i).getContents();
String num=rs.getCell(j++, i).getContents();

System.out.println("id:"+id+" name:"+name+" sex:"+sex+" num:"+num);
list.add(new StuEntity(Integer.parseInt(id), name, sex, Integer.parseInt(num)));
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;

}

 

*/

posted @ 2017-02-08 11:49  伍春晖  阅读(1154)  评论(0编辑  收藏  举报