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. Android 的 ListView 的CheckBox标题栏显示文本之后显示单选框

    https://blog.csdn.net/u013790519/article/details/50036223 2.CheckBox的android:button=”@null”属性代码设置如下: ...

  2. JAVA JDBC 增删改查简单例子

    1.数据库配置文件jdbc.properties driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test username= ...

  3. Java中常用的方法

    枚举: 1:DemoEnum.valueOf(String str) 从枚举类中中找字符串,如果有则返回对应枚举值 2:DemoEnum.values() 获得枚举集合 3:DemoEnum.prop ...

  4. demoshow - webdemo展示助手

    demoshow - web demo展示助手 动态图演示页面: http://www.cnblogs.com/daysme/p/6790829.html 一个用来展示前端网页demo的小“助手”,提 ...

  5. angularjs启动项目报ERROR in AppModule is not an NgModule解决方法

    这主要是ts编译器版本问题,一般是因为ts编译器版本过高导致. 解决方式: npm uninstall -g typescript npm install -g typescript tsc -v 查 ...

  6. Git 分支 - 远程分支

    Git 分支 - 远程分支 远程分支 远程分支(remote branch)是对远程仓库中的分支的索引.它们是一些无法移动的本地分支:只有在 Git 进行网络交互时才会更新.远程分支就像是书签,提醒着 ...

  7. Winform 设置控件值

    private void SetControlValue(Control control, object value) { try { control.FindForm().Invoke((Actio ...

  8. 每日质量NPM包拖拽文件上传_react-dropzone

    一.react-dropzone 官方定义: Simple HTML5-compliant drag'n'drop zone for files built with React.js. 理解: 一个 ...

  9. git项目,VSCode显示不同颜色块的含义

    一. 概念 代码里的左侧颜色标识: 红色,未加入版本控制; (刚clone到本地) 绿色,已经加入版本控制暂未提交; (新增部分) 蓝色,加入版本控制,已提交,有改动: (修改部分) 白色,加入版本控 ...

  10. 蚂蚁金服“定损宝”现身AI顶级会议NeurIPS

    小蚂蚁说: 长期以来,车险定损(通过现场拍摄定损照片确定车辆损失,以作为保险公司理赔的依据)是车险理赔中最为重要的操作环节.以往传统保险公司的车险处理流程,一般为报案.现场查勘.提交理赔材料.审核.最 ...