Java带样式导出excle(poi)
来源:互联网 发布:压缩空气管道计算软件 编辑:程序博客网 时间:2024/06/13 21:16
1.工具类
package com.hand.util;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.HSSFPalette;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
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.util.CellRangeAddress;
import com.hand.pojo.ComplianceAlarm;
/**
* 导出Excel公共方法
*
* @version 1.0
*
* @author zsj
*
*/
public class ExportExcel {
// 显示的导出表的标题
private String title;
// 导出表的列名
private String[] rowName;
private List<ComplianceAlarm> dataList = new ArrayList<ComplianceAlarm>();
HttpServletResponse response;
// 构造方法,传入要导出的数据
public ExportExcel(String title, String[] rowName, List<ComplianceAlarm> dataList) {
this.dataList = dataList;
this.rowName = rowName;
this.title = title;
}
/*
* 导出数据
*/
public void export(String fileName,List<Integer> columnWidthList, HttpServletRequest request, HttpServletResponse response) throws Exception {
try {
HSSFWorkbook workbook = new HSSFWorkbook(); // 创建工作簿对象
HSSFSheet sheet = workbook.createSheet(title); // 创建工作表
HSSFPalette palette = workbook.getCustomPalette(); //wb HSSFWorkbook对象
//设置颜色
//标题
palette.setColorAtIndex((short)10, (byte) (0xff & 27), (byte) (0xff & 102), (byte) (0xff & 142));
//列名
palette.setColorAtIndex((short)11, (byte) (0xff & 243), (byte) (0xff & 243), (byte) (0xff & 243));
//偶数行
palette.setColorAtIndex((short)12, (byte) (0xff & 235), (byte) (0xff & 243), (byte) (0xff & 255));
//列宽设置
for(int i =0;i<columnWidthList.size();i++){
sheet.setColumnWidth(i, columnWidthList.get(i));
}
//设置行高
sheet.setDefaultRowHeightInPoints(20);
// 产生表格标题行
HSSFRow rowm = sheet.createRow(0);
HSSFCell cellTiltle = rowm.createCell(0);
// sheet样式定义【getColumnTopStyle()/getStyle()均为自定义方法 - 在下面 - 可扩展】
HSSFCellStyle titleStyle = this.getTitleStyle(workbook);// 标题样式对象
HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook);// 获取列头样式对象
HSSFCellStyle style1 = this.getStyle(workbook,1); // 基数行单元格样式对象
HSSFCellStyle style2 = this.getStyle(workbook,2); // 偶数行单元格样式对象
sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, (rowName.length - 1)));
cellTiltle.setCellStyle(titleStyle);
cellTiltle.setCellValue(title);
// 定义所需列数
int columnNum = rowName.length;
HSSFRow rowRowName = sheet.createRow(2); // 在索引2的位置创建行(最顶端的行开始的第二行)
// 将列头设置到sheet的单元格中
for (int n = 0; n < columnNum; n++) {
HSSFCell cellRowName = rowRowName.createCell(n); // 创建列头对应个数的单元格
cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING); // 设置列头单元格的数据类型
HSSFRichTextString text = new HSSFRichTextString(rowName[n]);
cellRowName.setCellValue(text); // 设置列头单元格的值
cellRowName.setCellStyle(columnTopStyle); // 设置列头单元格样式
}
// 将查询出的数据设置到sheet对应的单元格中
for (int i = 0; i < dataList.size(); i++) {
ComplianceAlarm ca= dataList.get(i);// 遍历每个对象
HSSFRow row = sheet.createRow(i + 3);// 创建所需的行数
for(int j=0; j<columnNum; j++){
HSSFCell cell = null; // 设置单元格的数据类型
cell = row.createCell(j, HSSFCell.CELL_TYPE_STRING);
//根据具体情况,设置单元格的内容
if(j==0){cell.setCellValue(ca.getIcuName()); // 设置单元格的值
}
if(j==1){
if(title.equals("告警详情")){
cell.setCellValue(ca.getAlarmTypeValue()); // 设置单元格的值
}else{
cell.setCellValue(ca.getTimeTypeValue()); // 设置单元格的值
}
}
if(j==2){
cell.setCellValue(DateUtil.format(ca.getGmtFrom(), "yyyy-MM-dd")+"至"+DateUtil.format(ca.getGmtTo(),"yyyy-MM-dd")); // 设置单元格的值
}
if(j==3){
cell.setCellValue(ca.getComplianceRate()+"%");
}
if(j==4){
cell.setCellValue(ca.getBenDeptRate()+"%");
}
if(j==5){
cell.setCellValue(ca.getWaiDeptRate()+"%");
}
//
if(i%2==0){cell.setCellStyle(style1); // 设置单元格样式
}else{
cell.setCellStyle(style2); // 设置单元格样式
}
}
}
if (workbook != null) {
try {
fileName = fileName + ".xls";
if ("FF".equals(getBrowser(request))) {
// 针对火狐浏览器防止乱码处理
fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
} else {
fileName = URLEncoder.encode(fileName, "UTF-8");// 文件名编码,防止乱码或者取不到中文字符
}
String headStr = "attachment; filename=\"" + fileName + "\"";
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition", headStr);
OutputStream out = response.getOutputStream();
workbook.write(out);
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 列头单元格样式
*/
public HSSFCellStyle getTitleStyle(HSSFWorkbook workbook) {
// 设置字体
HSSFFont font = workbook.createFont();
// 设置字体大小
font.setFontHeightInPoints((short) 14);
// 设置字体颜色
font.setColor(HSSFColor.WHITE.index);
// 字体加粗
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 设置字体名字
font.setFontName("Courier New");
// 设置样式;
HSSFCellStyle style = workbook.createCellStyle();
// 在样式用应用设置的字体;
style.setFont(font);
// 设置自动换行;
style.setWrapText(false);
// 设置水平对齐的样式为居中对齐;
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 设置垂直对齐的样式为居中对齐;
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
//设置标题背景色
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setFillForegroundColor((short)10);
return style;
}
/*
* 列头单元格样式
*/
public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) {
// 设置字体
HSSFFont font = workbook.createFont();
// 设置字体大小
font.setFontHeightInPoints((short) 12);
// 字体加粗
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 设置字体名字
font.setFontName("Courier New");
// 设置样式;
HSSFCellStyle style = workbook.createCellStyle();
// 设置底边框;
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
// 设置底边框颜色;
style.setBottomBorderColor(HSSFColor.BLACK.index);
// 设置左边框;
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
// 设置左边框颜色;
style.setLeftBorderColor(HSSFColor.BLACK.index);
// 设置右边框;
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
// 设置右边框颜色;
style.setRightBorderColor(HSSFColor.BLACK.index);
// 设置顶边框;
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
// 设置顶边框颜色;
style.setTopBorderColor(HSSFColor.BLACK.index);
// 在样式用应用设置的字体;
style.setFont(font);
// 设置自动换行;
style.setWrapText(false);
// 设置水平对齐的样式为居中对齐;
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 设置垂直对齐的样式为居中对齐;
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
//设置列头背景色
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setFillForegroundColor((short)11);
return style;
}
/*
* 列数据信息单元格样式
*/
public HSSFCellStyle getStyle(HSSFWorkbook workbook,int index) {
// 设置字体
HSSFFont font = workbook.createFont();
// 设置字体大小
font.setFontHeightInPoints((short)11);
// 字体加粗
// font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 设置字体名字
font.setFontName("Courier New");
// 设置样式;
HSSFCellStyle style = workbook.createCellStyle();
// 设置底边框;
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
// 设置底边框颜色;
style.setBottomBorderColor(HSSFColor.BLACK.index);
// 设置左边框;
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
// 设置左边框颜色;
style.setLeftBorderColor(HSSFColor.BLACK.index);
// 设置右边框;
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
// 设置右边框颜色;
style.setRightBorderColor(HSSFColor.BLACK.index);
// 设置顶边框;
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
// 设置顶边框颜色;
style.setTopBorderColor(HSSFColor.BLACK.index);
// 在样式用应用设置的字体;
style.setFont(font);
// 设置自动换行;
style.setWrapText(false);
// 设置水平对齐的样式为居中对齐;
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 设置垂直对齐的样式为居中对齐;
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
if(index == 2){
//设置偶数行背景色
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setFillForegroundColor((short)12);
}
return style;
}
// 以下为服务器端判断客户端浏览器类型的方法
public static String getBrowser(HttpServletRequest request) {
String UserAgent = request.getHeader("USER-AGENT").toLowerCase();
if (UserAgent != null) {
if (UserAgent.indexOf("msie") >= 0)
return "IE";
if (UserAgent.indexOf("firefox") >= 0)
return "FF";
if (UserAgent.indexOf("safari") >= 0)
return "SF";
}
return null;
}
}
2.工具类的调用
//导出数据
@RequestMapping("getShujufenxiExport")
public void getShujufenxiExport(HttpServletRequest request, HttpServletResponse response,Map<String, Object> out,Search search){
Integer flag = 2;
List<ComplianceAlarm> list = complianceAlarmService.queryComplianceRateList(search,flag);
String title="数据分析";
String[] rowName = new String[]{"科室","时间周期","时间","依从率(%)"};
ExportExcel ex = new ExportExcel(title, rowName, list);
List<Integer> columnWidthList = new ArrayList<Integer>() ;
columnWidthList.add(30*255);
columnWidthList.add(30*255);
columnWidthList.add(35*255);
columnWidthList.add(30*255);
try {
ex.export("数据分析-"+DateUtil.format(new Date(), "yyyyMMddhhmmss"), columnWidthList, request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
- Java带样式导出excle(poi)
- Java操作POI导出excle(单个excle)
- POI导出Excle HSSF
- poi导出excle
- POI导出excle数据
- Java操作POI批量导出多个excle
- POI根据模板导出Excle
- poi导出excle方式二
- Apache POI 导入导出EXCLE
- excle导出纵向表格(POI)
- Java Poi 操作Excle
- Java Poi 操作Excle
- POI导出excle,文件导出到本地
- java excle导入 导出
- java excle导出
- java 导出excle
- Apache poi导出word和Excle
- POI 方式-excle导出工具类实现
- Codeforces Round #433 C
- 对下列数据进行操作
- 关于Lan和Wan网口分配配置
- null排序用法
- taskctl的后台字符界面登录不了解决办法
- Java带样式导出excle(poi)
- PHP的魔术方法
- SpringMVC——处理模型数据
- json时间字符串带T
- Yii
- C# winform 判断程序是否已经在运行 判断程序是否已经打开
- 分析Handler异步消息机制关键步骤
- 高德地图web服务API
- .9图片设置不拉伸的区域被拉伸的问题