jxl

public static void createCSVFile(List<String> basicInfo, List exportData0,List exportData,List subEventExportData, LinkedHashMap rowMapper,LinkedHashMap subEventMap, String outPutPath, String filename, HttpServletResponse response) {
		try {
			OutputStream os = response.getOutputStream();// 取得输出流
			response.reset();// 清空输出流
			response.setHeader("Content-disposition", "attachment; filename=event.csv");// 设定输出文件头
			response.setContentType("application/msexcel");// 定义输出类型

			WritableWorkbook wbook = Workbook.createWorkbook(os);
			String tmptitle = "活动信息表"; // 标题
			WritableSheet wsheet = wbook.createSheet(tmptitle, 0); // sheet名称
			wsheet.setColumnView(0, 30);// 设置第0列宽度,
			wsheet.setColumnView(1, 35);
			wsheet.setColumnView(2, 30);
			wsheet.setColumnView(3, 30);
			wsheet.setColumnView(4, 30);
			wsheet.setColumnView(5, 30);
			WritableFont wfont = new WritableFont(WritableFont.ARIAL, 16, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
			WritableCellFormat wcfFC = new WritableCellFormat(wfont);
			wcfFC.setBackground(Colour.AQUA);
			wsheet.addCell(new Label(1, 0, tmptitle, wcfFC));
			wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 14, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
			wcfFC = new WritableCellFormat(wfont);

			int rowCnt = 2;
			int columnCnt = 0;
			for (int idx = 0; idx < basicInfo.size(); idx++) {
				String str= basicInfo.get(idx);
				if(str!=null){
					str=str.replace("null", Constants.EMPTY);
				}
				wsheet.addCell(new Label(columnCnt, rowCnt, str));
				rowCnt++;
			}
			//生成子活动内容
			columnCnt = 0;
			for (Iterator propertyIterator = subEventMap.entrySet().iterator(); propertyIterator.hasNext();) {
				java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator.next();
				String value=Constants.EMPTY;
				if(propertyEntry.getValue()!=null){
					value=propertyEntry.getValue().toString();
				}
				wsheet.addCell(new Label(columnCnt, rowCnt,value));
				columnCnt++;
			}
			rowCnt++;
			columnCnt = 0;
			for (Iterator iterator = subEventExportData.iterator(); iterator.hasNext();) {
				Map row = (Map) iterator.next();
				for (Iterator propertyIterator = row.entrySet().iterator(); propertyIterator.hasNext();) {
					java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator.next();
					String value=Constants.EMPTY;
					if(propertyEntry.getValue()!=null){
						value=propertyEntry.getValue().toString();
					}
					wsheet.addCell(new Label(columnCnt, rowCnt,value));
					columnCnt++;
				}
				rowCnt++;
				columnCnt = 0;
			}
			rowCnt++;
			// 开始生成主体内容
			columnCnt = 0;
			for (Iterator propertyIterator = rowMapper.entrySet().iterator(); propertyIterator.hasNext();) {
				java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator.next();
				String value=Constants.EMPTY;
				if(propertyEntry.getValue()!=null){
					value=propertyEntry.getValue().toString();
				}
				wsheet.addCell(new Label(columnCnt, rowCnt,value));
				columnCnt++;
			}
			rowCnt++;
			// 参与信息 写入文件内容0
			columnCnt = 0;
			for (Iterator iterator = exportData0.iterator(); iterator.hasNext();) {
				Map row = (Map) iterator.next();
				for (Iterator propertyIterator = row.entrySet().iterator(); propertyIterator.hasNext();) {
					java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator.next();
					String value=Constants.EMPTY;
					if(propertyEntry.getValue()!=null){
						value=propertyEntry.getValue().toString();
					}
					wsheet.addCell(new Label(columnCnt, rowCnt,value));
					columnCnt++;
				}
				rowCnt++;
				columnCnt = 0;
			}
			rowCnt++;
			// 开始生成主体内容
			columnCnt = 0;
			for (Iterator propertyIterator = rowMapper.entrySet().iterator(); propertyIterator.hasNext();) {
				java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator.next();
				String value=Constants.EMPTY;
				if(propertyEntry.getValue()!=null){
					value=propertyEntry.getValue().toString();
				}
				wsheet.addCell(new Label(columnCnt, rowCnt,value));
				columnCnt++;
			}
			rowCnt++;
			// 参与信息 写入文件内容1
			columnCnt = 0;
			for (Iterator iterator = exportData.iterator(); iterator.hasNext();) {
				Map row = (Map) iterator.next();
				for (Iterator propertyIterator = row.entrySet().iterator(); propertyIterator.hasNext();) {
					java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator.next();
					String value=Constants.EMPTY;
					if(propertyEntry.getValue()!=null){
						value=propertyEntry.getValue().toString();
					}
					wsheet.addCell(new Label(columnCnt, rowCnt,value));
					columnCnt++;
				}
				rowCnt++;
				columnCnt = 0;
			}
			wbook.write(); // 写入文件
			wbook.close();
			os.close(); // 关闭流

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

你可能感兴趣的:(JXL)