public HSSFWorkbook MakeExcel(List<TransactionLogVO> logList) {

        // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/*response.reset();
response.setHeader("Content-Disposition", "attachment;fileName="+ fileName);// 指定下载的文件名
OutputStream output = response.getOutputStream();
BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output); */
// String path = this.getClass().getClassLoader().getResource("WEB-INF/tqjsxx").getPath();
// 定义单元格报头
String worksheetTitle = "交易日志信息"; HSSFWorkbook wb = new HSSFWorkbook(); // 创建单元格样式
HSSFCellStyle cellStyleTitle = wb.createCellStyle();
// 指定单元格居中对齐
cellStyleTitle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 指定单元格垂直居中对齐
cellStyleTitle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// 指定当单元格内容显示不下时不换行
cellStyleTitle.setWrapText(false);
// ------------------------------------------------------------------
HSSFCellStyle cellStyle = wb.createCellStyle();
// 指定单元格居中对齐
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 指定单元格垂直居中对齐
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// 指定当单元格内容显示不下时换行
cellStyle.setWrapText(true); // ------------------------------------------------------------------
// 设置单元格字体
HSSFFont font = wb.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setFontName("宋体");
font.setFontHeight((short) 200);
cellStyleTitle.setFont(font); // 工作表名
String id = "序号";
String channelCode = "渠道代码";
String channelName = "渠道名称";
String cityName = "所属城市";
String functionCode = "接口功能";
String logTime = "日志时间";
String reqMsgId = "消息ID";
String reqTime = "请求时间";
String reqMsg = "请求报文";
String respTime = "响应时间";
String resultCode = "处理代码";
String respMsg = "响应报文"; HSSFSheet sheet = wb.createSheet();
ExportExcel exportExcel = new ExportExcel(wb, sheet);
// 创建报表头部
exportExcel.createNormalHead(worksheetTitle, 11);
// 定义第一行
HSSFRow row1 = sheet.createRow(1);
HSSFCell cell1 = row1.createCell(0); //第一行第1列
cell1.setCellStyle(cellStyleTitle);
cell1.setCellValue(new HSSFRichTextString(id));
sheet.setColumnWidth(0, 7 * 256);
//第一行第2列
cell1 = row1.createCell(1);
cell1.setCellStyle(cellStyleTitle);
cell1.setCellValue(new HSSFRichTextString(channelCode));
//第一行第3列
cell1 = row1.createCell(2);
cell1.setCellStyle(cellStyleTitle);
cell1.setCellValue(new HSSFRichTextString(channelName)); //第一行第4列
cell1 = row1.createCell(3);
cell1.setCellStyle(cellStyleTitle);
cell1.setCellValue(new HSSFRichTextString(cityName)); //第一行第5列
cell1 = row1.createCell(4);
cell1.setCellStyle(cellStyleTitle);
cell1.setCellValue(new HSSFRichTextString(functionCode));
sheet.setColumnWidth(4, 20 * 256);
//第一行第6列
cell1 = row1.createCell(5);
cell1.setCellStyle(cellStyleTitle);
cell1.setCellValue(new HSSFRichTextString(logTime));
sheet.setColumnWidth(5, 20 * 256);
//第一行第7列
cell1 = row1.createCell(6);
cell1.setCellStyle(cellStyleTitle);
cell1.setCellValue(new HSSFRichTextString(reqMsgId));
sheet.setColumnWidth(6, 20 * 256);
//第一行第8列
cell1 = row1.createCell(7);
cell1.setCellStyle(cellStyleTitle);
cell1.setCellValue(new HSSFRichTextString(reqTime));
sheet.setColumnWidth(7, 20 * 256);
//第一行第9列
cell1 = row1.createCell(8);
cell1.setCellStyle(cellStyleTitle);
cell1.setCellValue(new HSSFRichTextString(reqMsg));
sheet.setColumnWidth(8, 40 * 256);
//第一行第10列
cell1 = row1.createCell(9);
cell1.setCellStyle(cellStyleTitle);
cell1.setCellValue(new HSSFRichTextString(respTime));
sheet.setColumnWidth(9, 20 * 256);
//第一行第11列
cell1 = row1.createCell(10);
cell1.setCellStyle(cellStyleTitle);
cell1.setCellValue(new HSSFRichTextString(resultCode));
sheet.setColumnWidth(10, 20 * 256);
//第一行第12列
cell1 = row1.createCell(11);
cell1.setCellStyle(cellStyleTitle);
cell1.setCellValue(new HSSFRichTextString(respMsg));
sheet.setColumnWidth(11, 40 * 256); //定义第二行开始后的实体内容
HSSFRow row = sheet.createRow(2);
HSSFCell cell = row.createCell(1);
for (int i = 0; i < logList.size(); i++) {
TransactionLogVO transactionLog = JSON.parseObject(JSONObject.toJSONString(logList.get(i)), TransactionLogVO.class); row = sheet.createRow(i + 2); cell = row.createCell(0);
cell.setCellStyle(cellStyle);
cell.setCellValue(new HSSFRichTextString(transactionLog.getId() + "")); cell = row.createCell(1);
cell.setCellStyle(cellStyle);
cell.setCellValue(new HSSFRichTextString(transactionLog.getChannelCode())); cell = row.createCell(2);
cell.setCellStyle(cellStyle);
cell.setCellValue(new HSSFRichTextString(transactionLog.getChannelName())); cell = row.createCell(3);
cell.setCellStyle(cellStyle);
cell.setCellValue(new HSSFRichTextString(transactionLog.getCityName())); cell = row.createCell(4);
cell.setCellStyle(cellStyle);
cell.setCellValue(new HSSFRichTextString(transactionLog.getFunctionCode())); cell = row.createCell(5);
cell.setCellStyle(cellStyle);
//cell.setCellValue(new HSSFRichTextString(sdf.format(transactionLog.getLogTime())));
cell.setCellValue(new HSSFRichTextString(timeStamp2Date(transactionLog.getLogTime()))); cell = row.createCell(6);
cell.setCellStyle(cellStyle);
cell.setCellValue(new HSSFRichTextString(transactionLog.getReqMsgId())); cell = row.createCell(7);
cell.setCellStyle(cellStyle);
//cell.setCellValue(new HSSFRichTextString(sdf.format(transactionLog.getReqTime())));
cell.setCellValue(new HSSFRichTextString(timeStamp2Date(transactionLog.getReqTime()))); cell = row.createCell(8);
cell.setCellStyle(cellStyle);
cell.setCellValue(new HSSFRichTextString(transactionLog.getReqMsg())); cell = row.createCell(9);
cell.setCellStyle(cellStyle);
//cell.setCellValue(new HSSFRichTextString(sdf.format(transactionLog.getRespTime())));
cell.setCellValue(new HSSFRichTextString(timeStamp2Date(transactionLog.getRespTime()))); cell = row.createCell(10);
cell.setCellStyle(cellStyle);
cell.setCellValue(new HSSFRichTextString(transactionLog.getResultCode())); cell = row.createCell(11);
cell.setCellStyle(cellStyle);
cell.setCellValue(new HSSFRichTextString(transactionLog.getRespMsg())); }
return wb;
}

导出表格工具类
 public class ExportExcel {  

     private HSSFWorkbook wb = null;
private HSSFSheet sheet = null; /**
* @param wb
* @param sheet
*/
public ExportExcel(HSSFWorkbook wb, HSSFSheet sheet) {
// super();
this.wb = wb;
this.sheet = sheet;
} /**
* 创建通用EXCEL头部
*
* @param headString
* 头部显示的字符
* @param colSum
* 该报表的列数
*/
public void createNormalHead(String headString, int colSum) {
HSSFRow row = sheet.createRow(0);
// 设置第一行
HSSFCell cell = row.createCell(0);
// row.setHeight((short) 1000); // 定义单元格为字符串类型
cell.setCellType(HSSFCell.ENCODING_UTF_16);// 中文处理
cell.setCellValue(new HSSFRichTextString(headString)); // 指定合并区域
/**
* public Region(int rowFrom, short colFrom, int rowTo, short colTo)
*/
sheet.addMergedRegion(new CellRangeAddress(0, (short) 0, (short) 0, (short) colSum)); // 定义单元格格式,添加单元格表样式,并添加到工作簿
HSSFCellStyle cellStyle = wb.createCellStyle();
// 设置单元格水平对齐类型
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 指定单元格居中对齐
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 指定单元格垂直居中对齐
cellStyle.setWrapText(true);// 指定单元格自动换行 // 设置单元格字体
HSSFFont font = wb.createFont();
// font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// font.setFontName("宋体");
// font.setFontHeight((short) 600);
// cellStyle.setFont(font);
cell.setCellStyle(cellStyle);
} /**
* 创建通用报表第二行
*
* @param params
* 统计条件数组
* @param colSum
* 需要合并到的列索引
*/
public void createNormalTwoRow(String[] params, int colSum) {
// 创建第二行
HSSFRow row1 = sheet.createRow(1); row1.setHeight((short) 400); HSSFCell cell2 = row1.createCell(0); cell2.setCellType(HSSFCell.ENCODING_UTF_16);
cell2.setCellValue(new HSSFRichTextString("时间:" + params[0] + "至"
+ params[1])); // 指定合并区域
/**
* public Region(int rowFrom, short colFrom, int rowTo, short colTo)
*/
sheet.addMergedRegion(new CellRangeAddress(1, 1, (short) 0, (short) colSum)); HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 指定单元格居中对齐
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 指定单元格垂直居中对齐
cellStyle.setWrapText(true);// 指定单元格自动换行 // 设置单元格字体
HSSFFont font = wb.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setFontName("宋体");
font.setFontHeight((short) 250);
cellStyle.setFont(font); cell2.setCellStyle(cellStyle);
} /**
* 设置报表标题
*
* @param columHeader
* 标题字符串数组
*/
public void createColumHeader(String[] columHeader) { // 设置列头 在第三行
HSSFRow row2 = sheet.createRow(2); // 指定行高
row2.setHeight((short) 600); HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 指定单元格居中对齐
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 指定单元格垂直居中对齐
cellStyle.setWrapText(true);// 指定单元格自动换行 // 单元格字体
HSSFFont font = wb.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setFontName("宋体");
font.setFontHeight((short) 250);
cellStyle.setFont(font); // 设置单元格背景色
cellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); HSSFCell cell3 = null; for (int i = 0; i < columHeader.length; i++) {
cell3 = row2.createCell(i);
cell3.setCellType(HSSFCell.ENCODING_UTF_16);
cell3.setCellStyle(cellStyle);
cell3.setCellValue(new HSSFRichTextString(columHeader[i]));
}
} /**
* 创建内容单元格
*
* @param wb
* HSSFWorkbook
* @param row
* HSSFRow
* @param col
* short型的列索引
* @param align
* 对齐方式
* @param val
* 列值
*/
public void cteateCell(HSSFWorkbook wb, HSSFRow row, int col, short align,
String val) {
HSSFCell cell = row.createCell(col);
cell.setCellType(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(new HSSFRichTextString(val));
HSSFCellStyle cellstyle = wb.createCellStyle();
cellstyle.setAlignment(align);
cell.setCellStyle(cellstyle);
} /**
* 创建合计行
*
* @param colSum
* 需要合并到的列索引
* @param cellValue
*/
public void createLastSumRow(int colSum, String[] cellValue) { HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 指定单元格居中对齐
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 指定单元格垂直居中对齐
cellStyle.setWrapText(true);// 指定单元格自动换行 // 单元格字体
HSSFFont font = wb.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setFontName("宋体");
font.setFontHeight((short) 250);
cellStyle.setFont(font);
// 获取工作表最后一行
HSSFRow lastRow = sheet.createRow((short) (sheet.getLastRowNum() + 1));
HSSFCell sumCell = lastRow.createCell(0); sumCell.setCellValue(new HSSFRichTextString("合计"));
sumCell.setCellStyle(cellStyle);
// 合并 最后一行的第零列-最后一行的第一列
sheet.addMergedRegion(new CellRangeAddress(sheet.getLastRowNum(), sheet.getLastRowNum(),
(short) 0, (short) colSum));// 指定合并区域 for (int i = 2; i < (cellValue.length + 2); i++) {
// 定义最后一行的第三列
sumCell = lastRow.createCell(i);
sumCell.setCellStyle(cellStyle);
// 定义数组 从0开始。
sumCell.setCellValue(new HSSFRichTextString(cellValue[i - 2]));
}
} /**
* 输入EXCEL文件
*
* @param fileName
* 文件名
*/
public void outputExcel(String fileName) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File(fileName));
wb.write(fos);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} /**
* @return the sheet
*/
public HSSFSheet getSheet() {
return sheet;
} /**
* @param sheet
* the sheet to set
*/
public void setSheet(HSSFSheet sheet) {
this.sheet = sheet;
} /**
* @return the wb
*/
public HSSFWorkbook getWb() {
return wb;
} /**
* @param wb
* the wb to set
*/
public void setWb(HSSFWorkbook wb) {
this.wb = wb;
}
}

调用方法

  @RequestMapping("/export")
public void export(TransactionLogVO transactionlog, HttpServletRequest request, HttpServletResponse response) {
ResultMessage<DataGridVO> queryRs = transactionLogServiceImpl.list(transactionlog);
//System.out.println(queryRs.toString());
//Map<?, ?> resultMap = JSON.parseObject(queryRs.toString());
List<TransactionLogVO> resultL = new ArrayList<TransactionLogVO>();
//if ((Integer) resultMap.get("errcode") == 0) {
resultL = (List<TransactionLogVO>) queryRs.getObj().getRows();
// }
if (resultL == null || resultL.size() == 0) {
resultL = new ArrayList<TransactionLogVO>();
}
System.out.println("resultL:" + resultL.toString());
HSSFWorkbook wb = MakeExcel(resultL);
String fileName = "交易日志.xls";
try {
fileName = new String(fileName.getBytes("utf-8"), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println("fileName:" + fileName);
response.reset();
response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);// 指定下载的文件名
response.setContentType("application/vnd.ms-excel;charset=utf-8");
try {
OutputStream output = response.getOutputStream();
wb.write(output);
output.flush();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
} 需要在pom文件加入POI依赖

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.10.1</version>
</dependency>


使用org.apache.poi导出Excel表格的更多相关文章

  1. Apache POI导出excel表格

    项目中我们经常用到导出功能,将数据导出以便于审查和统计等.本文主要使用Apache POI实现导出数据. POI中文文档 简介 ApachePOI是Apache软件基金会的开放源码函式库,POI提供A ...

  2. 使用Apache POI导出Excel小结--导出XLS格式文档

    使用Apache POI导出Excel小结 关于使用Apache POI导出Excel我大概会分三篇文章去写 使用Apache POI导出Excel小结--导出XLS格式文档 使用Apache POI ...

  3. apache poi导出excel报表

    Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能.POI为"P ...

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

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

  5. 使用poi导出Excel表格,jar包冲突

    HTTP Status 500 – Internal Server Error Type Exception Report Message Handler processing failed; nes ...

  6. 复杂的POI导出Excel表格(多行表头、合并单元格)

    poi导出excel有两种方式: 第一种:从无到有的创建整个excel,通过HSSFWorkbook,HSSFSheet HSSFCell, 等对象一步一步的创建出工作簿,sheet,和单元格,并添加 ...

  7. apache POI 导出excel相关方法

    apache POI 操作excel无比强大.同时有操作word和ppt的接口. 下面讲解poi中常用方法. 1,设置列宽 HSSFSheet sheet = wb.getSheetAt(0); sh ...

  8. Apache POI导出excel

    public String exportXls(HttpServletRequest request, HttpServletResponse response) { try { HSSFWorkbo ...

  9. poi导出excel表格

    package poiexcel; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; ...

随机推荐

  1. P3239 [HNOI2015]亚瑟王

    思路 神仙概率dp 由于期望的线性性质,能够想到最后要求的期望价值就是把每个卡牌发动的概率\(g_i\)乘上伤害\(val_i\)之后加到一起 然后怎么求\(g_i\)呢,肯定是要dp的 我想了例如d ...

  2. Docker:Service

    Prerequisites Install Docker version 1.13 or higher. Get Docker Compose. On Docker for Mac and Docke ...

  3. oracle批量删除某用户下的表

    昨天干了一天的体力活,到快下班时被要求删除一批测试库上错误的表,主要是这些表的字段和生产上字段顺序对不上,然后让我写个脚本,让dba执行一下,主要是删表这种东西我们都没权限. 然后,我就被难到了,我记 ...

  4. 转 Failed to run the WC DB work queue associated with 错误的解决

    svn 异常终止导致的缓存工作队列问题 解决方法:清空svn的队列 1.下载sqlite3.exe 2.找到你项目的.svn文件,查看是否存在wc.db 3.将sqlite3.exe放到.svn的同级 ...

  5. 转youhu科技的文章 勿怪 感激 感激

    资源加载 资源加载是加载模块中最为耗时的部分,其CPU开销在Unity引擎中主要体现在Loading.UpdatePreloading和Loading.ReadObject两项中,相信经常查看Prof ...

  6. python3使用pymysql模块,连接mysql数据库,实现新增、查询和更新操作

    1.环境数据准备: python3环境.pymysql模块 mysql数据库:本次代码中用到的数据库为本地的testdb数据库,user表(表字段比较简单,只有主键id,手机号mobile,密码pas ...

  7. VC.【转】窗口置于前台并激活的方法

    1.VC 窗口置于前台并激活的方法 - CSDN博客.html https://blog.csdn.net/oXunFeng/article/details/52681279 2.(http://ww ...

  8. [转]xml解析工具的效率比较QDomDocument、TinyXml-2、RapidXml、PugiXml

    转自:http://www.itdaan.com/blog/2017/02/20/301ad47832f4.html 由于windows环境下测试不稳定,博主选择在linux下进行的测试! Qt - ...

  9. openmodelica警告及错误

    Warning: The initial conditions are not fully specified. simulate(TCS.TCS,startTime=0,stopTime=200.0 ...

  10. Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 0 解决方法: 要对切割字符进行转义\\

    使用str.split("[",15)时,出现Exception in thread "main" java.util.regex.PatternSyntaxE ...