java生成excel到本地_java导出excel至本地(非服务器)

这篇博客介绍了如何使用Java的POI库将数据导出为Excel文件,并将其保存到服务器的临时文件中,然后通过HTTP响应流让用户下载到本地。主要步骤包括创建Excel工作簿,设置单元格样式,填充数据,最后将文件写入到指定路径,并提供下载链接。

思路:将excle文件导出至服务器的临时文件中,然后再读取excel文件,输出到response流中下载到本地。

第一步:将数据导出到服务器临时文件中:

public static void main(String[] args) {

// TODO Auto-generated method stub

//添加excel

HSSFWorkbook wb=new HSSFWorkbook();

HSSFSheet sheet=wb.createSheet();//创建sheet表

wb.setSheetName(0,"学生表",HSSFWorkbook.ENCODING_UTF_16);//参数分别为sheet表索引值,sheet表的名字,处理中文问题用的编码

//合并单元格,Region(起始行号,起始列号,终止行号,终止列号)即起始单元格,终止单元格

//列号限制为short型

sheet.addMergedRegion(new Region(0,(short)0,0,(short)3));

HSSFCellStyle style=wb.createCellStyle();

style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//创建居中样式,更多单元格格式设置百度“POI导出Excel”

//title标题部分

HSSFRow row=sheet.createRow(0);//导入第一行

HSSFCell cell =row.createCell((short)0);//导入第一单元格

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

cell.setCellValue("学生表");

cell.setCellStyle(style);//单元格添加居中样式

//head表头部分

row=sheet.createRow(1);

cell=row.createCell((short)0);

cell.setCellValue("id");

row.createCell((short)1).setCellValue("name");

row.createCell((short)2).setCellValue("age");

cell=row.createCell((short)3);

cell.setEncoding(HSSFCell.ENCODING_UTF_16);//中文乱码问题,只对当前单元格起作用,目前我还不知道有什么一劳永逸的办法

cell.setCellValue("生日");

//cell.setCellValue("name");

//cell.setCellValue("age");

//cell.setCellValue("birth");

//body数据部分,写法与表头设置相同,这里不写了

//导出

try{

//创建文件夹

File file=new File("F:/student");

if(!file.exists() && !file.isDirectory()){

//当文件夹不存在或者不是文件夹时创建文件夹

file.mkdir();

}

//FileOutputStream out=new FileOutputStream("F:/student.xls");

//wb.write(out);//导出至服务器中

//out.close();

//System.out.println("导出完毕!");

}catch(Exception e){

e.printStackTrace();

}

}

}

第二步:导出到本地:

File file =new File("F:/student/student.xls");

if(file.exists && !file.isDirectory()){

//response.reset();//这句话查资料的时候好多都有写,但是我添加项目中时报错

response.setContentType("application/octet-stream");

try{

String excelName=URLEncoder.encode("学生表.xls","utf-8");//下载文件名称(客户端弹出保存窗口时看到的(默认的保存名称))

response.setHeader("Content-Disposition","attachment; filename=excelName);

}catch(Exception e){

e.printStackTrace();

}

response.setContentLength((int)file.length());

byte buffer[]=new byte[4096];

BufferedOutputStream out=null;

BufferedInputStream in=null;

//写缓冲区

try{

out=new BufferedOutputStream(response.getOutpStream());//response.getOutpStream()可能会报错,但是不影响代码运行,网上查资料貌似是tomcat6的bug

in=new BufferedInputStream(new FileInputStream(file));

int n=(-1);

while((n=in.read(buffer,0,4096))){

out.write(buffer,0,n);

}

reponse.flushBuffer();

}catch(Exception e){

e.printStackTrace();

}finally{

if (input != null)

try {

input.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

if (output != null)

try {

output.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

至此就可以将excel文件导出至本地了,另外导出到本地的部分还可以用于导出.txt文件。

整理这些资料借鉴了很多大神的博客和文章,做完项目都已经忘了他们的地址,这里只能表示感谢了!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值