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. 2019,.Net开发者的高光时刻

    随着微软发布的一系列关于Windows..net和C#的公告,.Net开发者将在2019年,迎来自己的高光时刻,毕竟“世界上只有少数几种语言是多功能的,而没有一个像C#那样干净整洁.” 一.现在学C# ...

  2. liux 命令行

    查找文件 () find / -name httpd.conf #在根目录下查找文件httpd.conf,表示在整个硬盘查找 () find /etc -name httpd.conf #在/etc目 ...

  3. C语言学习建议!8年编程开发经验

    C语言是几乎所有编程语言的先驱与灵感的来源,Perl,PHP,Python和Ruby都是用它写的,同样什么Microsoft Windows,Mac OS X,还有GNU/Linu这些操作系统,都是靠 ...

  4. git按需过滤提交文件的一个细节

    问题场景 用git管理代码时,作为git小白的我总会遇到一些无法理解的问题,在请教了一些高手后终于解开了疑惑,参考以下场景: 1.比如我们已在电脑1上完成用vs编辑项目.添加.提交到服务器的完整流程, ...

  5. nginx 解决 connect() failed (111: Connection refused) while connecting to upstream,

    嗯哼,刚装了个ubuntu的lnmp,我的天啊,踩的坑比我脂肪还多了 比如刚装完的时候访问显示502, 也不知道什么问题,就去看了一下nginx日志  /var/log/nginx/error.log ...

  6. Rsync+inotify数据同步

    安装环境 备份服务器端:CentOS7,IP:192.168.1.100 备份客户端:CentOS7,IP:192.168.1.200 服务器端Rsync服务部署 1.安装程序包 # yum –y i ...

  7. 微信HTTP公告

  8. python manage.py shell

    启动python有两种方式:python manage.py shell和python. 这两个命令 都会启动交互解释器,但是manage.py shell命令有一个重要的不同: 在启动解释器之前,它 ...

  9. nginx+keepalived+tomcat实现主从高可用负载均衡

    设备: 1.准备四台虚拟机,两台tomcat,两台nginx 2.两台tomcat配置相同,测试页不同 两台Tomcat配置完全相同.只有测试页面不同 安装jdk和tomcat [root@local ...

  10. js中的局部函数和全局函数的调用

    //局部函数和全局函数的特点 function fc1(){ var name ="chenhao"; function fc2(){ var age = 30; alert(na ...