controller:

    /**
* 导出报表
* @return
*/
@RequestMapping(value = "/export")
@ResponseBody
public void export(HttpServletRequest request,HttpServletResponse response, String id, String auditno) throws Exception {
/*jxl
String headers = "";
String[] array = addEmpService.returnStringArray(id);
jxl.createTable(headers, array, "F:\\qq"+auditno+".xls");*/
//获取数据
List<EmpAuditDetails> list = addEmpService.queryDetailsById(id); //excel标题
String[] title = {"姓名","身份证号码","性别","出生日期","保险缴费基数","工种","增员年月","增员原因","手机号","紧急联系人","紧急联系电话","家庭住址","通讯地址省级","通讯地址市级","通讯地址区县级","通讯地址补充信息","户口所在地省级","户口所在地市级","户口所在地区县级","户口所在地补充信息"}; //excel文件名
String fileName = auditno+".xls"; //sheet名
String sheetName = auditno;
String [][] content = new String[list.size()][];
for (int i = ; i < list.size(); i++) {
content[i] = new String[title.length];
content[i][] = list.get(i).getName();
content[i][] = list.get(i).getIdno();
content[i][] = list.get(i).getSex();
content[i][] = list.get(i).getBirth();
content[i][] = String.valueOf(list.get(i).getInsuranceBase());
content[i][] = list.get(i).getJob();
content[i][] = DateUtils.convertDateToString(list.get(i).getAddYm(),"yyyy-MM-dd");
content[i][] = list.get(i).getAddReason();
content[i][] = list.get(i).getPhone();
content[i][] = list.get(i).getLink();
content[i][] = list.get(i).getLinkphone();
content[i][] = list.get(i).getAddress();
content[i][] = list.get(i).getProvince();
content[i][] = list.get(i).getCity();
content[i][] = list.get(i).getDistrict();
content[i][] = list.get(i).getAddressExtra();
content[i][] = list.get(i).getNativeProvince();
content[i][] = list.get(i).getNativeCity();
content[i][] = list.get(i).getNativeDistrict();
content[i][] = list.get(i).getNativeExtra(); } //创建HSSFWorkbook
HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null); //响应到客户端
try {
ResponseUtil.setResponseHeader(response, fileName);
OutputStream os = response.getOutputStream();
wb.write(os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}

excelutil

package com.lf.common.utils;

import java.sql.ResultSet;
import java.util.List;
import java.util.Map; import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook; public class ExcelUtil {
/**
* 导出Excel
* @param sheetName sheet名称
* @param title 标题
* @param values 内容
* @param wb HSSFWorkbook对象
* @return
*/
public static HSSFWorkbook getHSSFWorkbook(String sheetName,String []title,String [][]values, HSSFWorkbook wb){ // 第一步,创建一个HSSFWorkbook,对应一个Excel文件
if(wb == null){
wb = new HSSFWorkbook();
} // 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet(sheetName); // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
HSSFRow row = sheet.createRow(); // 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFFont font=(HSSFFont) wb.createFont();
font.setColor(HSSFColor.RED.index);
style.setFont(font);// 创建一个居中格式 //声明列对象
HSSFCell cell = null; //创建标题
for(int i=;i<title.length;i++){
cell = row.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(style);
}
//创建内容
for(int i=;i<values.length;i++){
row = sheet.createRow(i + );
for(int j=;j<values[i].length;j++){
//将内容按顺序赋给对应的列对象
row.createCell(j).setCellValue(values[i][j]);
}
}
return wb;
}
}
ResponseUtil:
package com.lf.common.utils;

import org.apache.poi.ss.usermodel.Workbook;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException; public class ResponseUtil {
public static void write(HttpServletResponse response, Object o) throws Exception {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println(o.toString());
out.flush();
out.close();
} public static void export(HttpServletResponse response, Workbook wb, String fileName) throws Exception{
//设置头 固定格式
response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("utf-8"), "iso8859-1")); response.setContentType("text/html;charset=utf-8"); OutputStream out = response.getOutputStream();
wb.write(out);
out.flush();
out.close();
}
//发送响应流方法
public static 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();
}
}
}

jxl:

package  com.lf.common.utils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set; import org.springframework.stereotype.Service; import com.lf.utils.DateUtils; import jxl.CellView;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.UnderlineStyle;
import jxl.read.biff.BiffException;
import jxl.write.Border;
import jxl.write.BorderLineStyle;
import jxl.write.Colour;
import jxl.write.Label;
import jxl.write.NumberFormats;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
@Service
public class JxlWriteExcel {
public boolean createTable(String header, String[] body, String filePath) {
boolean createFlag = true;
WritableWorkbook book;
try {
// 根据路径生成excel文件
book = Workbook.createWorkbook(new File(filePath)); // 创建一个sheet名为"表格"
WritableSheet sheet = book.createSheet("表格", );
// 设置NO列宽度 // 去掉整个sheet中的网格线
sheet.getSettings().setShowGridLines(false);
Label tempLabel = null;
// 表头输出
String[] headerArr = header.split(",");
int headerLen = headerArr.length;
// 循环写入表头内容
for (int i = ; i < headerLen; i++) {
tempLabel = new Label( i, , headerArr[i],
getHeaderCellStyle());
sheet.addCell(tempLabel);
sheet.setColumnView(i, );
}
// 表体输出
int bodyLen = body.length;
// 循环写入表体内容
for (int j = ; j < bodyLen; j++) {
String[] bodyTempArr = body[j].split(",");
for (int k = ; k < bodyTempArr.length; k++) {
WritableCellFormat tempCellFormat = null;
tempCellFormat = getBodyCellStyle();
if (tempCellFormat != null) {
if (k == || k == (bodyTempArr.length - )) {
tempCellFormat.setAlignment(Alignment.CENTRE);
}
}
tempLabel = new Label( k, + j, bodyTempArr[k],
tempCellFormat);
sheet.addCell(tempLabel);
}
}
book.write();
book.close();
} catch (IOException e) {
createFlag = false;
System.out.println("EXCEL创建失败!");
e.printStackTrace();
} catch (RowsExceededException e) {
createFlag = false;
System.out.println("EXCEL单元设置创建失败!");
e.printStackTrace();
} catch (WriteException e) {
createFlag = false;
System.out.println("EXCEL写入失败!");
e.printStackTrace();
} return createFlag;
}
public boolean createErroeTable(String header, String[] body, String filePath ,String sheetName,int sheetNum) {
boolean createFlag = true;
WritableWorkbook book ;
WritableSheet sheet = null ;
File errorFile = new File(filePath);
try {
if (errorFile.exists()) {
Workbook workbook = Workbook.getWorkbook(errorFile);
book = workbook.createWorkbook(errorFile,workbook);
sheet = book.createSheet(sheetName, );
}else {
// 根据路径生成excel文件
book = Workbook.createWorkbook(errorFile); // 创建一个sheet名为"表格"
sheet = book.createSheet(sheetName, sheetNum);
}
// 设置NO列宽度 // 去掉整个sheet中的网格线
sheet.getSettings().setShowGridLines(false);
Label tempLabel = null;
// 表头输出
String[] headerArr = header.split(",");
int headerLen = headerArr.length;
// 循环写入表头内容
for (int i = ; i < headerLen; i++) {
tempLabel = new Label( i, , headerArr[i],
getHeaderCellStyle());
sheet.addCell(tempLabel);
sheet.setColumnView(i, );
}
// 表体输出
int bodyLen = body.length;
// 循环写入表体内容
for (int j = ; j < bodyLen; j++) {
String[] bodyTempArr = body[j].split(",");
for (int k = ; k < bodyTempArr.length; k++) {
WritableCellFormat tempCellFormat = null;
tempCellFormat = getBodyCellStyle();
if (tempCellFormat != null) {
if (k == || k == (bodyTempArr.length - )) {
tempCellFormat.setAlignment(Alignment.CENTRE);
}
}
tempLabel = new Label( k, + j, bodyTempArr[k],
tempCellFormat);
sheet.addCell(tempLabel);
}
}
book.write();
book.close();
} catch (IOException e) {
createFlag = false;
System.out.println("EXCEL创建失败!");
e.printStackTrace();
} catch (RowsExceededException e) {
createFlag = false;
System.out.println("EXCEL单元设置创建失败!");
e.printStackTrace();
} catch (WriteException e) {
createFlag = false;
System.out.println("EXCEL写入失败!");
e.printStackTrace();
} catch (BiffException e) {
System.out.println("读取EXCEL对象失败!");
e.printStackTrace();
} return createFlag;
}
public WritableCellFormat getHeaderCellStyle() {
WritableFont font = new WritableFont(WritableFont.createFont("宋体"), ,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE);
WritableCellFormat headerFormat = new WritableCellFormat(
NumberFormats.TEXT);
try {
// 添加字体设置
headerFormat.setFont(font);
// 设置单元格背景色:表头为黄色
headerFormat.setBackground(Colour.YELLOW);
// 设置表头表格边框样式
// 整个表格线为粗线、黑色
headerFormat.setBorder(Border.ALL, BorderLineStyle.THICK,
Colour.BLACK);
// 表头内容水平居中显示
headerFormat.setAlignment(Alignment.CENTRE);
} catch (WriteException e) {
System.out.println("表头单元格样式设置失败!");
}
return headerFormat;
} public WritableCellFormat getBodyCellStyle() {
WritableFont font = new WritableFont(WritableFont.createFont("宋体"), ,
WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE);
WritableCellFormat bodyFormat = new WritableCellFormat(font);
try {
// 设置单元格背景色:表体为白色
bodyFormat.setBackground(Colour.WHITE);
// 设置表头表格边框样式
// 整个表格线为细线、黑色
bodyFormat
.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
} catch (WriteException e) {
System.out.println("表体单元格样式设置失败!");
}
return bodyFormat;
}
public void deleteFile(String fileName){
File file = new File(fileName);
if(file.exists()){
file.delete();
System.out.println("删除文件成功");
}else{
System.out.println("文件不存在");
}
}
/**
* 创建表格
* @module:
* @author: ZhangK
* @date: Aug 1, 2016
*/
public String createTableByZk(String header, String[] body, String filePath) {
WritableWorkbook book; String date = DateUtils.convertDateToString(new Date(), "yyyyMMddHHmmssSSS");
String fileName=date+".xls";
File file = null;
if (fileName !=null|| "".equals(fileName)) {
file = new File(fileName);
}else{
return null;
}
try {
// 根据路径生成excel文件
book = Workbook.createWorkbook(file);
// 创建一个sheet名为"表格"
WritableSheet sheet = book.createSheet("发货情况", );
// 去掉整个sheet中的网格线
sheet.getSettings().setShowGridLines(false);
Label tempLabel = null;
// 表头输出
String[] headerArr = header.split(",");
int headerLen = headerArr.length;
// 循环写入表头内容
CellView cv = null;
for (int i = ; i < headerLen; i++) {
tempLabel = new Label(i, , headerArr[i],
getHeaderCellStyle());
sheet.addCell(tempLabel);
// 设置NO列宽度
sheet.setColumnView(i, );
}
// 表体输出
int bodyLen = body.length; // 循环写入表体内容
for (int j = ; j < bodyLen; j++) {
String[] bodyTempArr = body[j].split(",");
for (int k = ; k < bodyTempArr.length; k++) {
WritableCellFormat tempCellFormat = null;
tempCellFormat = getBodyCellStyle();
if (tempCellFormat != null) {
if (k == || k == (bodyTempArr.length - )) {
tempCellFormat.setAlignment(Alignment.CENTRE);
}
}
tempLabel = new Label( k, + j, bodyTempArr[k],
tempCellFormat);
sheet.addCell(tempLabel);
}
}
book.write();
book.close();
} catch (IOException e) {
fileName = null;
System.out.println("EXCEL创建失败!");
e.printStackTrace();
} catch (RowsExceededException e) {
fileName = null;
System.out.println("EXCEL单元设置创建失败!");
e.printStackTrace();
} catch (WriteException e) {
fileName = null;
System.out.println("EXCEL写入失败!");
e.printStackTrace();
} return fileName;
} /**
* list<map>去重
* @param list
* @param param_list
* @return
*/
public String[] RemoveDuplicate(List<Map<String, String>> list,List<String> param_list) {
List<Map<String, String>> listMap = new ArrayList<Map<String, String>>();
Set<Map> setMap = new HashSet<Map>();
for (Map<String, String> map1 : list) {
if (setMap.add(map1)) {
listMap.add(map1);
}
}
String[] rowData_retail = new String[listMap.size()];
for (int i = ; i < listMap.size(); i++) {
Map<String, String> map = listMap.get(i);
for (int j = ; j < param_list.size(); j++) {
String param_i = map.get(param_list.get(j));
param_i = map.get(param_list.get(j)) == null ? "" : map
.get(param_list.get(j));
if (j == param_list.size() -) {
rowData_retail[i] = rowData_retail[i] + param_i ;
}else if (j==) {
rowData_retail[i] = param_i + ",";
}else {
rowData_retail[i] = rowData_retail[i] + param_i + ",";
} } }
return rowData_retail;
}
public static void main(String[] args) throws IOException {}
}
												

poi 和jxl导出excel(2)的更多相关文章

  1. Java中使用poi导入、导出Excel

    一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已经习惯用Excel打印.这样在我们实际 ...

  2. Java使用POI实现数据导出excel报表

    Java使用POI实现数据导出excel报表 在上篇文章中,我们简单介绍了java读取word,excel和pdf文档内容 ,但在实际开发中,我们用到最多的是把数据库中数据导出excel报表形式.不仅 ...

  3. POI导入和导出Excel总结

    POI导入和导出Excel总结   POI使用总结 1.POI读取Excel 打开工作簿的方式有以下两种简单的应用,POI读取和输出工作簿文件都可以通过以下两种方式来声明: //通过输入流的方式打开本 ...

  4. JXL导出Excel(只支持xls版本)——(一)

    注意: 导出的后缀是xls可以直接打开,如果导出的后缀是xlsx打开报错,需要手动将名字改为xls才可以打开.也就是JXL不可以导出xlsx的excel. Jar包

  5. jxl导出excel的问题

    jxl导出excel,通常浏览器会提示excel导出完成情况及默认保存路径,或让用户自定义选择保存路径,要达到这种效果,有些要做下修改,如:response是jsp的内置对象,在jsp中使用时不用声明 ...

  6. apache poi根据模板导出excel

    需要预先新建编辑好一个excel文件,设置好样式. 编辑好输出的数据,根据excel坐标一一对应. 支持列表数据输出,列表中列合并. 代码如下: package com.icourt.util; im ...

  7. Java web项目JXl导出excel,(从eclipse上移动到tomact服务器上,之路径更改)

    我用的是jxl导出excel,比较简单,最开始我是固定路径不能选择,很局限,后来改了,而且固定路径当把项目放在服务器上时,路径不可行. 在网上各位大神的帮助成功设置响应头,并且可选保存路径. 1.前端 ...

  8. jxl导出Excel中需要跳过的的坑

    正如上篇文章(JXL封装不能使用static关键字问题)所说,在jxl导出Excel时候如果频繁使用到WritableCellFormat去设置单元格的样式,这个时候经常会出现两种情况: 1.报警告: ...

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

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

随机推荐

  1. World Cup(The 2016 ACM-ICPC Asia China-Final Contest dfs搜索)

    题目: Here is World Cup again, the top 32 teams come together to fight for the World Champion. The tea ...

  2. 首次开通blog,以后会慢慢把oneNote和印象笔记的笔记转过来

    之前一直都是把笔记和经验写到OneNote上面,终于,OneNote无法同步了,转战blog吧,在这上面写一些笔记和一些技术,有空可以慢慢回顾一下.笔记这种东西,过一段时间再看就会有不同的感悟.

  3. 39页第7题 计算2的i次方之和

    /*计算2的i次方之和*/ #include<stdio.h> #include<math.h>/*调用math.h文件中的函数*/ int main(void) { int ...

  4. average column data from multiple files

    example in file a, data is [1 , 2, 3; 4,5,6] file b, data is  [4,5, 6; 7,8,9] average=0.5 (a+b) matl ...

  5. SecureCRT 8.0设置与使用

    1.设置回看缓冲信息的行数:

  6. cookie & cookies

    cookie & cookies "use strict"; /** * * @author xgqfrms * @license MIT * @copyright xgq ...

  7. Codeforces Round #260 (Div. 2) D

    D. A Lot of Games time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  8. vue.js组件之间通讯--父组件调用子组件的一些方法,子组件暴露一些方法,让父组件调用

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  9. IT学子成长指导类文章链接(十二)

    链接:IT学子成长指导类文章链接(一)(二)(三) (四) (五)(六)(七)(八)(九)(十)(十一) "IT学子成长指导"类我收藏过的好文(十二期:至2014年6月17日) 程 ...

  10. nginx源代码分析--框架设计 &amp; master-worker进程模型

    Nginx的框架设计-进程模型 在这之前,我们首先澄清几点事实: nginx作为一个高性能server的特点.事实上这也是全部的高性能server的特点,依赖epoll系统调用的高效(高效是相对sel ...