java导出excel
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java导出excel相关的知识,希望对你有一定的参考价值。
excel文件不是在数据库中,excel文件就在该java工程项目下的文件夹里面。。点击下载按钮,把这个文件导出到本地
参考技术A public String downModel(HttpServletRequest request, HttpServletResponse response) throws Exceptionresponse.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("UTF-8");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
String ctxPath = request.getSession().getServletContext().getRealPath("/") + "doc/";
String fileName = request.getParameter("fileName") == null ? "" : request.getParameter(
"fileName").trim();
String downLoadPath = ctxPath + fileName;
try
long fileLength = new File(downLoadPath).length();
/*response.setHeader("Content-disposition", "attachment; filename="
+ new String(fileName.getBytes("utf-8"), "ISO8859-1")); */
String agent= request.getHeader("USER-AGENT");System.out.println(agent);
if (null != agent && -1 != agent.indexOf("MSIE"))
response.addHeader("Content-Disposition","attachment; filename=\"" + java.net.URLEncoder.encode(fileName, "UTF-8").replace("+", " ") + "\"");
else if (null != agent && -1 != agent.indexOf("Firefox"))
response.addHeader("Content-Disposition","attachment; filename=\"" + new String(fileName.getBytes(), "iso8859-1") + "\"");
else
response.setHeader("Content-Disposition","attachment;filename="+new String(java.net.URLEncoder.encode(fileName,"utf-8").getBytes(),"iso8859-1"));
response.setHeader("Content-Length", String.valueOf(fileLength));
bis = new BufferedInputStream(new FileInputStream(downLoadPath));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length)))
bos.write(buff, 0, bytesRead);
catch (Exception e)
e.printStackTrace();
finally
if (bis != null)
bis.close();
if (bos != null)
bos.close();
return null;
差不多是这样,点击按钮时候走这个方法, path 跟 name那块需要根据你自己的情况改下。本回答被提问者采纳 参考技术B
你就是一个java本地程序,想要实现点击后另外一份的功能对吧?
JFileChooser fileChooser = new JFileChooser();fileChooser.setAcceptAllFileFilterUsed(false);
fileChooser.setAcceptAllFileFilterUsed(false);
System.out.println("操作 = " + fileChooser.showSaveDialog(mainWIndow));
File file = fileChooser.getSelectedFile();//
然后读入你自己的文件写出到file中去 参考技术C wb = Workbook.getWorkbook(file);//按路径获取excel表
Sheet sheet = wb.getSheet(0);//第一个工作簿
sheet.getCell(x,y).getContents())//获取x列y行的数据;
需要一个jxl架包 参考技术D 如过是WEB项目,你可以试着将其作为静态文件来处理,就像CSS和JS文件一样, 第5个回答 2015-02-25 <a href="/WebName/Files/nobody.xls" target="_blank">点我下载</a>追问
嗯 我试试 如果excel的名字不是只有一个是通过下拉框来选择的话,href怎么写呀?
以上是关于java导出excel的主要内容,如果未能解决你的问题,请参考以下文章