package com.adtech.tools;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
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.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
/**
* 报表打印工具类
*
* @date 2010-01-22
*/
public class ReportTool {
/**
* Excel编码处理
*
* @param toEncoded
* @param encoding
* @return
*/
public static String getUnicode(String toEncoded, String encoding) {
String retString = "";
if (toEncoded.equals("") || toEncoded.trim().equals("")) {
return toEncoded;
}
try {
byte[] b = toEncoded.getBytes(encoding);
sun.io.ByteToCharConverter convertor = sun.io.ByteToCharConverter
.getConverter(encoding);
char[] c = convertor.convertAll(b);
for (int i = 0; i < c.length; i++) {
retString += String.valueOf(c[i]);
}
} catch (java.io.UnsupportedEncodingException usee) {
System.out.println("不支持" + encoding + "编码方式");
usee.printStackTrace();
} catch (sun.io.MalformedInputException mfie) {
System.out.println("输入参数无效!!!");
mfie.printStackTrace();
}
return retString;
}
/**
* 处理文件名乱码
*
* @param str
* @return
*/
public static String getStr(String str) {
try {
String temp_p = str;
byte[] temp_t = temp_p.getBytes("ISO8859_1");
String temp = new String(temp_t);
return temp;
} catch (Exception e) {
e.printStackTrace();
}
return "null";
}
/**
* 定义字体大小样式
*
* @param workbook
* @param color
* 字体颜色
* @param boldweight
* 字体样式
* @param fontHeight
* 字体大小
* @return
*/
public static HSSFFont createHSSFFont(HSSFWorkbook workbook, short color,
short boldweight, int fontHeight) {
HSSFFont font = workbook.createFont();
font.setColor(HSSFFont.COLOR_NORMAL);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setFontHeight((short) fontHeight);
return font;
}
/**
* 定义边框样式 单元格四周都有边框
*
* @param workbook
* @param verticalAlignment
* 垂直显示
* @param alignment
* 水平显示样式
* @param top
* 上边框粗细
* @param bottom
* 下边框粗细
* @param left
* 左边框粗细
* @param right
* 右边框粗细
* @return
*/
public static HSSFCellStyle createHSSFCellStyle(HSSFWorkbook workbook,
short verticalAlignment, short alignment, int top, int bottom,
int left, int right) {
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setVerticalAlignment(verticalAlignment);
cellStyle.setAlignment(alignment);
cellStyle.setBorderBottom((short) bottom);
cellStyle.setBorderLeft((short) left);
cellStyle.setBorderRight((short) right);
cellStyle.setBorderTop((short) top);
return cellStyle;
}
/**
* 定义边框样式 单元格四周都有边框
*
* @param workbook
* @param verticalAlignment
* 垂直显示
* @param alignment
* 水平显示样式
* @param top
* 上边框粗细
* @param bottom
* 下边框粗细
* @param left
* 左边框粗细
* @param right
* 右边框粗细
* @param font
* 单元格字体样式
* @return
*/
public static HSSFCellStyle createHSSFCellStyle(HSSFWorkbook workbook,
HSSFFont font, short verticalAlignment, short alignment, int top,
int bottom, int left, int right) {
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setVerticalAlignment(verticalAlignment);
cellStyle.setAlignment(alignment);
cellStyle.setBorderBottom((short) bottom);
cellStyle.setBorderLeft((short) left);
cellStyle.setBorderRight((short) right);
cellStyle.setBorderTop((short) top);
cellStyle.setFont(font);
return cellStyle;
}
/**
* 创建行
*
* @param sheet
* 工作表
* @param rowNum
* 行号
* @param height
* 行高
*
* @return
*/
public static HSSFRow createHSSFRow(HSSFSheet sheet, int rowNum, int height) {
HSSFRow row = sheet.createRow((short) rowNum);
row.setHeight((short) height);
return row;
}
/**
* 创建列
*
* @param hssfRow
* 行对象
* @param cellStyle
* 单元格样式
* @param cellNum
* 列号
* @param content
* 单元格内容
*
*/
public static void createHSSFCell(HSSFRow hssfRow, HSSFCellStyle cellStyle,
int cellNum, String content) {
HSSFCell cell = hssfRow.createCell((short) cellNum);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(cellStyle);
content = getUnicode(content, "gb2312");
cell.setCellValue(content);
}
/**
* 创建列
*
* @param hssfRow
* 行对象
* @param cellStyle
* 单元格样式
* @param cellNum
* 列号
* @param content
* 单元格内容
* @param beginRow
* 合并单元格开始行
* @param beginCell
* 合并单元格开始列
* @param endRow
* 合并单元格结束行
* @param endCell
* 合并单元格结束列
*
*/
public static void createHSSFCell(HSSFSheet sheet, HSSFRow hssfRow,
HSSFCellStyle cellStyle, int cellNum, String content, int beginRow,
int beginCell, int endRow, int endCell) {
HSSFCell cell = hssfRow.createCell((short) cellNum);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(cellStyle);
content = getUnicode(content, "gb2312");
cell.setCellValue(content);
sheet.addMergedRegion(new Region((short) beginRow, (short) beginCell,
(short) endRow, (short) endCell));
for (int i = beginCell + 1; i < endCell + 1; i++) {
cell = hssfRow.createCell((short) i);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(cellStyle);
}
if (beginRow < endRow) {
for (int j = beginRow + 1; j < endRow + 1; j++) {
HSSFRow row = ReportTool.createHSSFRow(sheet, j, 300);
for (int i = beginCell; i < endCell + 1; i++) {
cell = row.createCell((short) i);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(cellStyle);
}
}
}
}
/**
* 设置页面返回类型
*
* @param request
* @param response
* @param jyPath
* @throws ServletException
* @throws IOException
*/
public static void setExclType(HttpServletRequest request,
HttpServletResponse response, String jyPath) {
response.setContentType("application/octet-stream");
String outputFile = request.getParameter("TEMPLET_NNAME") != null ? getUnicode(
request.getParameter("TEMPLET_NNAME"), "gb2312")
+ ".xls"
: "";
HttpSession session = request.getSession();
if (outputFile.equals("")) {
// 所有状态查询标题不能为空
session.setAttribute("ADQMS9000_MESSAGE_TYPE", "1");
session.setAttribute("ADQMS9000_MESSAGE_CONTENT", "你没有权限导出!");
session.setAttribute("ADQMS9000_MESSAGE_GOTO",
"javascript:history.back(1)");
try {
request.getRequestDispatcher(jyPath).forward(request, response);
} catch (Exception e) {
}
}
String fileName = outputFile;
response.setHeader("Content-Disposition", "attachment;filename="
+ fileName);
}
}
分享到:
相关推荐
Excel日常操作的一些工具类,简化开发,如有其他需求可以联系我,该工具类可满足日常开发的常见需求。
主要是针对于poi3.9版本进行pdf导出的工具类配置,比如设置页边距,设置背景色等,以及设定固定模块的写法
poigongju
excel导入导出(poi工具类)
Apache POI 工具类
Excel导入导出 jxl及Poi 工具类:jxl 实现 及 Poi实现 非常实用
excel poi工具类 导入 导出 合并单元格 计算公式
poi导出表格的工具类
easypoi功能如同名字easy,主打的功能就是容易,让一个没见...POI 工具类,Excel的快速导入导出,Excel模板导出,Word模板导出,可以仅仅5行代码就可以完成Excel的导入导出,修改导出格式简单粗暴,快速有效,easypoi值得你尝试
概述:Java poi导入导出EXCEL工具类(兼容各版本) 一、功能说明 允许同时导入或导出多个sheet,同一sheet可同时存在多个数据块,按数据块划分处理数据。 二、配置文件示例及详细说明 1、导入xml配置示例如下(见...
poi的工具类
在开发过程中工具类可以简化我们的操作,提高代码重用.下面是我操作poi(一个Excel操作组件)的工具类.请大家指点
java中poi读写excel封装工具类(兼容office2003和2007等版本),绝对可用的例子!
POI Excel 工具类POI Excel 工具类POI Excel 工具类POI Excel 工具类POI Excel 工具类
POIUtil:poi工具类,excel导出 QrCodeUtil:二维码操作工具, 包括生成和读取 ShellUtil:shell命令操作工具,包括linux登陆,命令执行...... 较为简单,具体需要自行扩充 SignUtil:签名工具,包括MD5 位运算 ...
使用POI工具类,导入到项目中,引用项目的方法,修改一下内部的Bean类型,就可以将数据导出为excel格式的文件。(支持excel2013)
Poi导出Excel工具类 支持大数据量 多sheet页方式 已经封装好,直接拿来调用里面的方法传入参数就行 很简单
java使用POI导出 Excel+图片工具类 ,里面含有poi jar包,只调用接口即可直接保存Excel。使用的时候需先把数据封装,具体包装需根据实际导出数据进行处理。文件demo中只提供包装格式。
一个比较实用的java导出excel工具类,使用的是Poi技术