JSP导出Excel文件的方法_JSP编程

本文实例讲述了JSP导出Excel文件的方法。分享给大家供大家参考,具体如下:

<%@page import="jxl.Workbook,com.ecc.emp.core.*,com.ecc.emp.data.*,com.ecc.emp.jdbc.ConnectionManager,jxl.format.VerticalAlignment,java.sql.*,jxl.write.*,jxl.format.UnderlineStyle,javax.sql.DataSource"%>
<%@page language="java" contentType="text/html; charset=UTF-8"%>
<%@taglib uri="/WEB-INF/CTP.tld" prefix="ctp"%>
<jsp:directive.page import="java.util.List"/>
<jsp:directive.page import="java.util.ArrayList"/>
<%@ include file="http://www.xxxx.com/head.jsp" %>
<%
 Context context = (Context)request.getAttribute("context");String flag ="交易日期,交易时间,摘要,借方发生额,贷方发生额,对方账号,对方户名,单位卡号,主卡标识,用途,备注";
 IndexedCollection indexedCollection = (IndexedCollection)context.getDataElement("parentCardDetails");
 String[] sourceStrArray=flag.split(",");
 List selValList = new ArrayList();
 selValList.add("序号#");
 for(int i=0;i<sourceStrArray.length;i++){
  selValList.add(sourceStrArray[i]+"#");
 }
 WritableWorkbook book = null;
 if (indexedCollection == null || indexedCollection.size()<0)
 {%>
 <script language="javascript">
  alert('对不起!当前条件没有数据,不能导出!');
  //history.go(-1);
 </script>
 <%
 }
 else
 {
  response.reset();
  response.setContentType("charset=UTF-8");
  response.setContentType("application/vnd.ms-excel");
  String filename = "regularInform.xls";
  response.setHeader("Content-Disposition","attachment; filename=" + filename);
  //解决HTTPS不能下载的问题
  response.setHeader("Cache-Control","public");
  book = Workbook.createWorkbook(response.getOutputStream());
  //表头字体
  WritableCellFormat writableCellFormat = new WritableCellFormat();
  writableCellFormat.setAlignment(Alignment.CENTRE);
  writableCellFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
  writableCellFormat.setWrap(true);
  WritableFont cellFont = new WritableFont(WritableFont.createFont("宋体"), 14, WritableFont.BOLD, false,
  UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
  writableCellFormat.setFont(cellFont);
  writableCellFormat.setBorder(Border.ALL,
  BorderLineStyle.THIN);
  writableCellFormat.setBackground(Colour.SKY_BLUE,
  Pattern.SOLID);
  //标题字体(左对齐)
  WritableCellFormat writableCellFormat1 = new WritableCellFormat();
  writableCellFormat1.setAlignment(Alignment.CENTRE);
  writableCellFormat1.setVerticalAlignment(VerticalAlignment.CENTRE);
  writableCellFormat1.setWrap(false);
  WritableFont cellFont1 = new WritableFont(WritableFont.createFont("宋体"), 14, WritableFont.NO_BOLD, false,
  UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
  writableCellFormat1.setFont(cellFont1);
  //数据字体(居中)
  WritableCellFormat writableCellFormat2 = new WritableCellFormat();
  writableCellFormat2.setAlignment(Alignment.CENTRE);
  writableCellFormat2.setVerticalAlignment(VerticalAlignment.CENTRE);
  writableCellFormat2.setWrap(true);
  WritableFont cellFont2 = new WritableFont(WritableFont.createFont("宋体"), 10, WritableFont.NO_BOLD, false,
  UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
  writableCellFormat2.setFont(cellFont2);
  writableCellFormat2.setBorder(Border.ALL,BorderLineStyle.THIN);
  WritableSheet sheet = book.createSheet("Sheet1", 0);
  sheet = setSheetHeader(sheet, writableCellFormat, selValList);
  int startRow = 2;
  Label label1 = null;
  int m = 0;
  sheet.mergeCells(0, 0, selValList.size()-1, 0);
  if(indexedCollection!=null && indexedCollection.size()>0)
   {
      for (int i = 0; i < indexedCollection.size(); i++){
       KeyedCollection keyedCollection = (KeyedCollection) indexedCollection.get(i);
        String TranDate=(String)keyedCollection.getDataValue("TranDate");//交易日期
        String tt = (String)keyedCollection.getDataValue("TranTime");
        tt = tt.substring(0,2)+":"+tt.substring(2,4);//交易时间
        String narr=utb.nvl(keyedCollection.getDataValue("narr"), "--");//摘要
        String entrAmt1=utb.formatMoney("DEBIT".equals((String)(keyedCollection.getDataValue("DCFlag")))?((String)keyedCollection.getDataValue("entrAmt")):"0.00");//借方发生额
        String entrAmt2=utb.formatMoney("DEBIT".equals((String)(keyedCollection.getDataValue("DCFlag")))?"0.00":((String)keyedCollection.getDataValue("entrAmt")));//贷方发生额
        String OppAc=utb.nvl(keyedCollection.getDataValue("OppAc"), "--");//对方账号
        String OppacName=utb.nvl(keyedCollection.getDataValue("OppacName"), "--");//对方户名
        String TransCardNo=utb.nvl(keyedCollection.getDataValue("TransCardNo"), "--");//单位卡号
        String stdflag=(String)context.getDataValue("stdflag");
        String cardNo=(String)context.getDataValue("parentCardNo");
        String mainflag = "附";if(!"parent".equals(stdflag)){}else{mainflag = cardNo.equals((String)(keyedCollection.getDataValue("TransCardNo")))?"主":"附";}//主卡标识
        String Usage=utb.nvl(keyedCollection.getDataValue("Usage"), "--");//用途
        String ReMark=utb.nvl(keyedCollection.getDataValue("ReMark"), "--");//备注
       int k = 0;
       sheet.setRowView(startRow, 450);
       label1 = new Label(k, startRow, (startRow-1)+"", writableCellFormat2);
       sheet.addCell(label1);
       k++;
       //(列,行,值,格式)
       label1 = new Label(k, startRow,TranDate, writableCellFormat2);
       sheet.addCell(label1);
       k++;
       label1 = new Label(k, startRow,tt, writableCellFormat2);
       sheet.addCell(label1);
       k++;
       label1 = new Label(k, startRow,narr, writableCellFormat2);
       sheet.addCell(label1);
       k++;
       label1 = new Label(k, startRow,entrAmt1, writableCellFormat2);
       sheet.addCell(label1);
       k++;
       label1 = new Label(k, startRow,entrAmt2, writableCellFormat2);
       sheet.addCell(label1);
       k++;
       label1 = new Label(k, startRow,OppAc, writableCellFormat2);
       sheet.addCell(label1);
       k++;
       label1 = new Label(k, startRow,OppacName, writableCellFormat2);
       sheet.addCell(label1);
       k++;
       label1 = new Label(k, startRow,TransCardNo, writableCellFormat2);
       sheet.addCell(label1);
       k++;
       label1 = new Label(k, startRow,mainflag, writableCellFormat2);
       sheet.addCell(label1);
       k++;
       label1 = new Label(k, startRow,Usage, writableCellFormat2);
       sheet.addCell(label1);
       k++;
       label1 = new Label(k, startRow,ReMark, writableCellFormat2);
       sheet.addCell(label1);
       k++;
       startRow = startRow+1;
       }
       }
   book.write();
   try {
    book.close();
    response.flushBuffer();
   } catch (Exception e) {
    System.out.println("Excel is not exist!");
   }
   out = pageContext.pushBody();
  }
%><%!private static WritableSheet setSheetHeader(WritableSheet sheet,WritableCellFormat writableCellFormat, List selValList) throws Exception
 {
  int i = 0;
  int j = 1;
  Label label = new Label(i, j, "序号", writableCellFormat);
  sheet.addCell(label);
  sheet.setColumnView(i, "序号".length() * 4);
  for(int w =1; w < selValList.size(); w++)
  {
   label = new Label(w, j, ((String)selValList.get(w)).split("#")[0], writableCellFormat);
   sheet.addCell(label);
   sheet.setColumnView(w, ((String)selValList.get(w)).split("#")[0].length() * 5);
  }
  return sheet;
 }
%>

希望本文所述对大家JSP程序设计有所帮助。

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索jsp
, 导出
excel文件
jsp导出excel、jsp导出excel表格、jsp页面导出excel、jsp页面表格导出excel、jsp poi导出excel,以便于您获取更多的相关知识。

时间: 2024-12-02 13:51:21

JSP导出Excel文件的方法_JSP编程的相关文章

JSP下载服务器文件的方法_JSP编程

本文实例讲述了JSP下载服务器文件的方法.分享给大家供大家参考,具体如下: <%@page import="java.io.FileInputStream"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <%@page

Jsp中的table多表头导出excel文件具体实现_JSP编程

首先引入两份JS:copyhtmltoexcel.js以及 tableToExcel.js 复制代码 代码如下: /*  * 默认转换实现函数,如果需要其他功能,需自行扩展 * 参数: *      tableID : HTML中Table对象id属性值 * 详细用法参见以下 TableToExcel 对象定义   */function saveAsExcel(tableID){ var tb = new TableToExcel(tableID);  tb.setFontStyle("Cour

jsp导出excel文件的操作

问题描述 jsp导出excel文件的操作 jsp页面导出excel文件或者PDF文件应该是用什么技术,求指导~~ 解决方案 JSP导出Excel文件c# 导出Excel文件的操作示例操作本地文件的jsp 解决方案二: 导入导出excel一般用的jxl架包,可以在网上搜一下例子... 解决方案三: 网上导出EXCEL的例子很多的,可以搜一下

PHP导入导出Excel文件的方法

借助PHP-ExcelReader这个开源类,我们可以轻松地导入Excel文件数据,示例代码如下: PHP-ExcelReader下载地址:http://sourceforge.net/projects/phpexcelreader/ 例.导入Excel文件  代码如下 复制代码 <?php require_once 'Excel/reader.php'; $data = new Spreadsheet_Excel_Reader(); $data->setOutputEncoding('gbk

JSP实现从数据库导出数据到Excel下载的方法_JSP编程

本文实例讲述了JSP实现从数据库导出数据到Excel下载的方法.分享给大家供大家参考,具体如下: 关键代码: <%@ page contentType="application/msexcel" %> <% //response.setHeader("Content-disposition","inline; filename=videos.xls"); response.setHeader("Content-disp

JSP实现快速上传文件的方法_JSP编程

本文实例讲述了JSP实现快速上传文件的方法.分享给大家供大家参考.具体如下: 这里演示JSP不使用第三方库,实现快速上传文件的功能 1. FileUpload.java: package FileUpload; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import javax.servlet.ServletInputStream; /** * */ /*

jsp+jdbc实现连接数据库的方法_JSP编程

本文实例讲述了jsp+jdbc实现连接数据库的方法.分享给大家供大家参考.具体如下: 初次尝试JSP+jdbc,按照书上的例子折腾了半天,就是连不上数据库.于是在网上找材料,终于发现,老的jar包与新版数据库直接不兼容.于是下了新的数据库jdbc包,试了一下,果然搞定.这里,把这个程序跟大家共享下,程序实现了网页登录界面上提取用户名与密码,然后与数据库中用户名密码对应,从而决定程序是否通过登录. inc.jsp文件: <%@ page import="java.sql.Connection

JSP常见的文件操作小结_JSP编程

本文实例总结了JSP常见的文件操作.分享给大家供大家参考.具体如下: JSP中的文件操作:FILE类 String path=request.getRealPath("/");//传递参数"/"可以返回web应用根目录 String tmp_path=path+"tmp"; File f1=new File(tmp_path);//创建FILE类,指定路径为tmp_path f1.mkdir();//创建目录 File f2=new File(t

JSP页面传值乱码过滤方法_JSP编程

本文实例讲述了JSP页面传值乱码过滤方法.分享给大家供大家参考,具体如下: 1.过滤器类: package com.token.t1; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.serv