基于easyui利用poi技术实现导出,导入,导出text功能
来源:互联网 发布:js typeof5类型 编辑:程序博客网 时间:2024/05/17 08:49
首先是针对前端的利用ajax请求,我这里用的是play框架做的
<a href="#" class="easyui-linkbutton" id="id_cancel " onclick="AutomateExcel();" iconcls="icon-print" plain="true">导出Excel</a>
function AutomateExcel() {
$.ajax({
type: "POST",
url: "index8",
success: function (data) {
if (data == "1") {
alert("导出成功");
} else if (data == "-1") {
alert("导出失败");
}
}
});
}
然后进入相关后台进行相关的操作
建议了解下poi和excel的相关知识i\
以下是单表进行相关的操作,对于多表操作就是list保存的时候改为利用MAP来储存,根据他的key得到相关的value值,其他的都一样;
public static void index8(){
//单表导出
try{
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheets
HSSFSheet sheet = wb.createSheet("人员信息");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int) 0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
//设置列的宽度
sheet.setColumnWidth(4, 3000);
sheet.setColumnWidth(5, 3000);
sheet.setColumnWidth(8, 3000);
sheet.setColumnWidth(9, 3000);
for(int j =1;j<8;j++){
sheet.setColumnWidth(9+j, 3500);
}
HSSFCell cell = row.createCell(0);
cell.setCellValue("编号");
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue("姓名");
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellValue("密码");
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue("性别");
cell.setCellStyle(style);
cell = row.createCell(4);
cell.setCellValue("年龄");
cell.setCellStyle(style);
cell = row.createCell(5);
cell.setCellValue("住址");
// 第五步,写入实体数据 实际应用中这些数据从数据库得到,
List<Look> lists = Look.findAll();
for(int i =0;i<lists.size();i++){
row = sheet.createRow((int) i + 1);
// 第四步,创建单元格,并设置值
String no = lists.get(i).no;
String name = lists.get(i).name;
String pwd = lists.get(i).pwd;
String sex = lists.get(i).sex;
String age = lists.get(i).age;
String address = lists.get(i).address;
row.createCell(0).setCellValue(no);
row.createCell(1).setCellValue(name);
row.createCell(2).setCellValue(pwd);
if("1".equals(sex)){
row.createCell(3).setCellValue("男");
}else{
row.createCell(3).setCellValue("女");
}
row.createCell(5).setCellValue(age);
row.createCell(6).setCellValue(address);
}
// 第六步,将文件存到指定位置
FileOutputStream fout = new FileOutputStream( "C:/cc/workbook1.xls");
wb.write(fout);
fout.close();
renderText("1");
} catch (Exception e) {
e.printStackTrace();
renderText("-1");
}
}
针对导入excel和导出是大同小异的
//导入excel
public static void main(String[] args) throws IOException{
String filePath = "C:/cc/workbook1.xls";
//判断是否为excel类型文件
if(!filePath.endsWith(".xls")&&!filePath.endsWith(".xlsx"))
{
System.out.println("文件不是excel类型");
}
//文件输入流
FileInputStream fis =null;
//创建Workbook 对应一个EXCEL文件
Workbook wookbook = null;
try
{
//获取一个绝对地址的流
fis = new FileInputStream(filePath);
}
catch(Exception e)
{
e.printStackTrace();
}
try
{
//2003版本的excel,用.xls结尾
wookbook = new HSSFWorkbook(fis);//得到工作簿
}
catch (Exception ex)
{
//ex.printStackTrace();
try
{
//2007版本的excel,用.xlsx结尾
wookbook = new XSSFWorkbook(fis);//得到工作簿
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//得到一个工作表
Sheet sheet = wookbook.getSheetAt(0);
//获得表头
Row rowHead = sheet.getRow(0);
//判断表头是否正确
if(rowHead.getPhysicalNumberOfCells() != 3)
{
System.out.println("表头的数量不对!");
}
//获得数据的总行数
int totalRowNum = sheet.getLastRowNum();
//要获得属性
String name = "";
String latitude = "";
List<Look> list=null;
//获得所有数据
//获得第i行对象
Row row =null;
for(int i = 1 ; i <= totalRowNum ; i++)
{
//获得第i行对象
row = sheet.getRow(i);
//获得获得第i行第0列的 String类型对象
Cell cell = row.getCell((short)0);
name = i+cell.getStringCellValue().toString();
//获得一个数字类型的数据
cell = row.getCell((short)1);
latitude =i+ cell.getStringCellValue().toString();
System.out.println("读取行:" + row.getRowNum());
System.out.println("编号:"+name+",姓名:"+latitude);
// list=new ArrayList<Look>();
// Look a =new Look();
// a.no=name;
// a.name=latitude;
// list.add(a);
// }
// FileWriter output = new FileWriter("D:test1.txt");
// BufferedWriter bf = new BufferedWriter(output);
// for (Look l : list) {
// bf.write(l.no+"\t\n"+l.name);
// }
// bf.flush();// 此处很关键,如果不写该语句,是不能从缓冲区写到文件里的
//
}
}
以下为导出为text文件的相关内容,因为数据源很多,根据自己的需求进行相关操作
List<Look> lists = Look.findAll();
try {
FileWriter output = new FileWriter("D:test.txt");
BufferedWriter bf = new BufferedWriter(output);
for (Look l : lists) {
bf.write(l.no+"\t\n"+l.name+"\t\n"+l.pwd+l.sex+"\t\n"+l.address);
}
bf.flush();// 此处很关键,如果不写该语句,是不能从缓冲区写到文件里的
renderText("1");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
renderText("-1");
}
}
- 基于easyui利用poi技术实现导出,导入,导出text功能
- 利用POI实现Excel的导入导出
- POI技术实现EXCEL的导入导出
- POI实现导入导出
- JAVA利用POI实现excel的导入导出
- Poi简单实现Excel的导出导入功能
- 利用Java反射机制和POI实现导出Excel功能
- 利用Java反射机制和POI实现导出Excel功能
- spring+springmvc+hibernate利用poi实现导出Excel功能
- POI实现动态导出功能
- javaweb项目基于poi的excel导出功能实现
- 利用POI组件 导入导出excel
- 利用POI导入和导出excel文件
- java 利用POI 导入导出初步学习
- 利用poi 导入导出excel文件
- 导入/导出Excel利用apache.poi
- 基于POI操作Excel的导入导出
- 利用poi实现页面上excel的导入和导出(不刷新页面)之导出
- 阻塞IO和非阻塞IO的区别(转载)
- mysql远程无法连接数据库的问题
- 二分查找
- JFinal -spring整合的代码
- spark maxResultSize
- 基于easyui利用poi技术实现导出,导入,导出text功能
- 5.常见概念和问题
- Unity5.6大规模地形—地形资源的创建(2)
- c++之智能指针
- 手机变身高清摄像头 DroidCamX完全使用教程
- 第十二单元练习题
- Oracle 12C 新特性之 PDB热克隆(本地克隆、远端异机克隆)
- 冒泡排序--对数组中的元素进行降序排列
- MFC使用定时器