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. springboot集成实现秒杀

    springboot集成开发实现商场秒杀 加入主要依赖 <dependency> <groupId>org.springframework.boot</groupId&g ...

  2. JAVA基础学习(6)之使用对象

    6使用对象 6.1字符类型 6.1.1字符类型 char和int互相转换 //a比A大32 Scanner in=new Scanner(System.in); char c='B'; char c1 ...

  3. appium可通过SDK自带的uiautomatorviewer或monitor工具,来查看页面元素(Android)

    工具一:uiautomatorviewer 1.在SDK的tools目录中找到uiautomatorviewer,双击打开若出现闪退一般是jdk版本不匹配(建议安装jdk1.8的): 2.在使用这个工 ...

  4. 我来给你讲清楚Pythony广播

    初学python广播搞的人头大,今天老师上课讲了一下,茅塞顿开,zt老师nb 首先说一下后向纬度(这个后向纬度书里边称作低维),举例:(3,4,5)后向纬度是:3*4*5或4*5或5 向量广播的条件有 ...

  5. C语言-对字符串二维数组各个元素进行比较-十进制数转化为其他进制数-进行规则矩阵的输出-190225

    //编写一个函数:从传入的num个字符中找到最长的一个字符,并通过max传回该串地址. //重点:切记这里a[0]就是一个地址. #include<conio.h> #include< ...

  6. php编译安装扩展redis及swoole

    一.安装redis扩展 下载redis扩展包以及解压 wget https://github.com/edtechd/phpredis/archive/php7.zip unzip php7.zip ...

  7. Windows 查看并关闭占用指定端口的程序

    windows关闭端口的小工具: 链接:https://pan.baidu.com/s/1ZGL4cdSluy0lbi3tDERUvA 提取码:spxy 查看指定端口的使用情况 netstat -an ...

  8. 【渗透测试】Msf提权步骤

    1.生成反弹木马(脚本,执行程序) msfvenom -p windows/meterpreter/reverse_tcp LHOST=<Your IP Address> LPORT=&l ...

  9. 吴裕雄 Bootstrap 前端框架开发——简介

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  10. 到底是哪个“O”管理内部人员风险?

    导读 俗话说,家和万事兴.与之相对的,家不睦则必自败.同理,如果缺乏明确的领导,内部人员风险管理项目或内部人威胁项目 (ITP) 也将走向失败. 俗话说,家和万事兴.与之相对的,家不睦则必自败.同理, ...