java导出到excel工具类


直接上代码,解释下参数,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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值