jsp页面端

<a href="/portal/server/importExec" title="Data Download">
<img src="${pageContext.request.contextPath}/style/images/excel6.jpg" width=20px height=20px style="padding-top:15px"/>
</a>

controller端处理

     @RequestMapping(value = "importExec", method = RequestMethod.GET)
@ResponseBody
public void importExec(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
String fname = "serverlist";
response.reset();// 清空输出流
response.setHeader("Content-disposition","attachment; filename=" + fname + ".xls");// 设定输出文件头
response.setContentType("application/msexcel");//EXCEL格式 Microsoft excel
//创建workbook
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
HSSFFont f = workbook.createFont();
// f.setColor(HSSFColor.RED.index);
f.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);//加粗
style.setFont(f);
style.setFillForegroundColor(HSSFColor.LIME.index);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); //创建sheet页
HSSFSheet sheet = workbook.createSheet("Server Info.");
//创建单元格
HSSFRow row = sheet.createRow(0);
HSSFCell c0 = row.createCell(0);
c0.setCellValue(new HSSFRichTextString("No"));
c0.setCellStyle(style); HSSFCell c1 = row.createCell(1);
c1.setCellValue(new HSSFRichTextString("IP"));
c1.setCellStyle(style); HSSFCell c2 = row.createCell(2);
c2.setCellValue(new HSSFRichTextString("Server Name"));
c2.setCellStyle(style); HSSFCell c3 = row.createCell(3);
c3.setCellValue(new HSSFRichTextString("Server Usage"));
c3.setCellStyle(style); HSSFCell c4 = row.createCell(4);
c4.setCellValue(new HSSFRichTextString("CPU"));
c4.setCellStyle(style); HSSFCell c5 = row.createCell(6);
c5.setCellValue(new HSSFRichTextString("Memory"));
c5.setCellStyle(style); HSSFCell c6 = row.createCell(8);
c6.setCellValue(new HSSFRichTextString("HDD"));
c6.setCellStyle(style); HSSFCell c7 = row.createCell(11);
c7.setCellValue(new HSSFRichTextString("OS Version"));
c7.setCellStyle(style); HSSFCell c8 = row.createCell(12);
c8.setCellValue(new HSSFRichTextString("Manager"));
c8.setCellStyle(style); HSSFRow row1 = sheet.createRow(1);
HSSFCell c9 = row1.createCell(4);
c9.setCellValue(new HSSFRichTextString("Count"));
c9.setCellStyle(style);
HSSFCell c10 = row1.createCell(5);
c10.setCellValue(new HSSFRichTextString("Core Num."));
c10.setCellStyle(style);
HSSFCell c11 = row1.createCell(6);
c11.setCellValue(new HSSFRichTextString("Count"));
c11.setCellStyle(style);
HSSFCell c12 = row1.createCell(7);
c12.setCellValue(new HSSFRichTextString("Size (GB)"));
c12.setCellStyle(style);
HSSFCell c13 = row1.createCell(8);
c13.setCellValue(new HSSFRichTextString("Count"));
c13.setCellStyle(style);
HSSFCell c14 = row1.createCell(9);
c14.setCellValue(new HSSFRichTextString("Type"));
c14.setCellStyle(style);
HSSFCell c15 = row1.createCell(10);
c15.setCellValue(new HSSFRichTextString("Size (GB)"));
c15.setCellStyle(style);
sheet.addMergedRegion(new CellRangeAddress(0, 1,(short)0, (short)0));
sheet.addMergedRegion(new CellRangeAddress(0, 1,(short)1, (short)1));
sheet.addMergedRegion(new CellRangeAddress(0, 1,(short)2, (short)2));
sheet.addMergedRegion(new CellRangeAddress(0, 1,(short)3, (short)3));
sheet.addMergedRegion(new CellRangeAddress(0, 0,(short)4, (short)5));
sheet.addMergedRegion(new CellRangeAddress(0, 0,(short)6, (short)7));
sheet.addMergedRegion(new CellRangeAddress(0, 0,(short)8, (short)10));
sheet.addMergedRegion(new CellRangeAddress(0, 1,(short)11, (short)11));
sheet.addMergedRegion(new CellRangeAddress(0, 1,(short)12, (short)12)); List<Server> serverList = serverService.findServerList("");
for(int i=0;i<serverList.size();i++){
row=sheet.createRow((int)i+2);
Server server = (Server)serverList.get(i);
row.createCell((short)0).setCellValue(new HSSFRichTextString(i+1+""));
row.createCell((short)1).setCellValue(new HSSFRichTextString(server.getIp()));
row.createCell((short)2).setCellValue(new HSSFRichTextString(server.getName()));
row.createCell((short)3).setCellValue(new HSSFRichTextString(server.getUseFor()));
row.createCell((short)4).setCellValue(new HSSFRichTextString(String.valueOf(server.getCpuCount())));
row.createCell((short)5).setCellValue(new HSSFRichTextString(server.getCpuNumber()+""));
row.createCell((short)6).setCellValue(new HSSFRichTextString(server.getMemCount()+""));
row.createCell((short)7).setCellValue(new HSSFRichTextString(server.getMemSize()));
row.createCell((short)8).setCellValue(new HSSFRichTextString(server.getHddCount()+""));
row.createCell((short)9).setCellValue(new HSSFRichTextString(server.getHddType()));
row.createCell((short)10).setCellValue(new HSSFRichTextString(server.getHddSize()));
row.createCell((short)11).setCellValue(new HSSFRichTextString(server.getOsVersion()));
row.createCell((short)12).setCellValue(new HSSFRichTextString(server.getManager()));
} try{
workbook.write(response.getOutputStream());
}
catch (Exception e){
e.printStackTrace();
}
}

action的处理方法:

jsp端

 	 <a href="DownDefectServlet?projectname=<%=request.getParameter("projectname")%>&item=<%=request.getParameter("item")%>"  title="Data Download">
<img src="${pageContext.request.contextPath}/images/excel6.jpg" width=20px height=20px style="padding-top:15px"/>
</a>

web.xml加入

<servlet>
<servlet-name>DownDefectServlet</servlet-name>
<servlet-class>net.nw.servlet.DownDefectServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DownDefectServlet</servlet-name>
<url-pattern>/DownDefectServlet</url-pattern>
</servlet-mapping>

后台servlet处理

	public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { //int currpage = Integer.parseInt(request.getParameter("currpage")==null?"1":request.getParameter("currpage"));
String projectname=request.getParameter("projectname");
String item=request.getParameter("item");
String where=null;
int total=0;
if ("".equals(item)){
where="where PROJECTNAME like '%"+projectname+"%' AND STATUS != 'PLM_Deleted' and STATUS != 'Not_Related' and PLMFLAG='Y'" ;
}
else { //Opened
where="where PROJECTNAME like '%"+projectname+"%' AND STATUS != 'PLM_Deleted' AND STATUS != 'Closed' AND STATUS != 'Resolved' and STATUS != 'Not_Related' and PLMFLAG='Y'" ;
} response = ServletActionContext.getResponse();
String fname = "defectlist";
response.reset();// 清空输出流
response.setHeader("Content-disposition","attachment; filename=" + fname + ".xls");// 设定输出文件头
response.setContentType("application/msexcel");//EXCEL格式 Microsoft excel
//创建workbook
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
HSSFFont f = workbook.createFont();
f.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);//加粗
style.setFont(f);
style.setFillForegroundColor(HSSFColor.LIME.index);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); //创建sheet页
HSSFSheet sheet = workbook.createSheet("Defect List");
//创建单元格
HSSFRow row = sheet.createRow(0);
HSSFCell c0 = row.createCell(0);
c0.setCellValue(new HSSFRichTextString("ID"));
c0.setCellStyle(style);
HSSFCell c1 = row.createCell(1);
c1.setCellValue(new HSSFRichTextString("Headline"));
c1.setCellStyle(style);
HSSFCell c2 = row.createCell(2);
c2.setCellValue(new HSSFRichTextString("Priority"));
c2.setCellStyle(style);
HSSFCell c3 = row.createCell(3);
c3.setCellValue(new HSSFRichTextString("ModelCode"));
c3.setCellStyle(style);
HSSFCell c4 = row.createCell(4);
c4.setCellValue(new HSSFRichTextString("Sub Component Name"));
c4.setCellStyle(style);
HSSFCell c5 = row.createCell(5);
c5.setCellValue(new HSSFRichTextString("Plat. Dev."));
c5.setCellStyle(style);
HSSFCell c6 = row.createCell(6);
c6.setCellValue(new HSSFRichTextString("Prod. Dev."));
c6.setCellStyle(style);
HSSFCell c7 = row.createCell(7);
c7.setCellValue(new HSSFRichTextString("Defect Solved Ver."));
c7.setCellStyle(style);
HSSFCell c8 = row.createCell(8);
c8.setCellValue(new HSSFRichTextString("Requester"));
c8.setCellStyle(style);
HSSFCell c9 = row.createCell(9);
c9.setCellValue(new HSSFRichTextString("Status"));
c9.setCellStyle(style);
HSSFCell c10 = row.createCell(10);
c10.setCellValue(new HSSFRichTextString("State Owner"));
c10.setCellStyle(style); ResultSet rs=null; total=this.getResultCount_1(where);
rs = this.getResultSet_1(where);
int i = 0;
try {
while (rs.next()) {
row=sheet.createRow((int)++i);
row.createCell((short)0).setCellValue(new HSSFRichTextString(rs.getString("ID")));
row.createCell((short)1).setCellValue(new HSSFRichTextString(rs.getString("HEADLINE").replaceAll("<", " <").replaceAll(">", " >")));
row.createCell((short)2).setCellValue(new HSSFRichTextString(rs.getString("SERIOUSNESS").replaceAll("<", " <").replaceAll(">", " >")));
row.createCell((short)3).setCellValue(new HSSFRichTextString(rs.getString("MODELCODE").replaceAll("<", " <").replaceAll(">", " >")));
row.createCell((short)4).setCellValue(new HSSFRichTextString(rs.getString("SUBCOMPONENTNAME").replaceAll("<", " <").replaceAll(">", " >")));
row.createCell((short)5).setCellValue(new HSSFRichTextString(rs.getString("PLATFORMDEVELOPER").replaceAll("<", " <").replaceAll(">", " >")));
row.createCell((short)6).setCellValue(new HSSFRichTextString(rs.getString("PRODUCTDEVELOPER").replaceAll("<", " <").replaceAll(">", " >")));
row.createCell((short)7).setCellValue(new HSSFRichTextString(rs.getString("DEFECTSOLVEDVERSION").replaceAll("<", " <").replaceAll(">", " >")));
row.createCell((short)8).setCellValue(new HSSFRichTextString(rs.getString("REQUESTER").replaceAll("<", " <").replaceAll(">", " >")));
row.createCell((short)9).setCellValue(new HSSFRichTextString(rs.getString("STATUS").replaceAll("<", " <").replaceAll(">", " >")));
row.createCell((short)10).setCellValue(new HSSFRichTextString(rs.getString("STATEOWNER").replaceAll("<", " <").replaceAll(">", " >"))); }
rs.close();
}catch (SQLException e) {
System.out.println(e.getMessage());
} try {
workbook.write(response.getOutputStream());
}
catch (Exception e){
e.printStackTrace();
}
}

后台数据download成excel的方法(controller/action)的更多相关文章

  1. sqlserver将数据库的数据导成excel文档方法

    sqlserver将数据库的数据导成excel文档方法 最近公司需要下载uniport的数据跟之前的数据进行对比,所以避免不了需要将数据库的数据导出来,把SQLServer表中的数据导出为Excel文 ...

  2. Pl/sql 如何将oracle的表数据导出成excel文件?

    oracle将表数据导出成excel文件的方法 1)在SQL窗体上,查询需要导出的数据 --查询数据条件-- ; 结果视图 2)在查询结果的空白处,右键选择Copy to Excel 3) 查看导出e ...

  3. Echarts 数据视图 生成Excel的方法

    一.生成Excel,两大方向:1后台生成Excel 查询数据库,使用NOPI生成Excel.2前台js生成Excel三种方式1)jquery.table2excel.js --采用,优势:兼容IE和C ...

  4. Extjs — Grid数据导出成Excel

    最近因为项目问题,需要解决Extjs导出成Excel的问题. 下面简单描述这个问题解决的步骤如下: 1.先在js文件里写了一个button的handler事件,通过点击按钮,来实现调用ExportEx ...

  5. MySQL要导出成excel的方法

    MySQL 要导出成 excel 文件很简单,执行类似这样的命令:   select * from 某个表 into outfile  'd:/文件名.xls';   上述命令你在服务器上执行,就导在 ...

  6. php将数据库导出成excel的方法

    <?php $fname = $_FILES['MyFile']['name']; $do = copy($_FILES['MyFile']['tmp_name'],$fname); if ($ ...

  7. c# 数据导出成excel 方法总结 见标红部分

    public void ServiceOrderExport(string data) { StringBuilder sb = new StringBuilder(); Type entityTyp ...

  8. 使用SSM框架实现Sql数据导出成Excel表

    SSM框架实现SQL数据导出Excel 思路 首先在前端页面中添加一个导出功能的button,然后与后端controller进行交互. 接着在相应的controller中编写导出功能方法. 方法体: ...

  9. 有趣的Node爬虫,数据导出成Excel

    最近一直没更新了诶,因为学习Backbone好头痛,别问我为什么不继续AngularJs~因为2.0要出来了啊,妈蛋!好,言归正传,最近帮我的好基友扒数据,他说要一些股票债券的数据.我一听,那不就是要 ...

随机推荐

  1. 2014最热门、最具争议的10个Java话题

    Java 的哪些内容已在2014年死去,Java 的哪些变更又遭到整个Java社区的竭力反对?请随我们一起来回顾在2014年这个多事之秋中Java都发生了哪些变化,以及小伙伴们都在JAXenter热烈 ...

  2. 第一百零七节,JavaScript基本包装类型,数据类型的方法

    JavaScript基本包装类型,数据类型的方法 学习要点: 1.基本包装类型概述 2.Boolean类型 3.Number类型 4.String类型 为了便于操作基本类型值,ECMAScript提供 ...

  3. iOS开发QQ空间半透明效果的实现

    //1.首先我们可以确定的是cell的半透明, /* white The grayscale value of the color object, specified as a value from ...

  4. JUnit test case 执行顺序

    转自:JUnit中按照顺序执行测试方式 很多情况下,写了一堆的test case,希望某一些test case必须在某个test case之后执行.比如,测试某一个Dao代码,希望添加的case在最前 ...

  5. 文本变语音引擎 ekho

    https://github.com/donaldlee2008/ekho https://www.oschina.net/p/ekho

  6. 遍历(二)javascript的Foreach语法

    原文:http://www.cnblogs.com/Fskjb/archive/2011/03/26/1996165.html 首先,虽然叫foreach语法但关键字还是用for哦,这个语法只是对平时 ...

  7. POJ 1065 Wooden Sticks#贪心+qsort用法

    (- ̄▽ ̄)-* 这道题用到了cstdlib库的qsort()函数: 用法链接:http://www.cnblogs.com/syxchina/archive/2010/07/29/2197382.h ...

  8. 2DToolkit官方文档中文版打地鼠教程

    初始设置 创建一个Unity项目,并导入2D Toolkit插件. 导入完成后,在Project窗口会显示TK2DROOT文件夹(后续版本文件夹名称或许会有变动). 导入素材游戏,你可以从这里下载.下 ...

  9. ESPlatform 支持的三种群集模型 —— ESFramework通信框架 4.0 进阶(09)

    对于最多几千人同时在线的通信应用,通常使用单台服务器就可以支撑.但是,当同时在线的用户数达到几万.几十万.甚至百万的时候,我们就需要很多的服务器来分担负载.但是,依据什么规则和结构来组织这些服务器,并 ...

  10. 转载:Ubuntu下deb包的安装方法

    转载:Ubuntu下deb包的安装方法,http://blog.csdn.net/kevinhg/article/details/5934462 deb是debian linus的安装格式,跟red ...