POI导出excle,文件导出到本地
来源:互联网 发布:php时间戳转换工具 编辑:程序博客网 时间:2024/05/21 17:24
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;
import javax.annotation.Resource;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;
@Override
public String CreateExcelDemo(ArrayList list) throws Exception {
String fileName="";
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("学生表一");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int) 0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
//style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("序号");
cell = row.createCell((short) 1);
cell.setCellValue("姓名");
cell = row.createCell((short) 2);
cell.setCellValue("住址");
cell = row.createCell((short) 3);
for(int i=0;i<list.size();i++){
HashMap map =(HashMap)list.get(i);
// 第四步,创建单元格,并设置值
row = sheet.createRow((int) i+1);
HSSFCell celli = row.createCell((short) 0);
row.createCell((short) 0).setCellValue(i);
if(map.get("JGOBJECTNAME")!=null)
row.createCell((short) 1).setCellValue((String) map.get("JGOBJECTNAME"));
if(map.get("ZHUSUO")!=null)
row.createCell((short) 2).setCellValue((String) map.get("ZHUSUO"));
if(map.get("SHXYDM")!=null)
row.createCell((short) 3).setCellValue((String) map.get("SHXYDM"));
if(map.get("LXR")!=null)
row.createCell((short) 4).setCellValue((String) map.get("LXR"));
}
// 第六步,将文件存到指定位置
try {
fileName = "E:/"+UUID.randomUUID().toString()+".xls";
FileOutputStream fout = new FileOutputStream(fileName);
wb.write(fout);
fout.close();
wb.close();
} catch (Exception e) {
e.printStackTrace();
}
return fileName;
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;
import javax.annotation.Resource;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Override
public String CreateExcelDemo(ArrayList list) throws Exception {
String fileName="";
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("学生表一");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int) 0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
//style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("序号");
cell = row.createCell((short) 1);
cell.setCellValue("姓名");
cell = row.createCell((short) 2);
cell.setCellValue("住址");
cell = row.createCell((short) 3);
cell.setCellValue("班级");
cell = row.createCell((short) 4);
cell.setCellValue("年级");
for(int i=0;i<list.size();i++){
HashMap map =(HashMap)list.get(i);
// 第四步,创建单元格,并设置值
row = sheet.createRow((int) i+1);
HSSFCell celli = row.createCell((short) 0);
row.createCell((short) 0).setCellValue(i);
if(map.get("JGOBJECTNAME")!=null)
row.createCell((short) 1).setCellValue((String) map.get("JGOBJECTNAME"));
if(map.get("ZHUSUO")!=null)
row.createCell((short) 2).setCellValue((String) map.get("ZHUSUO"));
if(map.get("SHXYDM")!=null)
row.createCell((short) 3).setCellValue((String) map.get("SHXYDM"));
if(map.get("LXR")!=null)
row.createCell((short) 4).setCellValue((String) map.get("LXR"));
}
// 第六步,将文件存到指定位置
try {
fileName = "E:/"+UUID.randomUUID().toString()+".xls";
FileOutputStream fout = new FileOutputStream(fileName);
wb.write(fout);
fout.close();
wb.close();
} catch (Exception e) {
e.printStackTrace();
}
return fileName;
}
}
阅读全文
0 0
- POI导出excle,文件导出到本地
- excle表格导出到本地
- POI导出Excle HSSF
- poi导出excle
- POI导出excle数据
- POI根据模板导出Excle
- poi导出excle方式二
- Apache POI 导入导出EXCLE
- excle导出纵向表格(POI)
- 导出到excle
- C#导出Excle文件
- Php导出excle文件
- poi 实现Excel导出到本地
- SpringMVC 实现POI读取Excle文件中数据导入数据库(上传)、导出数据库中数据到Excle文件中(下载)
- Java导出数据到Excle
- .Net导出数据到Excle
- Apache poi导出word和Excle
- POI 方式-excle导出工具类实现
- 几种序列化协议(protobuf,xstream,jackjson,jdk,hessian)相关数据对比
- React基础(路由,组件,事件)
- 序列化表单
- swiper轮播插件出现Uncaught TypeError: Failed to execute 'observe' on 'MutationObserver': parameter 1 is no
- web.xml中url-pattern的3种写法
- POI导出excle,文件导出到本地
- Eclipse项目导入到Android Studio的过程和一些问题的解决
- 1080. Graduate Admission (30)
- Java随机产生4位验证码(数字、大小写字母组成)
- CCF考试——201612-1中间数
- java 实现统计字母个数,并输出最多的字母
- 回文子串 记忆化搜索(DP)
- poj 1681 Painter's Problem(高斯消元)
- Codevs1332上白泽慧音