public String exportXls(HttpServletRequest request, HttpServletResponse response) {
try {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFRow row = null;
HSSFCell cell = null;
//建立新的sheet对象
HSSFSheet sheet = wb.createSheet("导出信息");//sheet的名称
sheet.setColumnWidth(0, 5*256);//设置第columnIndex+1列的列宽,单位为字符宽度的1/256
sheet.setColumnWidth(1, 30*256);
sheet.setColumnWidth(2, 20*256);
sheet.setColumnWidth(3, 10*256);
sheet.setColumnWidth(4, 10*256);
sheet.setColumnWidth(5, 10*256);
sheet.setColumnWidth(6, 10*256);
sheet.setColumnWidth(7, 10*256);
//标题样式
HSSFCellStyle titleStyle = wb.createCellStyle();
titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
org.apache.poi.ss.usermodel.Font ztFont = wb.createFont();
ztFont.setItalic(false);
ztFont.setColor(org.apache.poi.ss.usermodel.Font.COLOR_NORMAL);
ztFont.setFontHeightInPoints((short) 16);
ztFont.setFontName("宋体");
ztFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); titleStyle.setFont(ztFont); //----------------二级标题格样式----------------------------------
HSSFCellStyle titleStyle2 = wb.createCellStyle(); //表格样式
titleStyle2.setAlignment(HSSFCellStyle.ALIGN_LEFT);
titleStyle2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
HSSFFont ztFont2 = wb.createFont();
ztFont2.setItalic(false); // 设置字体为斜体字
ztFont2.setColor(org.apache.poi.ss.usermodel.Font.COLOR_NORMAL);
ztFont2.setFontHeightInPoints((short)11); // 将字体大小设置为18px
ztFont2.setFontName("宋体"); // 字体应用到当前单元格上
titleStyle2.setFont(ztFont2);
//----------------------------------------------------------
//----------------单元格样式----------------------------------
HSSFCellStyle cellStyle = wb.createCellStyle(); //表格样式
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
HSSFFont cellFont = wb.createFont();
cellFont.setItalic(false); // 设置字体为斜体字
cellFont.setColor(org.apache.poi.ss.usermodel.Font.COLOR_NORMAL);
cellFont.setFontHeightInPoints((short)10); // 将字体大小设置为18px
cellFont.setFontName("宋体"); // 字体应用到当前单元格上
cellStyle.setFont(cellFont);
cellStyle.setWrapText(true);//设置自动换行
//----------------------------------------------------------
// ----------------------创建第一行---------------
// 在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个
row = sheet.createRow(0);
// 创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个
cell = row.createCell(0);
// 合并单元格CellRangeAddress构造参数依次表示 起始行,截至行,起始列, 截至列
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 7));
// 设置单元格内容
cell.setCellValue("测试excel");
cell.setCellStyle(titleStyle);
// ---------------------------------------------- // ------------------创建第二行()---------------------
row = sheet.createRow(1); // 创建第二行
cell = row.createCell(0);
sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 1));
cell.setCellValue("入库单号:15203");
cell.setCellStyle(titleStyle2); cell = row.createCell(2);
sheet.addMergedRegion(new CellRangeAddress(1, 1, 2, 4));
cell.setCellValue("执行单号:14520");
cell.setCellStyle(titleStyle2); cell = row.createCell(5);
sheet.addMergedRegion(new CellRangeAddress(1, 1, 5, 7));
String createDate = MapUtil.getRealValue(headInfo, "create_date", "");
cell.setCellValue("创建时间:2018-06-19 13:59:23");
cell.setCellStyle(titleStyle2); //---------------------创建第三行------------------------------------- row = sheet.createRow(2);
cell = row.createCell(0);
sheet.addMergedRegion(new CellRangeAddress(2, 2, 0, 1));
cell.setCellValue("供应商:京东");
cell.setCellStyle(titleStyle2); cell = row.createCell(2);
sheet.addMergedRegion(new CellRangeAddress(2, 2, 2, 4));
cell.setCellValue("仓库名称:一号仓库");
cell.setCellStyle(titleStyle2); cell = row.createCell(5);
sheet.addMergedRegion(new CellRangeAddress(2, 2, 5, 7));
cell.setCellValue("总金额:23.65");
cell.setCellStyle(titleStyle2); // ------------------创建表头start---------------------
row = sheet.createRow(3); // 创建第四行
sheet.addMergedRegion(new CellRangeAddress(3, 3, 0, 0));
cell = row.createCell(0);
cell.setCellValue("序号");
cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(3, 3, 1, 1));
cell = row.createCell(1);
cell.setCellValue("物料名称");
cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(3, 3, 2, 2));
cell = row.createCell(2);
cell.setCellValue("物料编号");
cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(3, 3, 3, 3));
cell = row.createCell(3);
cell.setCellValue("物料产地");
cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(3, 3, 4, 4));
cell = row.createCell(4);
cell.setCellValue("入库价格");
cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(3, 3, 4, 4));
cell = row.createCell(5);
cell.setCellValue("参考价格");
cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(3, 3, 5, 5));
cell = row.createCell(6);
cell.setCellValue("需求数量");
cell.setCellStyle(cellStyle); sheet.addMergedRegion(new CellRangeAddress(3, 3, 6, 6));
cell = row.createCell(7);
cell.setCellValue("总金额");
cell.setCellStyle(cellStyle);         //循环填充数据
int i =1;
int rowNum= 3;
List<Object> data = null;
for (Map m : mapList) {
data = new ArrayList<>();
data.add(i);
data.add((String) m.get("wuliao_name"));
data.add((String) m.get("wuliao_no"));
data.add((String) m.get("wuliao_address"));
data.add((Double) m.get("danjia"));
data.add((Double) m.get("reference_price"));
data.add((Integer) m.get("wuliao_count"));
data.add((Double) m.get("totalmoney")); rowNum ++; //从第四行开始
row = sheet.createRow(rowNum);
for (int j = 0; j < data.size(); j++) { //将数据添加到单元格中
sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum, j, j));
cell = row.createCell(j);
cell.setCellValue(""+data.get(j)+"");
cell.setCellStyle(cellStyle);
}
i++;
}
      //添加底部合计行
row = sheet.createRow(rowNum+1);
sheet.addMergedRegion(new CellRangeAddress(rowNum+1, rowNum+1, 0, 4));
cell = row.createCell(0);
cell.setCellStyle(cellStyle);
cell = row.createCell(1);
cell.setCellStyle(cellStyle);
cell = row.createCell(2);
cell.setCellStyle(cellStyle);
cell = row.createCell(3);
cell.setCellStyle(cellStyle);
cell = row.createCell(4);
cell.setCellStyle(cellStyle); cell = row.createCell(5);
cell.setCellStyle(cellStyle); cell = row.createCell(6);
cell.setCellValue(totalNumber);
cell.setCellStyle(cellStyle); cell = row.createCell(7);
cell.setCellValue(new BigDecimal(totalMoney).setScale(2, RoundingMode.DOWN).toString());
cell.setCellStyle(cellStyle); OutputStream output = response.getOutputStream();
response.reset();

        response.setHeader("Content-Disposition", "filename="

new String("测试导出".getBytes("gb2312"), "iso8859-1"))+".xls";//设置文件头编码方式和文件名

            response.setContentType("application/msexcel");
wb.write(output);
output.close();
} catch (Exception e) {
e.printStackTrace();
}
}

Apache POI导出excel的更多相关文章

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

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

  2. apache POI 导出excel相关方法

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

  3. apache poi导出excel报表

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

  4. Apache POI导出excel表格

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

  5. 使用org.apache.poi导出Excel表格

    public HSSFWorkbook MakeExcel(List<TransactionLogVO> logList) { // SimpleDateFormat sdf = new ...

  6. java 通过Apache poi导出excel代码demo实例

    package com.zuidaima.excel.util; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutput ...

  7. poi导出excel

    Java使用poi组件导出excel报表,能导出excel报表的还可以使用jxl组件,但jxl想对于poi功能有限,jxl应该不能载excel插入浮动层图片,poi能很好的实现输出excel各种功能, ...

  8. POI导出excel的简单demo

    目前使用过两种导出excel的方式,一种是如题所示的使用POI的方式进行数据的导出,这种方式一般只有在处理比较多的数据或者说需要导出的excel表格中有图片之类的需要特殊处理的文件的时候使用:还有一种 ...

  9. [转载]poi导出excel,可以自定义保存路径

    poi导出excel比js导出excel安全性更好,在使用poi导出excel时,先要导入poi-3.5-FINAL-20090928.jar包到你项目的lib目录下,我这里选择是3.5版的 1.ac ...

随机推荐

  1. java socket编程(也是学习多线程的例子)详细版----转

    7.2 面向套接字编程    我们已经通过了解Socket的接口,知其所以然,下面我们就将通过具体的案例,来熟悉Socket的具体工作方式 7.2.1使用套接字实现基于TCP协议的服务器和客户机程序  ...

  2. Delphi for iOS开发指南(7):在iOS应用程序中使用WebBrowser组件

    Delphi for iOS开发指南(7):在iOS应用程序中使用WebBrowser组件 在FireMonkey iOS应用程序中使用WebBrowser 在iOS平台上,FireMonkey使用T ...

  3. Android SQLiteOpenHelper Sqlite数据库升级onUpgrade

    Android Sqlite数据库升级,在Android APP开发之中,非常常见: 在确定原来的数据库版本号之后,在原来数据库版本号+1,就会执行onUpgrade方法,进行数据库升级操作: 在on ...

  4. abp + angular $http + webapi 服务

    什么是angular $http服务 http是angularjs的一个核心服务,用于读取远程服务器的数据,也就是封装了浏览器原生的xhtmlrequest对象,可以直接同外部进行通信. 怎样使用an ...

  5. MaxScript调用DotNet时命名空间的问题

    Fn GetSpecialFolder argEnumName = (DotNetClass "System.Environment").GetFolderPath (Execut ...

  6. day111 爬虫第一天

    一.模拟浏览器发请求. import requests r1 =requests.get( url ="https://dig.chouti.com/", headers ={ & ...

  7. CRUSH map 定制实例解析

    1.提取已有的CRUSH map ,使用-o参数,ceph将输出一个经过编译的CRUSH map 到您指定的文件ceph osd getcrushmap -o crushmap.txt 2.反编译你的 ...

  8. nodejs 像 C 语言那样输出当前代码的行数

    http://stackoverflow.com/questions/11386492/accessing-line-number-in-v8-javascript-chrome-node-js Ob ...

  9. Educational Codeforces Round 34 (Rated for Div. 2) D - Almost Difference(高精度)

    D. Almost Difference Let's denote a function You are given an array a consisting of n integers. You ...

  10. Flask从入门到精通之模型定义

    模型这个术语表示程序使用的持久化实体.在ORM 中,模型一般是一个Python 类,类中的属性对应数据库表中的列. Flask-SQLAlchemy 创建的数据库实例为模型提供了一个基类以及一系列辅助 ...