package com.thinkgem.jeesite.common.utils.excel;

import java.io.File;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List; import javax.servlet.http.HttpServletResponse; import org.apache.ibatis.ognl.Ognl;
import org.apache.ibatis.ognl.OgnlException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Sheet; import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.excel.annotation.ExcelField;
import com.thinkgem.jeesite.modules.account.entity.Account;
import com.thinkgem.jeesite.modules.account.service.AccountService; public class ExcelAccount { /**
* 导出excel表格方法一
*
* @param response
* @param accountService
* @param account
* @param data
*/
public void exportExcel(HttpServletResponse response, AccountService accountService, Account account, String data) { try {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("前台用户表数据");
HSSFRow row = sheet.createRow(0);
// Sheet样式
HSSFCellStyle style = wb.createCellStyle();//设置标题样式
String[] splitData = data.split(","); Font font = wb.createFont();
font.setFontHeightInPoints((short) 10);
font.setBoldweight((short) 600);
font.setColor(HSSFColor.DARK_TEAL.index);
style.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
style.setFont(font);
sheet.autoSizeColumn(1, true);
row.setHeightInPoints(26); HSSFCellStyle centerstyle = wb.createCellStyle();//设置普通表格样式
//centerstyle.setFont(font);
//centerstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
//centerstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
//centerstyle.setWrapText(true);
// centerstyle.setLeftBorderColor(HSSFColor.BLACK.index);
//centerstyle.setBorderLeft((short) 1);
//centerstyle.setRightBorderColor(HSSFColor.BLACK.index);
//centerstyle.setBorderRight((short) 1);
centerstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体
// centerstyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色.
//centerstyle.setFillForegroundColor(HSSFColor.WHITE.index);// 设置单元格的背景颜色. String fileName = "前台用户数据.xls";
setResponseHeader(response, fileName);
if (splitData != null && splitData.length > 0) {
int i = 0;
for (String str : splitData) {
HSSFCell cell = row.createCell(i);
if ("loginName".equals(str)) {
cell.setCellValue("用户登录名"); } else if ("companyCnName".equals(str)) {
cell.setCellValue("企业名称(中文)");
cell.setCellStyle(style);
} else if ("companyEnName".equals(str)) {
cell.setCellValue("企业名称(英文)"); } else if ("companyCnAddress".equals(str)) {
cell.setCellValue("企业地址(中文)"); } else if ("cnZhuYingProd".equals(str)) {
cell.setCellValue("主营产品(中文)"); } else if ("companyPhone".equals(str)) {
cell.setCellValue("公司电话"); } else if ("fzrName".equals(str)) {
cell.setCellValue("外贸负责人姓名"); } else if ("fzrSex".equals(str)) {
cell.setCellValue("外贸负责人性别"); } else if ("fzrPosition".equals(str)) {
cell.setCellValue("外贸负责人职位"); } else if ("fzrMobile".equals(str)) {
cell.setCellValue("外贸负责人手机"); } else if ("fzrEmail".equals(str)) {
cell.setCellValue("外贸负责人邮箱"); } else if ("fzrQq".equals(str)) {
cell.setCellValue("外贸负责人QQ"); } else if ("name".equals(str)) {
cell.setCellValue("姓名"); } else if ("sex".equals(str)) {
cell.setCellValue("性别"); } else if ("mobile".equals(str)) {
cell.setCellValue("手机号码"); } else if ("email".equals(str)) {
cell.setCellValue("邮箱"); } else if ("emailStatus".equals(str)) {
cell.setCellValue("邮箱状态"); } else if ("birthday".equals(str)) {
cell.setCellValue("生日"); } else if ("accountType".equals(str)) {
cell.setCellValue("用户类型"); } else if ("address".equals(str)) {
cell.setCellValue("个人地址"); } else if ("qq".equals(str)) {
cell.setCellValue("个人qq"); } else if ("companyUrl".equals(str)) {
cell.setCellValue("公司网站"); } else if ("registerType".equals(str)) {
cell.setCellValue("注册类型"); } else if ("shiBie".equals(str)) {
cell.setCellValue("识别码"); } else if ("tuiJianId".equals(str)) {
cell.setCellValue("推荐id"); }
cell.setCellStyle(style);
i++;
}
}
List<Account> findAccountList = accountService.findAccountList(account);
if (findAccountList != null && findAccountList.size() > 0) {
int m = 1;
for (Account at : findAccountList) {
row = sheet.createRow(m); int n = 0;
for (String str : splitData) {
String cellVal = ""; if ("loginName".equals(str)) {
cellVal = at.getLoginName();
} else if ("companyCnName".equals(str)) {
cellVal = at.getCompanyCnName();
} else if ("companyEnName".equals(str)) {
cellVal = at.getCompanyEnName();
} else if ("companyCnAddress".equals(str)) {
cellVal = at.getCompanyCnAddress();
} else if ("cnZhuYingProd".equals(str)) {
cellVal = at.getCnZhuYingProd();
} else if ("companyPhone".equals(str)) {
cellVal = at.getCompanyPhone();
} else if ("fzrName".equals(str)) {
cellVal = at.getFzrName();
} else if ("fzrSex".equals(str)) {
if (at.getFzrSex() != null) {
if ("1".equals(at.getFzrSex())) {
cellVal = "男";
} else if ("2".equals(at.getFzrSex())) {
cellVal = "女";
}
} else {
cellVal = "";
} } else if ("fzrPosition".equals(str)) {
cellVal = at.getFzrPosition();
} else if ("fzrMobile".equals(str)) {
cellVal = at.getFzrMobile();
} else if ("fzrEmail".equals(str)) {
cellVal = at.getFzrEmail();
} else if ("fzrQq".equals(str)) {
cellVal = at.getFzrQq();
} else if ("name".equals(str)) {
cellVal = at.getName();
} else if ("sex".equals(str)) {
if (at.getSex() != null) {
if ("1".equals(at.getSex())) {
cellVal = "男";
} else if ("2".equals(at.getSex())) {
cellVal = "女";
}
} else {
cellVal = "";
} } else if ("mobile".equals(str)) {
cellVal = at.getMobile();
} else if ("email".equals(str)) {
cellVal = at.getEmail();
} else if ("emailStatus".equals(str)) { if (at.getEmailStatus() != null) {
if (at.getEmailStatus() == 0) {
cellVal = "未验证";
} else if (at.getEmailStatus() == 1) {
cellVal = "已验证";
} else {
cellVal = "";
}
} else {
cellVal = "";
} } else if ("birthday".equals(str)) {
if (at.getBirthday() != null) {
cellVal = DateUtils.formatDate(at.getBirthday(), "yyyy-MM-dd");
} else {
cellVal = null;
} } else if ("accountType".equals(str)) {
if (at.getAccountType() != null) {
if (at.getAccountType() == 1) {
cellVal = "企业用户";
} else if (at.getAccountType() == 2) {
cellVal = "个人用户";
} else {
cellVal = "";
}
} else {
cellVal = "";
} } else if ("address".equals(str)) {
cellVal = at.getAddress();
} else if ("qq".equals(str)) {
cellVal = at.getQq();
} else if ("companyUrl".equals(str)) {
cellVal = at.getCompanyUrl();
} else if ("registerType".equals(str)) {
cellVal = at.getRegisterType();
} else if ("shiBie".equals(str)) {
cellVal = at.getShiBie();
} else if ("tuiJianId".equals(str)) {
cellVal = String.valueOf(at.getTuiJianId());
}
//row.createCell(n).setCellStyle(centerstyle);
HSSFCell createCell = row.createCell(n);
createCell.setCellValue(cellVal);
createCell.setCellStyle(centerstyle);
//row.createCell(n).setCellValue(cellVal); n++;
}
m++;
}
}
OutputStream os = response.getOutputStream();
wb.write(os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
} } public void setResponseHeader(HttpServletResponse response, String fileName) {
try {
try {
fileName = new String(fileName.getBytes(), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.setContentType("application/octet-stream;charset=ISO8859-1");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
} catch (Exception ex) {
ex.printStackTrace();
}
} /**
* 导出excel表格方法二
*
* @param response
* @param accountService
* @param account
* @param data
*/
public void exportAllExcel(HttpServletResponse response, AccountService accountService, Account account, String data) { String[] splitData = data.split(",");
List<Account> findAccountList = accountService.findAccountList(account);
String[][] datas = new String[findAccountList.size() + 1][splitData.length];
String titles[] = new String[splitData.length];
datas[0] = titles;
Field[] fields = Account.class.getDeclaredFields();
int titlesIndex = 0;
for (Field filed : fields) {
for (String filedName : splitData) {
if (filed.getName().equals(filedName)) {
ExcelField annotation = filed.getAnnotation(ExcelField.class);
String title = annotation.title();
titles[titlesIndex++] = title;
}
}
}
for (int i = 0; i < findAccountList.size(); i++) {
Account ac = findAccountList.get(i);
String[] row = new String[splitData.length];
for (int j = 0; j < splitData.length; j++) {
try {
row[j] = Ognl.getValue(ac, splitData[j]).toString();
} catch (OgnlException e) {
row[j] = "";
}
}
datas[i + 1] = row;
} HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("前台用户表数据"); HSSFRow createRow = sheet.createRow(0);
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
for (int i = 0; i < titles.length; i++) {
HSSFCell cell = createRow.createCell(i);
cell.setCellStyle(style);
cell.setCellValue(titles[i]);
} for (int i = 1; i < datas.length; i++) {
HSSFRow row = sheet.createRow(i);
String[] tls = datas[i];
for (int j = 0; j < tls.length; j++) {
HSSFCell cell = row.createCell(j);
cell.setCellValue(tls[j]);
}
} } }

控制层调用上面方法

@RequestMapping(value = "exportAccountList", method = RequestMethod.POST)
public String exportAccountList(Account account,HttpServletRequest request,HttpServletResponse response,RedirectAttributes redirectAttributes,String data){ try { ExcelAccount els = new ExcelAccount();
els.exportExcel(response, accountService, account, data);
return null;
} catch (Exception e) {
e.printStackTrace();
addMessage(redirectAttributes, "导出前台用户失败!失败信息:" + e.getMessage());
} return "redirect:"+adminPath+"/account/list?repage";
}

java 实现用户自由选择字段实现导出EXCEL表格的更多相关文章

  1. java中使用poi导出excel表格数据并且可以手动修改导出路径

    在我们开发项目中,很多时候会提出这样的需求:将前端的某某数据以excel表格导出,今天就给大家写一个简单的模板. 这里我们选择使用poi导出excel: 第一步:导入需要的jar包到 lib 文件夹下

  2. java导出excel表格

    java导出excel表格: 1.导入jar包 <dependency> <groupId>org.apache.poi</groupId> <artifac ...

  3. 使用NPOI将数据库里信息导出Excel表格并提示用户下载

    使用NPOI进行导出Excel表格大家基本都会,我在网上却很少找到导出Excel表格并提示下载的 简单的代码如下 //mvc项目可以传多个id以逗号相隔的字符串 public ActionResult ...

  4. Java代码导入导出 Excel 表格最简单的方法

    import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStrea ...

  5. NPOI_winfrom导出Excel表格(一)(合并单元格、规定范围加外边框、存储路径弹框选择)

    1.导出 private void btn_print_Click(object sender, EventArgs e) { DataTable dtNew = new DataTable(); d ...

  6. java中使用jxl导出Excel表格详细通用步骤

    该方法一般接收两个参数,response和要导出的表格内容的list. 一般我们将数据库的数据查询出来在页面进行展示,根据用户需求,可能需要对页面数据进行导出. 此时只要将展示之前查询所得的数据放入s ...

  7. Spring Boot 导出Excel表格

    Spring Boot 导出Excel表格 添加支持 <!--添加导入/出表格依赖--> <dependency> <groupId>org.apache.poi& ...

  8. PHP导入导出excel表格图片(转)

    写excel的时候,我用过pear的库,也用过pack压包的头,同样那些利用smarty等作的简单替换xml的也用过,csv的就更不用谈了.呵呵.(COM方式不讲了,这种可读的太多了,我也写过利用wp ...

  9. .NET环境下导出Excel表格的两种方式和导入两种类型的Excel表格

    一.导出Excel表格的两种方式,其中两种方式指的是导出XML数据类型的Excel(即保存的时候可以只需要修改扩展名为.xls)和真正的Excel这两种. using System; using Sy ...

随机推荐

  1. STM32F103之USART学习记录

    1.USART的主要特性 1)名称:串行异步通信接口 2)全双工.异步通信 3)发送和接收速率最高可达4.5MBits/s 4)可编程数据长度8或9bits 5)可配置的停止位:支持1或2位停止位 6 ...

  2. malloc实现机制

    使用过c语言的都知道malloc是一个动态分配内存的函数,还可以通过free释放内存空间. 如果我们想分析一下malloc的源码,这其实不是一会就能看懂的,但是我们可以讨论一下malloc的简单实现. ...

  3. VS Code的git配置

    最近打算使用VS Code作为python的编辑器,这里记录一下VS Code中git的配置方法 因为vscode中git只是使用本地的git,所以本地必须先安装git才行. 1.git的安装 git ...

  4. [Python] Tkinter的食用方法_02_LabelFrame RadioButton CheckButton

    #开始 Python的话 我是看的小甲鱼的视频 所以代码也是小甲鱼的修改版 本博客这里只是学习记录 小甲鱼是个很棒的老师,虽然经常飙车.... #第一个代码 from tkinter import * ...

  5. IoT生态不完善、与智能电视区别不大,荣耀智慧屏概念大于实际

    编辑 | 于斌 出品 | 于见(mpyujian) 前两天,华为荣耀略显"低调"地在北京召开了一场小型的媒体沟通会.在这场沟通会上,荣耀却颇为"重磅"地推出了坊 ...

  6. 【MySQL】完整性约束

    " 目录 not null default unique 单列唯一 联合唯一 primary key 单列主键 复合主键 auto_increment 步长与偏移量 foreign key ...

  7. Centos610安装Nexus

    1.下载Nexus 地址:     https://pan.baidu.com/s/1D5AI6zmuRBSMK0k7j41VuQ 提取码: q50j 选择02-nexus 2.新建nexus账号 u ...

  8. matplotlib显示AttributeError: 'module' object has no attribute 'verbose'

    解决办法:file-settings-tools-python scientific,将show plots in toolwindow前面的对号去掉即可.

  9. 兔子与兔子(字符串hash)

    传送门 很久很久以前,森林里住着一群兔子. 有一天,兔子们想要研究自己的 DNA 序列. 我们首先选取一个好长好长的 DNA 序列(小兔子是外星生物,DNA 序列可能包含 26 个小写英文字母). 然 ...

  10. 使用Vue时localhost:8080中localhost换成ip地址后无法显示页面的问题

    解决办法是:在package.json中 然后重新启动服务器 npm run dev 就正常显示了.