java导出到excel工具类
来源:互联网 发布:数控车床车圆球编程 编辑:程序博客网 时间:2024/06/05 20:19
直接上代码,解释下参数,str1的格式为"姓名=年龄=分数" list的格式为List<Object[]>注意:你如果想拿过去直接用工具类这里的集合必须放object[]数组
str1 是你的excel 的列名代表有三列 :姓名、年龄、分数
list为一行的数据,用object[]装载 ,比如我有 小米 5 90分 就用一个object 数组把这三个装进去 然后加入list
直接就可以用的工具类
需要的jar包我这边也会上传上去自行下载
public void test123(String tr1,List<Object> list) {
dbhe db=new dbhe();List<Object> l1=new ArrayList<Object>();
List<Object> excuteUpdate1 = db.excuteUpdate("select log_type,opt_ip,opt_user from sys_log", l1);
List<Object> excuteUpdate2=excuteUpdate1;
List<Object> excuteUpdate=list;
System.out.println(excuteUpdate.get(0));
long l=System.currentTimeMillis();
List ll=new ArrayList<Object>();
Iterator<Object> it=excuteUpdate.iterator();
System.out.println("总记录行数:"+ll.size());
//内存中保留 100 条数据,以免内存溢出,其余写入 硬盘
SXSSFWorkbook workbook = new SXSSFWorkbook(100);
// 生成一个表格
SXSSFSheet sheet = workbook.createSheet("基本信息");
// 设置表格默认列宽度为15个字节
sheet.setDefaultColumnWidth((short) 15);
// 产生表格标题行
SXSSFRow row = sheet.createRow(0);
String header[]= tr1.split("=");
for (int i = 0; i < header.length; i++) {
row.createCell(i).setCellValue(new XSSFRichTextString(header[i]));
}
int allcount=1;
int j=1;
int forall=1;
String sus="";
while(it.hasNext()){
row = sheet.createRow(allcount);
Object obj[]=(Object[]) it.next();
int i=0;
while(true) {
forall++;
Object value=obj[i];
/**************开始***********判断value 的格式并且赋值给 textvalue*********************************/
SXSSFCell cell = row.createCell(i);
try {
// 判断值的类型后进行强制类型转换
String textValue = null;
if (value instanceof Date) {
textValue = new SimpleDateFormat("yyyy-MM-dd").format((Date) value);
}else {
// 其它数据类型都当作字符串简单处理
if(value!=null){
textValue = value.toString();
}else{
textValue="";
}
}
// 如果不是图片数据,就利用正则表达式判断textValue是否全部由数字组成
if (textValue != null) {
if ( Pattern.compile("^//d+(//.//d+)?$").matcher(textValue).matches()) {
// 是数字当作double处理
cell.setCellValue(Double.parseDouble(textValue));
} else {
cell.setCellValue(new XSSFRichTextString(textValue));
}
}
} catch (Exception e) {
sus="导出数据错误!请联系 管理员!";
e.printStackTrace();
} finally {
}
i++;
if(i==obj.length){
value=null;
cell=null;
break;
}
}
allcount++;
/***************************************行数等于1000的时候把数据刷入缓存**********************************************/
if(allcount%50000==0){
allcount=1;
// 生成一个表格
sheet = workbook.createSheet();
// 产生表格标题行
row = sheet.createRow(0);
for (int ji = 0; ji < header.length; ji++) {
row.createCell(ji).setCellValue(new XSSFRichTextString(header[ji]));
}
}
/***************************************行数等于1000的时候把数据刷入缓存**********************************************/
if(allcount%1000==0){
System.out.println("共执行了:"+forall+"次");
System.out.println("成功 刷入 "+j+"次"+" 总刷入行数:"+allcount+" 总执行时长:"+(System.currentTimeMillis()-l)/1000);
try {
sheet.flushRows(1000);
} catch (IOException e) {
sus="导出数据错误!请联系 管理员!";
e.printStackTrace();
}
j++;
}
row=null;
//System.out.println("共执行行:"+allcount+"次");
}
String filePath= "d://testExcel";
File f=new File(filePath);
if(!f.exists()){
if(f.isFile()){
f.delete();
}else{
f.mkdir();
}
}
String newName="export.xlsx";
filePath+="/"+newName;
f=new File(filePath);
if(f.exists()){
f.delete();
}else{
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
ZipSecureFile.setMinInflateRatio(0l);
FileOutputStream out=null;
Map<String, String> mapall=new HashMap<String, String>();
boolean flag;
try {
out = new FileOutputStream(filePath);
workbook.write(out);
sus="导出总条数="+ll.size();
mapall.put("name", newName);
flag=true;
} catch (IOException e) {
sus="数据错误!请联系 管理员!";
e.printStackTrace();
}finally {
try {
out.flush();
out.close();
workbook.dispose();
workbook.close();
} catch (IOException e) {
sus="数据错误!请联系 管理员!";
e.printStackTrace();
}
}
}
阅读全文
1 0
- java导出到excel工具类
- Java导出信息到Excel的工具类
- java导入导出数据到excel的工具类
- Java导出Excel工具类
- Java导出Excel工具类
- java导出Excel工具类
- java导出excel工具类
- java excel导出工具类
- java excel导出工具类
- java导出excel工具类
- 导出数据到excel工具类
- Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类
- java导出excel,导入excel,导出csv工具类整理
- 导出到excel文件工具
- Excel导出到文件工具
- java 导出Excel文档工具类
- java工具类之导出Excel
- java 导入导出Excel工具类ExcelUtil
- Java并发编程:volatile关键字解析
- 虚函数与普通成员函数的区别
- 《挑战程序设计竞赛》P87 题目:Find them, Catch them Poj 1703 并查集
- java实现 单点登录SSO
- django timezone datetime 时间转换
- java导出到excel工具类
- 51 nod 数数字
- 全本 | iKcamp翻译 | 《JavaScript 轻量级函数式编程》|《你不知道的JS》姊妹篇
- 首次Android Studio中Git的配置和踩到的坑
- abs在linux 与windows 的不同
- 4种方法让SpringMVC接收多个对象(转:http://blog.csdn.net/lutinghuan/article/details/46820023)
- Xcode 9 上传ipa包异常
- 我热爱编程,但厌恶这个行业
- 轮播图(1)