`
beisicao
  • 浏览: 65875 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

导出excel

 
阅读更多
<%@page import="java.util.*"%>
<jsp:directive.page import="org.apache.poi.hssf.usermodel.HSSFWorkbook"/>
<jsp:directive.page import="org.apache.poi.hssf.usermodel.HSSFSheet"/>
<jsp:directive.page import="org.apache.poi.hssf.usermodel.HSSFRow"/>
<jsp:directive.page import="org.apache.poi.hssf.usermodel.HSSFCell"/>
<jsp:directive.page import="org.apache.poi.hssf.usermodel.HSSFCellStyle"/>
<jsp:directive.page import="org.apache.poi.hssf.util.HSSFColor"/>
<jsp:directive.page import="org.apache.poi.hssf.usermodel.HSSFFont"/>

<%
try
{
List list = (List)request.getAttribute("list");
if(list == null) list = new ArrayList();

         response.reset();
response.setContentType("application/vnd.ms-excel;charset=GBK");
   response.setCharacterEncoding("GBK");
response.setHeader("Content-Disposition","attachment;filename=\"Export.xls\"");
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("HWM_HJ");
HSSFCellStyle style = wb.createCellStyle();
HSSFFont  ft = wb.createFont();
ft.setColor(HSSFColor.RED.index);
style.setFont(ft);
HSSFRow headerRow = sheet.createRow(0);
HSSFCell headerCell0 =  headerRow.createCell((short)0);
headerCell0.setCellValue("Item");
HSSFCell headerCell1 =  headerRow.createCell((short)1);
headerCell1.setCellValue("PartNo");
HSSFCell headerCell2 =  headerRow.createCell((short)2);
headerCell2.setCellValue("HWM date");
HSSFCell headerCell3 =  headerRow.createCell((short)3);
headerCell3.setCellValue("HWM");
HSSFCell headerCell4 =  headerRow.createCell((short)4);
headerCell4.setCellValue("Shipped Qty");
HSSFCell headerCell5 =  headerRow.createCell((short)5);
headerCell5.setCellValue("Gross use");
HSSFCell headerCell6 =  headerRow.createCell((short)6);
headerCell6.setCellValue("Liability");
if(list!=null && list.size()>0){
          for(int i=0;i<list.size();i++){
          HWMHJ obj = (HWMHJ)list.get(i);
            
              HSSFRow lineRow = sheet.createRow(i+1);
              HSSFCell lineCell0 = lineRow.createCell((short)0);
              lineCell0.setCellValue(obj.getItem());
              HSSFCell lineCell1 = lineRow.createCell((short)1);
              lineCell1.setCellValue(obj.getPartNo());
              HSSFCell lineCell2 = lineRow.createCell((short)2);
              lineCell2.setCellValue(obj.getHwmName());
              HSSFCell lineCell3 = lineRow.createCell((short)3);
              lineCell3.setCellValue(obj.getHwmQty());
              HSSFCell lineCell4 = lineRow.createCell((short)4);
  lineCell4.setCellValue(obj.getShippedQty());
              HSSFCell lineCell5 = lineRow.createCell((short)5);
              lineCell5.setCellValue(obj.getGrossUse());
              HSSFCell lineCell9 = lineRow.createCell((short)6);
              lineCell9.setCellValue(obj.getLiability());
           }
        }
        wb.write(response.getOutputStream());
response.getOutputStream().flush();
response.getOutputStream().close();
   
   }catch(Exception e)
{
e.printStackTrace();
throw new HSException("Export xls error!" + e.getMessage());
}
%>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics