Scala使用POI生成Excel的Servlet

  1 lass PatentAnalysisExportExcelResult(requestContext: RequestContext,
  2   patentAnalysisResult: PatentAnalysisResult) extends ActionResult(requestContext) with Disposable {
  3   val fieldMap = Map[String, String](("ad_year", "申请日"),
  4     ("pd_year", "公开公告日"),
  5     ("paid", "申请人"),
  6     ("inid", "发明人"),
  7     ("ic_subclass", "IPC小类"),
  8     ("ic_supergroup", "IPC大组"),
  9     ("ic", "IPC小组"),
 10     ("co", "专利所属国家地区"),
 11     ("pac", "申请人所在国家地区"),
 12     ("pcid_1", "一级技术分类"),
 13     ("pcid_2", "二级技术分类"))
 14   val idSet = Set[String]("ad_year", "pd_year", "ic_subclass", "ic_supergroup", "ic")
 15 
 16   def execute = {
 17     response.setContentType("application/excel")
 18     response.setCharacterEncoding("utf-8")
 19 
 20     patentAnalysisResult.patentAnalysisResultParam.analysisType match {
 21       case "s" => {
 22         response.addHeader("Content-Disposition", "attachment; filename=" + "patent-analysis-tableexport-" + patentAnalysisResult.patentAnalysisResultParam.analysisFieldOne + "-" + patentAnalysisResult.patentAnalysisResultParam.analysisFieldTwo + "-" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xls")
 23         writeDoubleFieldFacet(patentAnalysisResult.DoubleFieldCounts, patentAnalysisResult.patentAnalysisResultParam.analysisFieldOne, patentAnalysisResult.patentAnalysisResultParam.analysisFieldTwo)
 24       }
 25       case _ => {
 26         response.addHeader("Content-Disposition", "attachment; filename=" + "patent-analysis-tableexport-" + patentAnalysisResult.patentAnalysisResultParam.analysisField + "-" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xls")
 27 
 28         patentAnalysisResult.patentAnalysisResultParam.analysisField match {
 29           case "ad_year" => writeFieldFacet(patentAnalysisResult.applicationDateCounts, "申请日分析表格", "申请年", "id")
 30           case "pd_year" => writeFieldFacet(patentAnalysisResult.publicationDateCounts, "公开公告日分析表格", "公开公告年", "id")
 31           case "paid" => writeFieldFacet(patentAnalysisResult.applicantCounts, "申请人分析表格", "申请人", "name")
 32           case "inid" => writeFieldFacet(patentAnalysisResult.inventorCounts, "发明人分析表格", "发明人", "name")
 33           case "ic_subclass" => writeFieldFacet(patentAnalysisResult.ipcSubClassCounts, "IPC小类分析表格", "IPC小类", "id")
 34           case "ic_supergroup" => writeFieldFacet(patentAnalysisResult.ipcSuperGroupCounts, "IPC大组分析表格", "IPC大组", "id")
 35           case "ic" => writeFieldFacet(patentAnalysisResult.ipcCounts, "IPC小类分析表格", "IPC小类", "id")
 36           case "cb" => writeFieldFacet(patentAnalysisResult.citationCounts, "引用专利分析表格", "公开公告号", "id")
 37           case "cf" => writeFieldFacet(patentAnalysisResult.cocitationCounts, "被引专利分析表格", "公开公告号", "id")
 38           case "fm" => writeFieldFacet(patentAnalysisResult.familyStatsCounts, "专利族分析表格", "公开公告号", "id")
 39           case "co" => writeFieldFacet(patentAnalysisResult.countryCounts, "申请人所在国家地区分析表格", "国家地区", "name")
 40           case "pac" => writeFieldFacet(patentAnalysisResult.applicantCountryCounts, "申请人所在国家地区分析表格", "国家地区", "name")
 41           case "pcid_1" => writeFieldFacet(patentAnalysisResult.categoryOneCounts, "一级技术分类分析表格", "分类名称", "name")
 42           case "pcid_2" => writeFieldFacet(patentAnalysisResult.categoryTwoCounts, "二级技术分类分析表格", "分类名称", "name")
 43         }
 44       }
 45     }
 46   }
 47 
 48   private def writeFieldFacet(list: Array[FacetCount[IdNameEntity]], title: String, columnName: String, t: String) = {
 49     val hwb = new HSSFWorkbook()
 50     val sheet = hwb.createSheet(title)
 51     val firstrow = sheet.createRow(0)
 52     val names = Array[String]("排名", columnName, "专利数量")
 53 
 54     for (i <- 0 until names.length) {
 55       val cell = firstrow.createCell(i)
 56       cell.setCellValue(new HSSFRichTextString(names(i)))
 57     }
 58 
 59     for (i <- 0 until list.length) {
 60       val row = sheet.createRow(i + 1)
 61       val xlsDto = list(i)
 62 
 63       for (j <- 0 until 3) {
 64         val sortCell = row.createCell(0)
 65 
 66         sortCell.setCellValue(i + 1)
 67 
 68         val nameCell = row.createCell(1)
 69 
 70         if (t == "id") nameCell.setCellValue(xlsDto.t.id.toString)
 71         else nameCell.setCellValue(xlsDto.t.name)
 72 
 73         val countCell = row.createCell(2)
 74 
 75         countCell.setCellValue(xlsDto.count)
 76       }
 77     }
 78 
 79     using(responseOutputStream) { out =>
 80       {
 81         hwb.write(out)
 82       }
 83     }
 84   }
 85 
 86   private def writeDoubleFieldFacet(list: Array[DoubleFacetFiled], fieldOne: String, fieldTwo: String) = {
 87     val hwb = new HSSFWorkbook()
 88     val sheet = hwb.createSheet(fieldMap.getOrElse(fieldOne, "") + "-" + fieldMap.getOrElse(fieldTwo, "") + "表格")
 89     val firstrow = sheet.createRow(0)
 90     val names = Array[String]("") ++ { if (idSet.contains(fieldOne)) list.map(_.field.id.toString) else list.map(_.field.name) }
 91 
 92     for (i <- 0 until names.length) {
 93       val cell = firstrow.createCell(i)
 94 
 95       cell.setCellValue(new HSSFRichTextString(names(i)))
 96     }
 97 
 98     val len = list(0).fields.length
 99 
100     for (i <- 0 until len) {
101       val row = sheet.createRow(i + 1)
102 
103       for (j <- 0 to list.length) {
104         if (j == 0) {
105           val cell = row.createCell(0)
106           val field = list(j).fields(i)
107 
108           cell.setCellValue(if (idSet.contains(fieldTwo)) field.t.id.toString else field.t.name)
109         } else {
110           val cell = row.createCell(j)
111           val field = list(j-1).fields(i)
112 
113           cell.setCellValue(field.count)
114         }
115       }
116     }
117 
118     using(responseOutputStream) { out =>
119       {
120         hwb.write(out)
121       }
122     }
123   }
124 }

 

posted @ 2013-01-29 10:06  TerryLiang  阅读(1477)  评论(1编辑  收藏