1 public class ExcelUtils {
2 /**
3 * 根据数据信息生成Excel文档到相应目录
4 * 导出指定list
5 *
6 * @param <T>
7 * @param title 文档 sheet 名称
8 * @param dataset 数据集,Map中的key与fieldStr对应
9 * @param out 输入流
10 * @param fieldStr 关联value与title的key值
11 * @param titleMap 文档标题栏
12 */
13 public <T> void exportExcel(String title, List<Map<String, Object>> dataset,
14 Map<String,String> titleMap,OutputStream out, String[] fieldStr) {
15 // 声明一个工作薄
16 try {
17 // 首先检查数据看是否是正确的
18 if (dataset == null || dataset.size() < 1 || title == null || out == null) {
19 throw new Exception("传入的数据不对!");
20 }
21
22 HSSFWorkbook workbook = new HSSFWorkbook();
23 // 生成一个表格
24 HSSFSheet sheet = workbook.createSheet(title);
25 // 设置表格默认列宽度为15个字节
26 sheet.setDefaultColumnWidth(15);
27 // 设置边框
28 HSSFCellStyle style = workbook.createCellStyle();
29 style.setBorderTop(HSSFCellStyle.BORDER_THIN); //上边框
30 style.setBorderLeft(HSSFCellStyle.BORDER_THIN); //左边框
31 style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
32 style.setBorderRight(HSSFCellStyle.BORDER_THIN); //右边框
33 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
34 style.setWrapText(true); // 自动换行
35 //表头样式
36 HSSFCellStyle headerStyle = workbook.createCellStyle();
37 headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); //上边框
38 headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); //左边框
39 headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
40 headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); //右边框
41 headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
42 // 单元格样式
43 HSSFFont headerFont = workbook.createFont();
44 headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //粗体显示
45 headerFont.setFontHeightInPoints((short)14); //设置字体大小
46 headerStyle.setFont(headerFont); //set到style
47 // 单元格样式
48 HSSFFont titlefont = workbook.createFont(); //create
49 titlefont.setFontName("微软雅黑"); //字体名称
50 titlefont.setFontHeightInPoints((short) 17); //设置字体大小
51 titlefont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
52 //标题样式
53 HSSFCellStyle titleStyle = workbook.createCellStyle();
54 titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
55 titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平
56 titleStyle.setFont(titlefont);
57
58
59 HSSFRow row = null;
60 HSSFCell cell = null;
61 row = sheet.createRow(0);//创建一行
62 row.setHeightInPoints(50);//行高
63 HSSFCell ce0 = row.createCell((short) 0); //创建列
64 ce0.setCellValue(title); // 表格的第一行第一列显示的数据
65 ce0.setCellStyle(titleStyle); // 样式,字体 ,居中
66
67 // 标题
68 List<String> exportfieldtile = new ArrayList<String>();
69 for(String key: fieldStr){
70 exportfieldtile.add(titleMap.get(key));
71 }
72 // 产生表格标题行
73 row = sheet.createRow(1);
74 //合并单元格 开始行,结束行,开始列,结束列
75 sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) (exportfieldtile.size()-1)));//合并单元格 开始行,结束行,开始列,结束列
76 //遍历字段列 并放入到该行 各列中
77 for (int i = 0; i < exportfieldtile.size(); i++) {
78 cell = row.createCell(i);//创建(i)列
79 cell.setCellStyle(headerStyle);//set top Style
80 HSSFRichTextString text = new HSSFRichTextString(exportfieldtile.get(i));
81 cell.setCellValue(text);
82 }
83
84 int index = 1;
85 // 循环整个集合
86 for(Map<String,Object> valueMap: dataset){
87 index++;
88 row = sheet.createRow(index);
89
90 for (int k = 0; k < fieldStr.length; k++) {
91 cell = row.createCell(k);
92 cell.setCellStyle(style);
93 Object value = valueMap.get(fieldStr[k]);
94 String textValue = getValue(value);
95
96 HSSFRichTextString richString = new HSSFRichTextString(textValue);
97 cell.setCellValue(richString);
98 }
99
100 }
101 workbook.write(out);
102 } catch (Exception e) {
103 e.printStackTrace();
104 }
105 }
106 public String getValue(Object value) {
107 String textValue = "";
108 if (value == null)
109 return textValue;
110
111 if (value instanceof Boolean) {
112 boolean bValue = (Boolean) value;
113 textValue = "是";
114 if (!bValue) {
115 textValue = "否";
116 }
117 } else if (value instanceof Date) {
118 Date date = (Date) value;
119 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ");
120 textValue = sdf.format(date);
121 } else
122 textValue = value.toString();
123 return textValue;
124 }
106 }
1 private String filename;
2 private InputStream inputStream;
3
4 public String getFilename() throws UnsupportedEncodingException {
5 response.setHeader("charset", "ISO8859-1");
6 try
7 {
8 return new String(this.filename.getBytes(), "ISO8859-1");
9 } catch (UnsupportedEncodingException e)
10 {
11 return "获取文件名出现了错误!";
12 }
13 }
14
15 public void setFilename(String filename) throws UnsupportedEncodingException {
16 this.filename = new String(filename.getBytes("gbk"), "ISO-8859-1");
17 }
18
19
20 public InputStream getInputStream() throws Exception
21 {
22 filename += "统计表.xls";
23 return new FileInputStream(request.getSession().getServletContext().getRealPath("/ExcelTempFolder") + "/" + filename); // 如果dir是绝对路径
24 }
25
26 public String downPersonExcel() throws Exception{
27 System.out.println("导出人员列表");
28 if (bean == null) {
29 bean = new InnovationBean();
30 }
31 String[] fieldStr = null;
32 fieldStr = new String[] { "userName", "sysEmpPost" ,"birthday", "comeAcademyTime", "temporary", "temporaryunit", "temporarybdate", "temporaryedate", "ComePlace", "retiredate", "temporaryremark" };
33 // 标题
34 Map<String, String> titleMap = new HashMap<String, String>();// 导出title
35 titleMap.put("userName", "人员名称");
36 titleMap.put("sysEmpPost", "聘用制岗位");
37 titleMap.put("birthday", "生日 ");
38 titleMap.put("comeAcademyTime", "入院时间");
39 titleMap.put("temporary", "挂职锻炼");
40 titleMap.put("temporaryunit", "挂职单位");
41 titleMap.put("temporarybdate", "挂职开始时间");
42 titleMap.put("temporaryedate", "挂职结束时间");
43 titleMap.put("ComePlace", "来院渠道");
44 titleMap.put("retiredate", "法定退休时间");
45 titleMap.put("temporaryremark", "挂职备注");
46
47 ExcelUtils excelUtils = new ExcelUtils();
48 HttpSession session=request.getSession();
49 String entityName = ParamUtils.getSession(session, "entityName");
50 String entityName1="";
51 if(entityName.contains("/")){
52 entityName1=entityName.replace("/","-");
53 System.out.println(entityName1+"__==asda");
54 }else{
55 entityName1=entityName;
56 }
57
58 //int i = entityName.indexOf("/");
59 //String str = entityName.substring(i);
60 //System.out.println(entityName);
61
62
63 String unitId=ParamUtils.getSession(session, "entityId");
64 System.out.println(unitId);
65 bean.setUnitId(unitId);
66 DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
67 List<Map<String, Object>> dataSet = new ArrayList<Map<String, Object>>();
68 List<InnovationBean> list = innovationService.getExportPersonList(bean);
69 for (InnovationBean innovationBean : list) {
70 Map<String, Object> valueMap = new HashMap<String, Object>();
71 //人员名称
72 if(innovationBean.getUserName() != null){
73 valueMap.put("userName", innovationBean.getUserName());
74 }else{
75 valueMap.put("userName", "--");
76 }
77 //聘用制岗位
78 if(innovationBean.getSysEmpPost() != null){
79 valueMap.put("sysEmpPost", innovationBean.getSysEmpPost());
80 }else{
81 valueMap.put("sysEmpPost", "--");
82 }
83 //生日
84 if(innovationBean.getBirthday() != null){
85 valueMap.put("birthday", format.format(innovationBean.getBirthday()));
86 }else{
87 valueMap.put("birthday", "--");
88 }
89 //入院时间
90 if(innovationBean.getComeAcademyTime() != null){
91 valueMap.put("comeAcademyTime", format.format(innovationBean.getComeAcademyTime()));
92 }else{
93 valueMap.put("comeAcademyTime", "--");
94 }
95 //挂职锻炼
96 if(innovationBean.getTemporary() != null){
97 valueMap.put("temporary", innovationBean.getTemporary());
98 }else{
99 valueMap.put("temporary", "--");
100 }
101 //挂职单位
102 if(innovationBean.getTemporaryunit() != null){
103 valueMap.put("temporaryunit", innovationBean.getTemporaryunit());
104 }else{
105 valueMap.put("temporaryunit", "--");
106 }
107 //挂职开始时间
108 if(innovationBean.getTemporarybdate() != null){
109 valueMap.put("temporarybdate", format.format(innovationBean.getTemporarybdate()));
110 }else{
111 valueMap.put("temporarybdate", "--");
112 }
113 //挂职结束时间
114 if(innovationBean.getTemporaryedate() != null){
115 valueMap.put("temporaryedate", format.format(innovationBean.getTemporaryedate()));
116 }else{
117 valueMap.put("temporaryedate", "--");
118 }
119 //来院渠道
120 if(innovationBean.getComePlace() != null){
121 valueMap.put("ComePlace", innovationBean.getComePlace());
122 }else{
123 valueMap.put("ComePlace", "--");
124 }
125 //法定退休时间
126 if(innovationBean.getRetiredate() != null){
127 valueMap.put("retiredate", format.format(innovationBean.getTemporaryedate()));
128 }else{
129 valueMap.put("retiredate", "--");
130 }
131 //挂职备注
132 if(innovationBean.getTemporaryremark() != null){
133 valueMap.put("temporaryremark", innovationBean.getTemporaryremark());
134 }else{
135 valueMap.put("temporaryremark", "--");
136 }
137 dataSet.add(valueMap);
138 }
139
140 filename = entityName1+"人员信息统计表";
141 // response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode("fileName", "utf-8"));
142 //InputStream in=ServletActionContext.getServletContext().getResourceAsStream(realPath);
143 FileOutputStream fileOut = new FileOutputStream(request.getSession().getServletContext().getRealPath("/ExcelTempFolder") + "/" + filename + "统计表.xls");
144 excelUtils.exportExcel(filename, dataSet, titleMap, fileOut, fieldStr);
145 return "success";
146
147
148 }
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="innovation" extends="default" namespace="/innovation">
<!-- gangwei管理 -->
<action name="innovation_*" method="{1}" class="innovationAction">
<interceptor-ref name="tokenSession" >
<param name="includeMethods">save</param>
</interceptor-ref>
<interceptor-ref name="cassInterceptorStack" />
<result name="success" type="stream">
<!-- 指定下载文件的内容类型,text/plain是默认类型 -->
<param name="contentType">text/plain,charset=UTF-8</param>
<!-- inputName默认值是inputStream,如果action中用于读取下载文件内容的属性名是inputStream,那么可以省略这个参数 -->
<param name="inputName">inputStream</param>
<!--动态获取文件名,从Action中的取得filename-->
<param name="contentDisposition">
attachment;filename="${filename}"
</param>
<param name="bufferSize">2048</param>
</result>
</action>
</package>
</struts>