package cn.com.tesoft.servlet
import java.io.IOException
import java.io.PrintWriter
import java.util.Map
import java.util.HashMap
import _root_.scala.xml._
import java.io.File
import net.sf.jasperreports.engine._
import net.sf.jasperreports.engine.export._
import net.sf.jasperreports.engine.util.JRLoader
import net.sf.jasperreports.j2ee.servlets._
import net.sf.jasperreports.view.JasperViewer
import javax.servlet.ServletException
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import _root_.javax.naming.{Context, InitialContext}
import _root_.javax.sql.{ DataSource}
import jxl._
import jxl.write._
import java.io._
/**
* Servlet implementation class ViewReportServlet
*/
class ViewReportServlet extends HttpServlet {
override def doGet(request: HttpServletRequest , response: HttpServletResponse) {
doPost(request, response)
}
override def doPost(request: HttpServletRequest , response: HttpServletResponse) {
response.setContentType("application/vnd.ms-excel");
response.addHeader("Content-Disposition","attachment; filename=\"test!!!!.xls" + "\"");
//获取模板
val is: FileInputStream = new FileInputStream("C:\\Documents and Settings\\qiankan\\桌面\\SCDB_template.xls") //写入到FileInputStream
val rw: jxl.Workbook = jxl.Workbook.getWorkbook(is)
//输出
val os: OutputStream = response.getOutputStream()
val wwb: jxl.write.WritableWorkbook = Workbook.createWorkbook(os, rw)
val st: jxl.write.WritableSheet = wwb.getSheet(0)
//得到工作薄中的第一个工作表
val cells: Array[jxl.Cell] = st.getRow(1)
val list = DB.runQuery("select orgcode, orgname, max(pmspolicecount) pmspolicecount, sum(slscdbrs) slscdbrs, sum(rjsabase) rjsabase,sum(pbrs) pbrs, sum(cars) cars, sum(cars_sfbg) cars_sfbg, sum(cars_lg) cars_lg,sum(cars_xshj) cars_xshj, sum(cars_sj) cars_sj, sum(cal) cal, sum(bhcabase) bhcabase,sum(bhbqs) bhbqs, sum(bhbqs_sfbg) bhbqs_sfbg, sum(bhbqs_sj) bhbqs_sj, sum(bqsl) bqsl,sum(bhbqsbase) bhbqsbase, sum(bhpwz) bhpwz, sum(bhpwz_sfbg) bhpwz_sfbg, sum(bhpwz_sj) bhpwz_sj, sum(wzl) wzl, sum(phpwzbase) phpwzbase from pmsscdb where pmsyear = $P{year} and pmsmonth between $P{beginMonth} and $P{endMonth} group by orgcode,orgname order by orgcode", List(selectYear, selectBeginMonth, selectEndMonth))
list._2.foreach(row =>{
row match {
case List(orgcode, orgname, pmspolicecount, slscdbrs, rjsabase, pbrs, cars, cars_sfbg, cars_lg, cars_xshj, cars_sj, cal, bhcabase, bhbqs, bhbqs_sfbg, bhbqs_sj, bqsl, bhbqsbase, bhpwz, bhpwz_sfbg, bhpwz_sj, wzl, phpwzbase) => {
cells.foreach(col =>{
if(col.getType() == CellType.LABEL && col.getContents() == ){
}
println(col.getContents())
println(col.getRow)
println(col.getColumn)
println(col.getType)
println(CellReferenceHelper.getCellReference(col.getColumn,col.getRow))
}
)
}
}
}
val FCell: FormulaCell = cells(3).asInstanceOf[FormulaCell]
println("@@@@@@@@@@"+FCell.getFormula())
wwb.write()
wwb.close()
os.close()
//
response.setCharacterEncoding("UTF-8")
//
val params :HashMap[Object, Any]= new HashMap()
//
// val context = (new InitialContext)
//
val jndiLookup = context.lookup("java:/comp/env/lift")
// val conn = jndiLookup.asInstanceOf[DataSource].getConnection
// val repName = request.getSession.getAttribute("repName")
//
//
val selectYear = request.getSession.getAttribute("selectYear").toString()
//
val selectBeginMonth = request.getSession.getAttribute("selectBeginMonth").toString()
//
val selectEndMonth = request.getSession.getAttribute("selectEndMonth").toString()
// val selectStyle = request.getSession.getAttribute("selectStyle").toString()
//
//
//
// params.put("IS_IGNORE_PAGINATION", selectStyle != "pdf")//不需要分页吗
//
params.put("year", selectYear)//年份
//
params.put("beginMonth", selectBeginMonth)//开始月份
//
params.put("endMonth", selectEndMonth)//结束月份
//
//
//得到jasperPrint对象
//
val jasperPrint: JasperPrint = JasperFillManager.fillReport(request.getRealPath("/") + "/reportFile/" + repName +".jasper", params, conn)
// //val jasperReport:JasperReport = JasperCompileManager.compileReport(request.getRealPath("/") + "/report/report1.jrxml")
//
// selectStyle match {
// case "html" => {
//
//JRHtmlExporter导出html格式 方法1(直接在页面打印)
//
response.setContentType("text/html;charset=UTF-8")
//
val out: PrintWriter = response.getWriter()
//
val exporter = new JRHtmlExporter
//
request.getSession().setAttribute("net.sf.jasperreports.j2ee.jasper_print", jasperPrint) // ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE
//
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint)
//
exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out)
//
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, request.getContextPath()+"/servlets/image?image=")
//
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8")
//
exporter.exportReport()
//
try{
//
if(conn != null && conn.isClosed == false){
//
conn.close
//
}
//
} catch {
//
case ex:Exception => println(ex.printStackTrace)
//
case _ =>
//
}
//
//
//方法2(创建一个html页面)
//
//JasperExportManager.exportReportToHtmlFile(jasperPrint, request.getRealPath("/") + "/report/result.html")
//
//启动jasperViewer预览报表 (仅限服务端生效)
//
//JasperViewer.viewReport(jasperPrint)
//
//response.sendRedirect(request.getContextPath()+"/report/result.html")
// }
// case "pdf" => {
//
val ouputStream = response.getOutputStream()
//
response.setContentType("application/pdf;charset=UTF-8")
//
request.getSession().setAttribute("net.sf.jasperreports.j2ee.jasper_print", jasperPrint)
//
val pdfexporter = new JRPdfExporter
//
pdfexporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint)
//
//设置输出流
//
pdfexporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream)
//
//如果保存到硬盘用下面语句
//
//pdfexporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "c:\\mypdf.pdf")
//
pdfexporter.setParameter(JRPdfExporterParameter.IS_ENCRYPTED, false)
//
//pdfexporter.setParameter(JRExporterParameter.OUTPUT_FILE, "TEST")
//
pdfexporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8")
//
pdfexporter.exportReport()
//
try{
//
if(conn != null && conn.isClosed == false){
//
conn.close
//
}
//
} catch {
//
case ex:Exception => println(ex.printStackTrace)
//
case _ =>
//
}
//
}
// case "excel" => {
//
response.setContentType("application/vnd.ms-excel;charset=UTF-8")
//
request.getSession().setAttribute("net.sf.jasperreports.j2ee.jasper_print", jasperPrint)
//
val ouputStream = response.getOutputStream()
//
val exporter = new net.sf.jasperreports.engine.export.JRXlsExporter()
//
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint)
//
//设置输出流
//
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream)
//
exporter.setParameter(JRXlsAbstractExporterParameter.IS_ONE_PAGE_PER_SHEET,false)
//
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8")
//
exporter.exportReport()
//
try{
//
if(conn != null && conn.isClosed == false){
//
conn.close
//
}
//
} catch {
//
case ex:Exception => println(ex.printStackTrace)
//
case _ =>
//
}
//
// }
// }
}
}